Scorpion
Scorpion

Reputation: 59

how to use QTimer without a QT application

i want to use the QTimer in my regular class (no QT Application but derived from QTimer). but when i try this:

Header:

#include <qtimer.h>
QTimer* m_timer;
public slots:
    void UpdateClock();

Source:

    m_timer = new QTimer(this);
    QObject::connect(m_timer, SIGNAL(timeout()), this, SLOT(UpdateClock()));
    m_timer->start(1000);

void MyClass::UpdateClock()
{
    int i = 0;
}

the timer never jumps into the UpdateClock method! Do you know why and how i solve that problem??

Thanks!

Upvotes: 1

Views: 2468

Answers (2)

Kirell
Kirell

Reputation: 9828

If you want to use signals and slots in Qt, you need an event loop to process the signals and dispatch them.

Usually, in main.cpp you have something like:

int main( int argc, char** argv )
{
    QApplication app(argc, argv);
    ...
    return app.exec(); // the event loop is started and runs here
}

If there is no need for a GUI, you can use

QCoreApplication

You can also create your own event loop using:

QEventLoop

http://qt-project.org/doc/qt-4.8/qeventloop.html and process only the QTimer event. However, you will still need to create a QApplication.

Upvotes: 3

arnt
arnt

Reputation: 9685

QTimer depends on QCoreApplication. If you don't start a QCoreApplication, nothing will activate the QTimer. (QApplication inherits QCoreApplication and is usually used.)

Upvotes: 5

Related Questions