13b3ebbe14
把川大 wiki「02.Qt方向」课程代码整理为 CMake 管理的可编译运行项目。
Part1 C/C++ 强化 (p01/, 144 目标, host gcc/g++ 构建, 全绿):
- 修复全部编译失败:补 <cstring>/<fstream>/<iomanip>/<vector> 等缺失头
(common_fix + infer_stl_includes 自动检测注入)、修正 pause() 壳注入
(改检测已转换后的 pause 调用)、namespace 提升出函数体、const 成员函数
正确性、dedupe 不再破坏函数重载集、K&R 隐式 int / 动态异常规范等
C++17 移除特性的可编译等价改写。
- 目录采用深缩写 p01/ch04/s03/ (原 part1_cpp/ch04_class_object/03_ctor_dtor)。
Part3 Qt 桌面 (p03/, 29 目标, 隔离 Qt 5.14.2 构建, 全绿):
- gen_part3.py: Qt 外壳模板 (main+QApplication+show, QTimer 自动退出便于
offscreen 验证)、AUTOMOC + .moc include、rewrite_* 处理散文混入/重载/
资源依赖、生成占位 png/gif + .qrc + rundata 样例数据。
- 修正 QLable 笔误、全角分号、.→->、Windows 路径、QApplication 阻塞事件
循环改为 QCoreApplication 等。
任务1 概念/版本验证:
- tools/std_probe.py: 跨 -std= 编译探针 (--pedantic-errors 让已废除特性硬失败)。
- tools/demo_versions/: K&R 隐式 int、三目左值、void* 转换、enum 赋整、
const 经指针、动态异常规范 共 6 个跨版本演示。
- docs/VERSION_NOTES.md: 每条「原文说法→C17/C++17 是否成立→何版本成立→已验证」。
文档: 顶层 README、docs/SOURCE_PAGES.md (页面→目录映射)、
docs/ERRATA.md (Part1 修正)、docs/ERRATA_part3.md (Part3 修正)。
另修复 tools/run.sh / run_qt.sh 的 ${1:?...} 引号语法 bug。
隔离 Qt 验证: ldd 0 系统 Qt 引用; 173/173 目标全绿; p03 offscreen 运行通过。
1 line
10 KiB
JSON
1 line
10 KiB
JSON
{"id":"58954515","type":"page","status":"current","title":"2 创建Qt项目","version":{"by":{"type":"known","username":"xpei","userKey":"2c90931b693d413801693d73eda10000","profilePicture":{"path":"/images/icons/profilepics/default.svg","width":48,"height":48,"isDefault":true},"displayName":"卢鑫培","_links":{"self":"https://wiki.suncaper.net/rest/api/user?key=2c90931b693d413801693d73eda10000"},"_expandable":{"status":""}},"when":"2023-07-09T02:15:16.000Z","message":"","number":2,"minorEdit":false,"hidden":false,"_links":{"self":"https://wiki.suncaper.net/rest/experimental/content/58954515/version/2"},"_expandable":{"content":"/rest/api/content/58954515"}},"body":{"storage":{"value":"<h1><ac:structured-macro ac:name=\"anchor\" ac:schema-version=\"1\" ac:macro-id=\"da8eb3f7-5320-491d-95a1-cc9389891a1c\"><ac:parameter ac:name=\"\">_Toc466556762</ac:parameter></ac:structured-macro><ac:structured-macro ac:name=\"anchor\" ac:schema-version=\"1\" ac:macro-id=\"8089f051-148d-4eb5-b3a0-9863610db50e\"><ac:parameter ac:name=\"\">_Toc471841990</ac:parameter></ac:structured-macro>2.1 使用向导创建</h1><p>打开Qt Creator 界面选择 New Project或者选择菜单栏 【文件】-【新建文件或项目】菜单项<br /><ac:image ac:height=\"417\" ac:width=\"554\"><ri:attachment ri:filename=\"worddave1a792da416f552dc5c38ed337a82246.png\" /></ac:image><br />弹出New Project对话框,选择Qt Widgets Application(窗口小部件)<br /><ac:image ac:height=\"330\" ac:width=\"554\"><ri:attachment ri:filename=\"worddav2fd0a78c9e09a3aa8f01ef637954a948.png\" /></ac:image><br />选择【Choose】按钮,弹出如下对话框<br /><ac:image ac:height=\"407\" ac:width=\"495\"><ri:attachment ri:filename=\"worddavac798200ecd68cc94ed227aa095d8443.png\" /></ac:image><br />设置项目名称和路径,按照向导进行下一步,<br /><ac:image ac:height=\"330\" ac:width=\"554\"><ri:attachment ri:filename=\"worddav659bb3ce4eb765db50e6a8aad152c4e7.png\" /></ac:image><br />选择编译套件<br /><ac:image ac:height=\"439\" ac:width=\"547\"><ri:attachment ri:filename=\"worddav6d0b87b9e12b72cadb81cc60a190a7a9.png\" /></ac:image><br />向导会默认添加一个继承自CMainWindow的类,可以在此修改类的名字和基类。默认的基类有QMainWindow、QWidget以及QDialog三个,我们可以选择QWidget(类似于空窗口),这里我们可以先创建一个不带UI的界面,继续下一步<br /><ac:image ac:height=\"478\" ac:width=\"576\"><ri:attachment ri:filename=\"worddav68c5fb54f0b58e19d0807d2927491837.png\" /></ac:image><br />系统会默认给我们添加main.cpp、mywidget.cpp、 mywidget.h和一个.pro项目文件,点击完成,即可创建出一个Qt桌面程序。</p><h1><ac:structured-macro ac:name=\"anchor\" ac:schema-version=\"1\" ac:macro-id=\"627211f7-6f5c-445e-b1ab-6d198ba2c221\"><ac:parameter ac:name=\"\">_Toc466556763</ac:parameter></ac:structured-macro><ac:structured-macro ac:name=\"anchor\" ac:schema-version=\"1\" ac:macro-id=\"9a5da65e-cffc-4c54-a207-00342c4b6ca8\"><ac:parameter ac:name=\"\">_Toc471841991</ac:parameter></ac:structured-macro>手动创建</h1><p>添加一个空项目<br /><ac:image ac:height=\"402\" ac:width=\"554\"><ri:attachment ri:filename=\"worddav7dfcce1991e20943a810f4a582f4b775.png\" /></ac:image><br />选择【choose】进行下一步。设置项目名称和路径 —> 选择编译套件 --> 修改类信息 --> 完成(步骤同上),生成一个空项目。在空项目中添加文件:在项目名称上单击鼠标右键弹出右键菜单,选择【添加新文件】<br /><ac:image ac:height=\"378\" ac:width=\"379\"><ri:attachment ri:filename=\"worddav032ebab4a3e6b0cda05b56c82546e366.png\" /></ac:image><br />弹出新建文件对话框<br /><ac:image ac:height=\"266\" ac:width=\"555\"><ri:attachment ri:filename=\"worddavb403fffeab362700a5853a90555e33fc.png\" /></ac:image><br />在此对话框中选择要添加的类或者文件,根据向导完成文件的添加。</p><h1><ac:structured-macro ac:name=\"anchor\" ac:schema-version=\"1\" ac:macro-id=\"93d3f31a-868d-419a-b84b-5bdf7f701cfc\"><ac:parameter ac:name=\"\">_Toc466556764</ac:parameter></ac:structured-macro><ac:structured-macro ac:name=\"anchor\" ac:schema-version=\"1\" ac:macro-id=\"5c8fa240-5a52-4474-849c-ca30a7b77271\"><ac:parameter ac:name=\"\">_Toc471841992</ac:parameter></ac:structured-macro>.pro文件</h1><p>在使用Qt向导生成的应用程序.pro文件格式如下: <br class=\"atl-forced-newline\" />QT += core gui //包含的模块<br />greaterThan(QT_MAJOR_VERSION, 4): QT += widgets //大于Qt4版本 才包含widget模块<br />TARGET = QtFirst //应用程序名 生成的.exe程序名称<br />TEMPLATE = app //模板类型 应用程序模板<br />SOURCES += main.cpp\\ //源文件<br />mywidget.cpp<br />HEADERS += mywidget.h //头文件 <br class=\"atl-forced-newline\" /><span style=\"color: rgb(255,0,0);\"><strong>.pro就是工程文件(project),它是qmake自动生成的用于生产makefile的配置文件</strong></span>。.pro文件的写法如下:</p><ul><li>注释</li></ul><p>从"#"开始,到这一行结束。</p><ul><li>模板变量告诉qmake为这个应用程序生成哪种makefile。下面是可供使用的选择:<span style=\"color: rgb(255,0,0);\"><strong>TEMPLATE</strong></span> = app<ul><li>app -建立一个应用程序的makefile。这是默认值,所以如果模板没有被指定,这个将被使用。</li><li>lib - 建立一个库的makefile。</li><li>vcapp - 建立一个应用程序的VisualStudio项目文件。</li><li>vclib - 建立一个库的VisualStudio项目文件。</li><li>subdirs -这是一个特殊的模板,它可以创建一个能够进入特定目录并且为一个项目文件生成makefile并且为它调用make的makefile。</li></ul></li><li>#指定生成的应用程序名:</li></ul><p><span style=\"color: rgb(255,0,0);\"><strong>TARGET</strong></span> = QtDemo</p><ul><li>#工程中包含的头文件</li></ul><p><span style=\"color: rgb(255,0,0);\"><strong>HEADERS</strong></span> += include/painter.h</p><ul><li>#工程中包含的.ui设计文件</li></ul><p><span style=\"color: rgb(255,0,0);\"><strong>FORMS</strong></span> += forms/painter.ui</p><ul><li>#工程中包含的源文件</li></ul><p><span style=\"color: rgb(255,0,0);\"><strong>SOURCES</strong></span> += sources/main.cpp sources</p><ul><li>#工程中包含的资源文件</li></ul><p><span style=\"color: rgb(255,0,0);\"><strong>RESOURCES</strong></span> += qrc/painter.qrc</p><ul><li><span style=\"color: rgb(255,0,0);\"><strong>greaterThan(QT_MAJOR_VERSION, 4): QT += widgets</strong></span></li></ul><p><span style=\"color: rgb(255,0,0);\"><strong>这条语句的含义是,如果QT_MAJOR_VERSION大于4(也就是当前使用的Qt5及更高版本)需要增加widgets模块。如果项目仅需支持Qt5,也可以直接添加"QT += widgets"一句。不过为了保持代码兼容,最好还是按照QtCreator生成的语句编写。</strong></span></p><ul><li>#配置信息</li></ul><p>CONFIG用来告诉qmake关于应用程序的配置信息。<br />CONFIG <span style=\"text-decoration: underline;\">= c</span><span style=\"text-decoration: underline;\">11//使用c</span>+11的特性<br />在这里使用"+=",是因为我们添加我们的配置选项到任何一个已经存在中。这样做比使用"="那样替换已经指定的所有选项更安全。</p><h1><ac:structured-macro ac:name=\"anchor\" ac:schema-version=\"1\" ac:macro-id=\"7c6665a2-00c4-4379-98bd-17db6fd99838\"><ac:parameter ac:name=\"\">_Toc466556765</ac:parameter></ac:structured-macro><ac:structured-macro ac:name=\"anchor\" ac:schema-version=\"1\" ac:macro-id=\"30dd7b04-6a81-4f29-ae05-e1757db156c1\"><ac:parameter ac:name=\"\">_Toc471841993</ac:parameter></ac:structured-macro>一个最简单的Qt应用程序</h1><p>main<span>入口函数中</span></p><p><br /></p><ac:structured-macro ac:name=\"code\" ac:schema-version=\"1\" ac:macro-id=\"a225a304-e5b9-4ad4-8bd2-88508ebb4f13\"><ac:parameter ac:name=\"language\">cpp</ac:parameter><ac:plain-text-body><![CDATA[#include \"widget.h\"\n#include <QApplication>\n\nint main(int argc, char *argv[])\n{\n QApplication a(argc, argv);\n Widget w;\n w.show();\n\n return a.exec();\n}\n\n]]></ac:plain-text-body></ac:structured-macro><p><br /></p><ul><li>Qt系统提供的标准类名声明头文件没有.h后缀</li><li>Qt一个类对应一个头文件,类名就是头文件名</li><li>QApplication应用程序类<ul><li>管理图形用户界面应用程序的控制流和主要设置。</li><li>是Qt的整个后台管理的命脉它<span style=\"color: rgb(255,0,0);\"><strong>包含主事件循环</strong></span>,在其中来自窗口系统和其它资源的<span style=\"color: rgb(255,0,0);\"><strong>所有事件处理和调度</strong></span>。它也处理<span style=\"color: rgb(255,0,0);\"><strong>应用程序的初始化和结束</strong></span>,并且<span style=\"color: rgb(255,0,0);\"><strong>提供对话管理</strong></span>。</li><li>对于任何一个使用Qt的图形用户界面应用程序,都正好存在一个QApplication 对象,而不论这个应用程序在同一时间内是不是有0、1、2或更多个窗口。</li></ul></li><li>a.exec()</li></ul><p><span style=\"color: rgb(255,0,0);\"><strong>程序进入消息循环</strong></span>,等待对用户输入进行响应。这里main()把控制权转交给Qt,Qt完成事件处理工作,当应用程序退出的时候exec()的值就会返回。<span style=\"color: rgb(255,0,0);\"><strong>在exec()中,Qt接受并处理用户和系统的事件并且把它们传递给适当的窗口部件。</strong></span></p>","representation":"storage","_expandable":{"content":"/rest/api/content/58954515"}},"_expandable":{"editor":"","view":"","export_view":"","styled_view":"","anonymous_export_view":""}},"extensions":{"position":14},"_links":{"webui":"/pages/viewpage.action?pageId=58954515","edit":"/pages/resumedraft.action?draftId=58954515","tinyui":"/x/E5ODAw","collection":"/rest/api/content","base":"https://wiki.suncaper.net","context":"","self":"https://wiki.suncaper.net/rest/api/content/58954515"},"_expandable":{"container":"/rest/api/space/2CT","metadata":"","operations":"","children":"/rest/api/content/58954515/child","restrictions":"/rest/api/content/58954515/restriction/byOperation","history":"/rest/api/content/58954515/history","ancestors":"","descendants":"/rest/api/content/58954515/descendant","space":"/rest/api/space/2CT"}} |