课程代码仓库初始化:Part1 (144) + Part3 (29) 全量生成与验证
把川大 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 运行通过。
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
# Auto-generated by tools/gen_part3.py — do not edit by hand.
|
||||
# 章节:p03/ch08
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
add_executable(label_text_create label_text_create.cpp)
|
||||
set_target_properties(label_text_create PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF AUTOMOC ON)
|
||||
target_link_libraries(label_text_create PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets)
|
||||
target_compile_options(label_text_create PRIVATE -Wall -Wextra)
|
||||
add_executable(label_text_html label_text_html.cpp)
|
||||
set_target_properties(label_text_html PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF AUTOMOC ON)
|
||||
target_link_libraries(label_text_html PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets)
|
||||
target_compile_options(label_text_html PRIVATE -Wall -Wextra)
|
||||
qt5_add_resources(RES label_pixmap.qrc)
|
||||
add_executable(label_pixmap label_pixmap.cpp ${RES})
|
||||
set_target_properties(label_pixmap PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF AUTOMOC ON)
|
||||
target_link_libraries(label_pixmap PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets)
|
||||
target_compile_options(label_pixmap PRIVATE -Wall -Wextra)
|
||||
qt5_add_resources(RES label_movie.qrc)
|
||||
add_executable(label_movie label_movie.cpp ${RES})
|
||||
set_target_properties(label_movie PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF AUTOMOC ON)
|
||||
target_link_libraries(label_movie PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets)
|
||||
target_compile_options(label_movie PRIVATE -Wall -Wextra)
|
||||
add_executable(custom_widget custom_widget.cpp)
|
||||
set_target_properties(custom_widget PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF AUTOMOC ON)
|
||||
target_link_libraries(custom_widget PRIVATE Qt5::Core Qt5::Gui Qt5::Widgets)
|
||||
target_compile_options(custom_widget PRIVATE -Wall -Wextra)
|
||||
add_custom_target(all_p03_ch08 DEPENDS label_text_create label_text_html label_pixmap label_movie custom_widget)
|
||||
@@ -0,0 +1,94 @@
|
||||
# p03/ch08 — Qt 示例
|
||||
|
||||
## 可运行示例
|
||||
|
||||
| 目标 | 源文件 | 说明 |
|
||||
|---|---|---|
|
||||
| `label_text_create` | `label_text_create.cpp` | 构建运行见顶层 README;离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh label_text_create` |
|
||||
| `label_text_html` | `label_text_html.cpp` | 构建运行见顶层 README;离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh label_text_html` |
|
||||
| `label_pixmap` | `label_pixmap.cpp` | 构建运行见顶层 README;离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh label_pixmap` |
|
||||
| `label_movie` | `label_movie.cpp` | 构建运行见顶层 README;离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh label_movie` |
|
||||
| `custom_widget` | `custom_widget.cpp` | 构建运行见顶层 README;离屏验证 `QT_QPA_PLATFORM=offscreen tools/run_qt.sh custom_widget` |
|
||||
|
||||
## 参考片段(签名/伪代码,未单独建目标)
|
||||
|
||||
### [block 0] 显示文字 (普通文本、html)
|
||||
|
||||
```cpp
|
||||
voidsetText(const QString &)
|
||||
```
|
||||
|
||||
### [block 3] 显示图片
|
||||
|
||||
```cpp
|
||||
voidsetPixmap(const QPixmap &)
|
||||
```
|
||||
|
||||
### [block 4] 显示图片
|
||||
|
||||
```cpp
|
||||
QPixmap pixmap;
|
||||
```
|
||||
|
||||
### [block 5] 显示图片
|
||||
|
||||
```cpp
|
||||
pixmap.load(":/Image/boat.jpg");
|
||||
```
|
||||
|
||||
### [block 6] 显示图片
|
||||
|
||||
```cpp
|
||||
QLabel *label = new QLabel;
|
||||
label.setPixmap(pixmap);
|
||||
```
|
||||
|
||||
### [block 7] 显示动画
|
||||
|
||||
```cpp
|
||||
voidsetMovie(QMovie * movie)
|
||||
```
|
||||
|
||||
### [block 8] 显示动画
|
||||
|
||||
```cpp
|
||||
QMovie *movie = new QMovie(":/Mario.gif");
|
||||
```
|
||||
|
||||
### [block 9] 显示动画
|
||||
|
||||
```cpp
|
||||
movie->start();
|
||||
```
|
||||
|
||||
### [block 10] 显示动画
|
||||
|
||||
```cpp
|
||||
QLabel *label = new QLabel;
|
||||
label->setMovie(movie);
|
||||
```
|
||||
|
||||
### [block 11] 设置/获取内容
|
||||
|
||||
```cpp
|
||||
QStringtext() const
|
||||
```
|
||||
|
||||
### [block 12] 设置/获取内容
|
||||
|
||||
```cpp
|
||||
voidsetText(const QString &)
|
||||
```
|
||||
|
||||
### [block 13] 设置显示模式
|
||||
|
||||
```cpp
|
||||
voidsetEchoMode(EchoMode mode)
|
||||
```
|
||||
|
||||
### [block 14] 设置显示模式
|
||||
|
||||
```cpp
|
||||
void setTextMargins(int left, int top, int right, int bottom)
|
||||
```
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 179 B |
Binary file not shown.
|
After Width: | Height: | Size: 40 B |
@@ -0,0 +1,44 @@
|
||||
// ============================================================================
|
||||
// custom_widget — Qt 桌面示例
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 原文片段:[58954540:15]
|
||||
// 说明:本文件由 gen_part3.py 从 wiki 片段生成(必要时补 main/QApplication
|
||||
// 外壳、include、修正笔误)。详见 docs/ERRATA.md。
|
||||
// 离屏验证:QT_QPA_PLATFORM=offscreen tools/run_qt.sh <target>
|
||||
// ============================================================================
|
||||
|
||||
#include <QApplication>
|
||||
#include <QWidget>
|
||||
#include <QSpinBox>
|
||||
#include <QSlider>
|
||||
#include <QHBoxLayout>
|
||||
#include <QTimer>
|
||||
|
||||
// 自定义控件:spinbox 与 slider 联动
|
||||
class SmallWidget : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
SmallWidget(QWidget* parent = nullptr) : QWidget(parent) {
|
||||
QSpinBox* spin = new QSpinBox(this);
|
||||
QSlider* slider = new QSlider(Qt::Horizontal, this);
|
||||
QHBoxLayout* layout = new QHBoxLayout(this);
|
||||
layout->addWidget(spin);
|
||||
layout->addWidget(slider);
|
||||
// spin 变化 → slider 跟随(Qt5 静态转换选重载)
|
||||
connect(spin,
|
||||
static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
|
||||
slider, &QSlider::setValue);
|
||||
// slider 变化 → spin 跟随
|
||||
connect(slider, &QSlider::valueChanged, spin, &QSpinBox::setValue);
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
QApplication app(argc, argv);
|
||||
SmallWidget w; w.show();
|
||||
// 离屏/自动化验证:200ms 后自动退出(GUI 模式下不阻塞)
|
||||
QTimer::singleShot(200, &app, &QApplication::quit);
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
#include "custom_widget.moc"
|
||||
@@ -0,0 +1,37 @@
|
||||
// ============================================================================
|
||||
// label_movie — Qt 桌面示例
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 原文片段:[58954540:movie]
|
||||
// 说明:本文件由 gen_part3.py 从 wiki 片段生成(必要时补 main/QApplication
|
||||
// 外壳、include、修正笔误)。详见 docs/ERRATA.md。
|
||||
// 离屏验证:QT_QPA_PLATFORM=offscreen tools/run_qt.sh <target>
|
||||
// ============================================================================
|
||||
|
||||
#include <QApplication>
|
||||
#include <QWidget>
|
||||
#include <QLabel>
|
||||
#include <QMovie>
|
||||
#include <QTimer>
|
||||
|
||||
class DemoWidget : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
DemoWidget() {
|
||||
QMovie* movie = new QMovie(":/Mario.gif"); // 从资源加载动画
|
||||
movie->start();
|
||||
QLabel* label = new QLabel(this);
|
||||
label->setMovie(movie);
|
||||
label->setGeometry(10, 10, 200, 200);
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
QApplication app(argc, argv);
|
||||
DemoWidget w;
|
||||
w.show();
|
||||
// 离屏/自动化验证:200ms 后自动退出(GUI 模式下不阻塞)
|
||||
QTimer::singleShot(200, &app, &QApplication::quit);
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
#include "label_movie.moc"
|
||||
@@ -0,0 +1,5 @@
|
||||
<RCC>
|
||||
<qresource>
|
||||
<file alias="//Mario.gif">assets/mario.gif</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
@@ -0,0 +1,37 @@
|
||||
// ============================================================================
|
||||
// label_pixmap — Qt 桌面示例
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 原文片段:[58954540:img]
|
||||
// 说明:本文件由 gen_part3.py 从 wiki 片段生成(必要时补 main/QApplication
|
||||
// 外壳、include、修正笔误)。详见 docs/ERRATA.md。
|
||||
// 离屏验证:QT_QPA_PLATFORM=offscreen tools/run_qt.sh <target>
|
||||
// ============================================================================
|
||||
|
||||
#include <QApplication>
|
||||
#include <QWidget>
|
||||
#include <QLabel>
|
||||
#include <QPixmap>
|
||||
#include <QTimer>
|
||||
|
||||
class DemoWidget : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
DemoWidget() {
|
||||
QPixmap pixmap;
|
||||
pixmap.load(":/Image/boat.jpg"); // 从资源加载图片
|
||||
QLabel* label = new QLabel(this);
|
||||
label->setPixmap(pixmap); // 注意:label 是指针,用 ->
|
||||
label->setGeometry(10, 10, 200, 120);
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
QApplication app(argc, argv);
|
||||
DemoWidget w;
|
||||
w.show();
|
||||
// 离屏/自动化验证:200ms 后自动退出(GUI 模式下不阻塞)
|
||||
QTimer::singleShot(200, &app, &QApplication::quit);
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
#include "label_pixmap.moc"
|
||||
@@ -0,0 +1,5 @@
|
||||
<RCC>
|
||||
<qresource>
|
||||
<file alias="/Image/boat.jpg">assets/image_boat.jpg</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
@@ -0,0 +1,33 @@
|
||||
// ============================================================================
|
||||
// label_text_create — Qt 桌面示例
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 原文片段:[58954540:1]
|
||||
// 说明:本文件由 gen_part3.py 从 wiki 片段生成(必要时补 main/QApplication
|
||||
// 外壳、include、修正笔误)。详见 docs/ERRATA.md。
|
||||
// 离屏验证:QT_QPA_PLATFORM=offscreen tools/run_qt.sh <target>
|
||||
// ============================================================================
|
||||
|
||||
#include <QApplication>
|
||||
#include <QWidget>
|
||||
#include <QLabel>
|
||||
#include <QTimer>
|
||||
|
||||
class DemoWidget : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
DemoWidget(){
|
||||
QLabel *label = new QLabel;
|
||||
label->setText("Hello, World!");
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
QApplication app(argc, argv);
|
||||
DemoWidget w;
|
||||
w.show();
|
||||
// 离屏/自动化验证:200ms 后自动退出(GUI 模式下不阻塞)
|
||||
QTimer::singleShot(200, &app, &QApplication::quit);
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
#include "label_text_create.moc"
|
||||
@@ -0,0 +1,35 @@
|
||||
// ============================================================================
|
||||
// label_text_html — Qt 桌面示例
|
||||
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
|
||||
// 原文片段:[58954540:2]
|
||||
// 说明:本文件由 gen_part3.py 从 wiki 片段生成(必要时补 main/QApplication
|
||||
// 外壳、include、修正笔误)。详见 docs/ERRATA.md。
|
||||
// 离屏验证:QT_QPA_PLATFORM=offscreen tools/run_qt.sh <target>
|
||||
// ============================================================================
|
||||
|
||||
#include <QApplication>
|
||||
#include <QWidget>
|
||||
#include <QLabel>
|
||||
#include <QTimer>
|
||||
|
||||
class DemoWidget : public QWidget {
|
||||
Q_OBJECT
|
||||
public:
|
||||
DemoWidget(){
|
||||
QLabel * label = new QLabel(this);
|
||||
label ->setText("Hello, World");
|
||||
label ->setText("<h1><a href=\"https://www.baidu.com\">百度一下</a></h1>");
|
||||
label ->setOpenExternalLinks(true);
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
QApplication app(argc, argv);
|
||||
DemoWidget w;
|
||||
w.show();
|
||||
// 离屏/自动化验证:200ms 后自动退出(GUI 模式下不阻塞)
|
||||
QTimer::singleShot(200, &app, &QApplication::quit);
|
||||
return app.exec();
|
||||
}
|
||||
|
||||
#include "label_text_html.moc"
|
||||
Reference in New Issue
Block a user