程序
#include "widget.h"
//對於 Qt 程序來說,GUI 程序是QApplication
//非 GUI 程序是QCoreApplication。QApplication派生自QCoreApplication
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //創建一個應用程序對象,該對象有且僅有一個。
Widget w; //創建一個窗口
w.show(); //把窗口顯式出來。默認創建的窗口不會直接顯式,需要show一下
return a.exec(); //進入事件循環
}
運行結果如下:
//Widgets.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget> //包含QWidget模塊
class Widget : public QWidget //Widget派生自QWidget
{
Q_OBJECT //Q_OBJECT表示允許使用信號槽機制。
public:
Widget(QWidget *parent = nullptr);
~Widget();
};
#endif // WIDGET_H
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent) //調用父類構造函數
{
}
Widget::~Widget()
{
}