在QT创建者中停止QTIMER
stopping a qtimer in Qt creator
我在这样的主构造函数中设置了qtimer。
Ball::Ball(QGraphicsItem *parent): QGraphicsRectItem(parent), QObject(){
// draw rect
setRect(0,0,50,50);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::red);
setBrush(brush);
// move up right initially
xVelocity = 0;
yVelocity = -5;
QTimer* timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(move()));
timer->start(15);
}
定时执行计时器中的MOVE()fution。
void Ball::move(){
reverseVelocityIfOutOfBounds();
handlePaddleCollision();
handleBlockCollision();
moveBy(xVelocity,yVelocity);
}
在上述reversevelocityifoutofbounds();功能,有这样的条件。
// bottom edge - NONE (can fall through bottom)
if (mapToScene(rect().topRight()).y() >= screenH){
执行此操作后,我想停止QTimer。我应用了一切。但是该程序一直意外地关闭。请任何帮助都将受到高度应用。
您可以做的最简单的事情是使计时器成为球类的成员变量。然后,您有一个句柄,只需在reversevelocityIfoutofBounds()中调用Timer-> stop()。
您可以做的另一件事是调用sender()移动以获取计时器对象并将其传递给reversevelocityifoutofbounds(虽然不良样式),却不能跨线程。
相关文章:
- Qt VTK交互风格的信号到小部件
- QT在错误的班级中寻找空位
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 如何在Qt C++中更改光标
- 如何在MS Visual Studio 2019中运行QT UI
- QT通过C++添加映射QML项目
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 如何从其他类n个Qt C++调用QTimer?
- QT-执行QTimer :: timout()信号导致QEVENT
- 在QT创建者中停止QTIMER
- 设置QTimer间隔时QT应用程序崩溃
- Qt QTimer 以这种方式停止它是否安全
- 使用 QTimer 的 Qt 登录超时
- 如何在 QT 上暂停和重新启动 Qtimer
- 使用来自非Qt DLL(JNA)的QTimer
- Qt基元类型,与QTimer和C++比赛
- Make QTimer public. (Qt, C++)
- 使用Qt的QTimer函数在OpenGl中制作动画
- 暂停功能在Qt使用QTimer按钮按下