1f66098277
p03/ 生成产物与 docs/teaching/examples/ 手工示例里的 QTimer::singleShot 自动退出逻辑,从"始终 200ms 后退出"改为"仅当 QT_QPA_PLATFORM=offscreen 时才退出"。正常运行(有显示环境)窗口保持打开,需手动关闭;离屏自动化 验证(tools/run_qt.sh + QT_QPA_PLATFORM=offscreen)行为不变。 p03/ 改动来自重新生成 tools/gen_part3.py(AUTO_QUIT 常量加 qgetenv 判断), 未手改生成产物。QCoreApplication 控制台风格示例(qfile_basic 等)本就 执行完即退出,不受影响。
48 lines
1.8 KiB
C++
48 lines
1.8 KiB
C++
// ============================================================================
|
||
// button_creation — Qt 桌面示例
|
||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||
// 原文片段:[58954525:0]
|
||
// 说明:本文件由 gen_part3.py 从 wiki 片段生成(必要时补 main/QApplication
|
||
// 外壳、include、修正笔误)。详见 docs/ERRATA.md。
|
||
// 离屏验证:QT_QPA_PLATFORM=offscreen tools/run_qt.sh <target>(自动退出)
|
||
// ============================================================================
|
||
|
||
#include <QApplication>
|
||
#include <QWidget>
|
||
#include <QPushButton>
|
||
#include <QTimer>
|
||
|
||
class DemoWidget : public QWidget {
|
||
Q_OBJECT
|
||
public:
|
||
DemoWidget() {
|
||
// 第一种创建:先构造再设属性
|
||
QPushButton* btn = new QPushButton;
|
||
btn->setParent(this); // 设置父亲
|
||
btn->setText(QStringLiteral("德玛西亚")); // 设置文字
|
||
btn->move(100, 100); // 移动位置
|
||
|
||
// 第二种创建:构造时指定文本与父亲
|
||
QPushButton* btn2 = new QPushButton(QStringLiteral("孙悟空"), this);
|
||
btn2->move(100, 140);
|
||
|
||
this->resize(600, 400); // 重新指定窗口大小
|
||
this->setWindowTitle(QStringLiteral("第一个项目")); // 设置窗口标题
|
||
this->setFixedSize(600, 400); // 限制窗口大小
|
||
}
|
||
};
|
||
|
||
int main(int argc, char* argv[]) {
|
||
QApplication app(argc, argv);
|
||
DemoWidget w;
|
||
w.show();
|
||
// 仅在离屏自动化验证时(QT_QPA_PLATFORM=offscreen)200ms 后自动退出;
|
||
// 正常运行(有显示环境)窗口保持打开,不自动退出。
|
||
if (qgetenv("QT_QPA_PLATFORM") == "offscreen") {
|
||
QTimer::singleShot(200, &app, &QApplication::quit);
|
||
}
|
||
return app.exec();
|
||
}
|
||
|
||
#include "button_creation.moc"
|