Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qtimer object blocking an application.



  • Hello,

    Im using a Qtimer object in order to create a repetitive event in a application. However this object is always running and blocks the application.
    Could you help me?

    Many thanks

    Ricardo Sousa

    Note: the Qtimer object is not in a different thread, but I suppose that it should work in this way.



  • Oh. QTimer is asynchronous - how it can block your application? OK. I will show you example:

    mainwindow.h
    @
    //...
    #include <QObject>
    #include <QTimer>
    #include <QDebug>

    class MainWindow : public QObject {
    Q_OBJECT
    public:
    MainWindow(QObject* parent = 0);
    public slots:
    void mySlot();
    private:
    QTimer * timer;
    }
    //...
    @

    mainwindow.cpp
    @
    MainWindow::MainWindow(QObject *parent = 0) :
    timer(new QTimer)
    {
    timer->setInterval(1000); // one second
    connect(timer, SIGNAL(timeout()), this, SLOT(mySlot()));
    timer->start();
    }

    void MainWindow::mySlot() // will be executed every second
    {
    qDebug() << "here it is";
    }
    @

    This code won't block application - it's asynchronous!



  • Please post your code



  • Is it possible that your timeout value is too low (for the amount of work that has to be done in the connected slots) so the timer runs out consecutively and thus your applications thrashes and blocks?



  • [quote author="Lukas Geyer" date="1339491711"]Is it possible that your timeout value is too low (for the amount of work that has to be done in the connected slots) so the timer runs out consecutively and thus your applications thrashes and blocks?[/quote]

    Oh. Yes. You may review your interval and slots, which connected with timer. It can be very small amount of time for hard work in slots, so they can be running all the time (without interval).


Log in to reply