[Interest] BUG? QTimer in QThread

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Tue May 13 13:48:25 CEST 2014


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 <senseiwa at 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 at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp
Type: text/x-c++src
Size: 1019 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140513/7032487b/attachment.cpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: threadtimertest.pro
Type: application/octet-stream
Size: 299 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140513/7032487b/attachment.obj>


More information about the Interest mailing list