[英]QTimer doesn't trigger
I'm trying to get the QTimer
running, but it never triggers. 我正在尝试使QTimer
运行,但它从未触发。 I found some other questions about the timer, but the problem was always the timer being out of scope. 我发现了有关计时器的其他问题,但问题始终是计时器超出范围。 This is not the case in my small example: 在我的小示例中,情况并非如此:
I create the timer in a custom QMainWindow
, this is the .h file: 我在自定义QMainWindow
创建计时器,这是.h文件:
#include <iostream>
#include <QtWidgets/QMainWindow>
#include <QTimer>
class MyMainWindow : public QMainWindow {
Q_OBJECT;
private:
QTimer *mainTimer;
public slots:
void timerUpdate();
public:
MyMainWindow();
};
This is the .cpp file: 这是.cpp文件:
#include "MyMainWindow.h"
MyMainWindow::MyMainWindow() {
QMainWindow();
mainTimer = new QTimer(this);
connect(mainTimer, SIGNAL(timeout()), this, SLOT(update()));
mainTimer->start(1000);
std::cout << "Timer created.\n";
}
void MyMainWindow::timerUpdate() {
std::cout << "test";
}
Finally, this is my main.cpp: 最后,这是我的main.cpp:
#include <QtWidgets/QApplication>
#include "MyMainWindow.h"
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
MyMainWindow mmw;
mmw.show();
return app.exec();
}
When I execute this code, I only get "Timer created." 执行此代码时,只会得到“创建计时器”的信息。 and never "test". 永远不要“测试”。
Any suggestions? 有什么建议么?
You're connecting to SLOT(update())
, but your function is called timerUpdate
. 您正在连接到SLOT(update())
,但您的函数称为timerUpdate
。
Using more modern Qt 5 signal-slot connection syntax , that would never have happened and you'd get an error at compile-time. 使用更现代的Qt 5信号插槽连接语法 ,这将永远不会发生,并且在编译时会出错。 You should prefer using that. 您应该更喜欢使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.