如何使用QTimer

33

在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"这一行在任何地方都没有显示...有人看出我做错了什么吗?


1
你是否在 main 函数中调用了 app.exec()(或者你给 QApplication 起的其他名字)? - tmpearce
2
你还在创建内存泄漏,将this添加到QTimer构造函数中。 - cmannett85
我添加了我的主要代码。将其添加到构造函数中并没有解决问题。 - Frank
1
@cmannett85,如果存在任何内存泄漏问题,MainWindow将拥有计时器,并且当MainWindow对象被删除时,它将在其析构函数中删除计时器。 - kato2
3个回答

26

另一种方法是使用内置的start timer和event TimerEvent方法。

标题:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    int timerId;

protected:
    void timerEvent(QTimerEvent *event);
};

#endif // MAINWINDOW_H

来源:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    timerId = startTimer(1000);
}

MainWindow::~MainWindow()
{
    killTimer(timerId);
    delete ui;
}

void MainWindow::timerEvent(QTimerEvent *event)
{
    qDebug() << "Update...";
}

1
我尝试了各种方法让QTimer工作,但都没有成功。但是这个方法立刻就有效了,谢谢! - Lucky Luke
1
这正是我现在已经寻找了几个小时的东西。瞬间成功。谢谢。 - Eiconic

17
  1. 使用Qt的内存管理系统,将父对象与QTimer关联是一种良好的实践。

  2. update()QWidget函数 - 这是否是您要调用的函数?请参考http://qt-project.org/doc/qt-4.8/qwidget.html#update

  3. 如果第2条不适用,请确保您尝试触发的函数已在头文件中声明为槽函数。

  4. 最后,如果以上都不是问题,请确认是否出现任何运行时连接错误,这将有助于解决问题。


1
修复了。显然,update()函数不是最好的选择...当我更改它时,问题就解决了。 - Frank
update() 被重载了多次,但“原始”的签名不带参数(请在您链接的页面上稍微向上滚动)。请修改您的答案。 - cmannett85
“update” 插槽名称对我来说运行良好。也许该函数没有声明为插槽? - Pavel Strakhov

8

mytimer.h:

    #ifndef MYTIMER_H
    #define MYTIMER_H

    #include <QTimer>

    class MyTimer : public QObject
    {
        Q_OBJECT
    public:
        MyTimer();
        QTimer *timer;

    public slots:
        void MyTimerSlot();
    };

    #endif // MYTIME

mytimer.cpp:

#include "mytimer.h"
#include <QDebug>

MyTimer::MyTimer()
{
    // create a timer
    timer = new QTimer(this);

    // setup signal and slot
    connect(timer, SIGNAL(timeout()),
          this, SLOT(MyTimerSlot()));

    // msec
    timer->start(1000);
}

void MyTimer::MyTimerSlot()
{
    qDebug() << "Timer...";
}

main.cpp:

#include <QCoreApplication>
#include "mytimer.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // Create MyTimer instance
    // QTimer object will be created in the MyTimer constructor
    MyTimer timer;

    return a.exec();
}

如果我们运行以下代码:
Timer...
Timer...
Timer...
Timer...
Timer...
...

资源


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接