.pro:qmake 專案配置,生成 Makefilemain.cpp:入口點xxx.cpp / xxx.h:自定義類(預設為空)
#include <QtGui/QApplication>
#include <QLabel>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QLabel *label = new QLabel("Hello, world!");
label->show();
return a.exec(); // 進入事件循環
}
點擊綠色三角形 Run,彈出標籤視窗顯示 "Hello, world!"。
逐行解析上述代碼:QApplication、QLabel、事件循環 exec()、對象樹內存管理等核心機制。