[Interest] QTimer system goes down?

Hugo Drumond Jacob hugo at jacob.eng.br
Wed Oct 3 15:44:56 CEST 2012


Hi folks!

A couple of days ago I had some problems with a scenario like that shown
below:

class SomeClassWorker : public QObject {
    Q_OBJECT

    public:
        SomeClassWorker(QObject* parent = 0) : QObject(parent) {
            connect(&someTimer, SIGNAL(timeout()), this,
SLOT(timeoutSlot()));
    someTimer.start(100); //Do something every 100msecs.
        }

    private slots:
        void timeoutSlot(); //Do something

    private:
        QTimer someTimer;

};

class SomeClass : public QObject {
    Q_OBJECT

    public:
        SomeClass(QObject* parent = 0) : QObject(parent) {

            worker = new SomeClassWorker(this);

            if(worker) {
                worker->moveToThread(&workerThread);
        workerThread.start();
            } else {
                qFatal("Can't instantiate worker.");
    }


        }

    private:
        QThread workerThread;
        SomeClassWorker* worker;

};


The SomeClass instance is running in main thread and the SomeClassWorker is
running in a different thread. Note that, on SomeClassWorker's constructor,
the "someTimer" is connected to a private slot "timeoutSlot()" with
Qt::AutoConnection connection type. On SomeClass's constructor, the
SomeClassWorker is moved to her thread. Note yet that someTimer is a
private member of SomeClassWorker and your parent is NULL (the default
QTimer's constructor is called).

>From Qt 4.8.0 documentation, the Qt::AutoConnection's description is:
"(default) If the signal is emitted from a different thread than the
receiving object, the signal is queued, behaving as Qt::QueuedConnection.
Otherwise, the slot is invoked directly, behaving as Qt::DirectConnection.
The type of connection is determined when the signal is emitted."

So, when SomeClassWorker is moved to her thread, the someTimer still in
main thread and for every "timeout()" signal emitted, the "timeoutSlot()"
is queued called.

The problem: for some reason, the Qt timer system goes down (is correct say
this, in this way?). The main thread still running, but, all
"QTimer::timeout()" events aren't delivered.

To "solve" that, I set the someTimer parent to "this", in SomeClassWorker's
constructor:
PS: Note the quotation marks at solve.

    SomeClassWorker(QObject* parent = 0) : QObject(parent), someTimer(this)
{

So, anyone can tell me what is wrong or what is happening?

Thanks!

Hugo Drumond Jacob
Eng. de Controle e Automação - CREA-MG 143908/LP
Mestrando em Modelagem Matemática e Computacional - CEFET/MG
hugo at jacob.eng.br | hugodj at gmail.com
+55(31)85244245
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20121003/306c4f52/attachment.html>


More information about the Interest mailing list