Without seeing the full code I can't comment on what the problem might be.

However I quickly wrote a small test app which essentially does
whatever you've mentioned. It works fine on my Ubuntu setup with both
Qt5 (5.2.0) and Qt4 (4.8.6).

Have a look at the code and see if there's anything different.

HTH,
-mandeep


On Tue, May 13, 2014 at 4:54 PM, Sensei <sense...@gmail.com> wrote:
> Dear all,
>
> I am reporting this weird behavior because I believe it could be a
> potential bug: my code is too simple to have an unexpected behavior.
>
> Anyway *BEWARE*: I might be making a huge mistake in my code that I
> don't see! Please forgive me if I am wrong! :)
>
> So, here I go.
>
>
> I have a class that I move to a thread. Each X millisecond, I need to
> run a method, and this is done with a QThread. However, it wasn't
> working and I didn't know why. The slot wasn't called by the timeout()
> connection.
>
> After one day, I decided the only possible culprit could be the thread,
> and... AHA! It works!
>
> Now, can anyone spot my mistake? I assume it's my fault, and not Qt's
> since they saved me so many times! :)
>
>
> The class is created this way in my QMainWindow:
>
>          inThreadThread_ = new QThread(this);
>          inThread_ = new myClass();
>          inThread_->moveToThread(inThreadThread_);
>
>
>
>
> And the class is as follows:
>
>
>
> class myClass : public QObject
> {
>      // ...
> public slots:
>
>      void dummy() { qWarning("timer in thread"); };
>
>      // ...
>
> private:
>
>      QTimer *inThread_;
> }
>
>
> myClass::myClass() : QObject(NULL)
> {
>      // ...
>      inThread_ = new QTimer(this);
>      connect(inThread_, SIGNAL(timeout()), this, SLOT(dummy()));
>      inThread_->start(1000);
> }
>
>
>
> Note that if I comment the thread-related parts in the main window, and
> I just create a new object, the dummy slot is called without a hiccup.
> When uncommenting the new QThread and the moveToThread lines, no timed
> slot execution.
>
> For complete information, I am using Qt 4.8.6 (from Homebrew) on a MacOS
> X 10.9.2.
>
>
> What am I doing wrong in my code?
>
>
>
> Thanks!
>
>
>
>
> _______________________________________________
> Interest mailing list
> Interest@qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
#include <QCoreApplication>
#include <QThread>
#include <QTimer>
#include <QDebug>

class Worker : public QObject
{
    Q_OBJECT
public:
    Worker() : QObject(NULL)
    {
        startTimer();
    }

public slots:
    void onTimeout()
    {
        qDebug() << "Timout called";
    }

    void startTimer()
    {
        timer = new QTimer(this);
        connect(timer, SIGNAL(timeout()),
                this, SLOT(onTimeout()));
        timer->start(1000);
    }

private:
    QTimer *timer;
};

class Tester : public QObject
{
    Q_OBJECT
public:
    Tester(QObject *parent = 0) : QObject(parent) {}

public slots:
    void startTest()
    {
        thread = new QThread(this);
        Worker *worker = new Worker();
        worker->moveToThread(thread);
        thread->start();
    }

private:
    QThread* thread;
};

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

    Tester *t = new Tester;
    QTimer::singleShot(0, t, SLOT(startTest()));
    return a.exec();
}

#include "main.moc"

Attachment: threadtimertest.pro
Description: Binary data

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to