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 等)本就 执行完即退出,不受影响。
42 lines
1.7 KiB
C++
42 lines
1.7 KiB
C++
// ============================================================================
|
|
// qt_property_animation —— 教学补充示例(非 wiki 生成,手工维护)
|
|
// 用途:方案书「C++方向 Day4」要求的 QPropertyAnimation 动画框架,仓库 p03/
|
|
// 原有 wiki 抓取内容未覆盖这一点,故补一个最小示例。
|
|
// 配套文档:docs/teaching/examples/property_animation/README.md
|
|
// 离屏验证:QT_QPA_PLATFORM=offscreen tools/run_qt.sh qt_property_animation(自动退出)
|
|
// ============================================================================
|
|
|
|
#include <QApplication>
|
|
#include <QWidget>
|
|
#include <QPushButton>
|
|
#include <QPropertyAnimation>
|
|
#include <QTimer>
|
|
|
|
int main(int argc, char* argv[]) {
|
|
QApplication app(argc, argv);
|
|
|
|
QWidget window;
|
|
window.resize(320, 200);
|
|
|
|
auto* button = new QPushButton(QStringLiteral("看我移动"), &window);
|
|
button->setGeometry(10, 10, 100, 30);
|
|
|
|
// QPropertyAnimation 直接驱动 Qt 属性系统里的 geometry 属性,
|
|
// 不需要手写定时器 + 逐帧计算坐标。
|
|
auto* anim = new QPropertyAnimation(button, "geometry", &window);
|
|
anim->setDuration(800);
|
|
anim->setStartValue(QRect(10, 10, 100, 30));
|
|
anim->setEndValue(QRect(180, 140, 120, 40));
|
|
anim->setEasingCurve(QEasingCurve::OutBounce);
|
|
|
|
anim->start(QAbstractAnimation::DeleteWhenStopped);
|
|
|
|
window.show();
|
|
// 仅在离屏自动化验证时(QT_QPA_PLATFORM=offscreen)自动退出:动画时长 800ms,
|
|
// 额外等 300ms 确保播放完再退出;正常运行窗口保持打开,不自动退出。
|
|
if (qgetenv("QT_QPA_PLATFORM") == "offscreen") {
|
|
QTimer::singleShot(1100, &app, &QApplication::quit);
|
|
}
|
|
return app.exec();
|
|
}
|