Files
scuc-qt-course/docs/teaching/examples/mandelbrot_renderer/main.cpp
T
张宗平 6773f9b29e Day4 教学示例:Mandelbrot 分形渲染器(QPainter 绘图 + 多线程 + 事件处理综合演示)
三段式对比直击 Day4 slides「主线程耗时=冻结」痛点:主线程渲染冻结 / 后台单线程不卡但慢 / 后台多线程不卡且快。
- QThreadPool + QRunnable 分水平条带并行,排队信号槽回传,epoch 版本号防过期覆盖
- wheelEvent 缩放(鼠标为锚)+ mouseEvent 拖拽平移 + paintEvent 画 QImage
- 子目录自带独立 CMakeLists.txt,可单独构建/共享(双模式约定同 lineedit_eye_toggle)
- 配套 README(概念/走读/双构建/5 踩坑点/练习)+ 设计规格
- 已验证:独立工程 offscreen 运行 exit=0;主工程构建 exit=0(w64devkit g++16 + mingw73 Qt5.14.2)

注:主工程 examples/CMakeLists.txt 的接入(mandelbrot + lineedit_eye_toggle + qdialog + themes 原子接入)与顶层 CMakeLists 本地调试改动未一并提交,待整体确认后单独提交。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 09:01:29 +08:00

31 lines
1.3 KiB
C++

// ============================================================================
// main —— Day4 Mandelbrot 分形渲染器入口
// offscreen 平台(QT_QPA_PLATFORM=offscreen)下:show 触发首帧后台渲染,
// 200ms 后退出;aboutToQuit 等线程池收尾,避免悬垂任务访问已析构对象。
// 仓库自动化验证据此断言退出码 0(CLAUDE.md 行为风格第 2 点)。
// ============================================================================
#include <QApplication>
#include <QtCore>
#include <QThreadPool>
#include <QTimer>
#include "mandelbrot_widget.h"
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
MandelbrotWidget w;
w.resize(820, 640);
w.setWindowTitle(QStringLiteral("Day4 · Mandelbrot 分形渲染器(绘图 / 多线程 / 事件)"));
w.show();
if (app.platformName() == QStringLiteral("offscreen")) {
// 等所有在途 QRunnable 跑完,再让 widget 析构(任务持值副本,但仍可能
// 通过排队信号访问 this——这里确保它们落定)。
QObject::connect(&app, &QCoreApplication::aboutToQuit, []() {
QThreadPool::globalInstance()->waitForDone();
});
QTimer::singleShot(200, &app, &QApplication::quit);
}
return app.exec();
}