在Qt中,我想要设置一个 QTimer
,每秒调用一个名为“update”的函数。这是我的.cpp文件:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include "QDebug"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::update()
{
qDebug() << "update";
}
并且主要的代码:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
项目正在构建中,但它没有执行更新,因为"update"这一行在任何地方都没有显示...有人看出我做错了什么吗?
main
函数中调用了app.exec()
(或者你给QApplication
起的其他名字)? - tmpearcethis
添加到QTimer
构造函数中。 - cmannett85