0

I have a class, a subclass of QObject. I am creating an instance for that class inside a QThread. I want to use QTimer in my class. But the timer is not starting because the caller thread is a woker thread. How can i achieve the solution?

Dev
  • 127
  • 2
  • 8
  • There is a similar and answered question on Stackoverflow: http://stackoverflow.com/questions/10492480/starting-qtimer-in-a-qthread – AquilaRapax Oct 10 '12 at 07:03
  • Tats for inside the QThread. But i want to use inside a class which is subclass of qobject and the instance of my qobject class is created in qthread. since the caller of my class is qthread, i couldn't able to use qtimer inside my class. – Dev Oct 10 '12 at 07:12
  • Show code or it didn't happen! – Kamil Klimek Oct 10 '12 at 08:27
  • caller thread is also a thread subclass of QThread. – Dev Oct 10 '12 at 09:18

1 Answers1

0

Well, can you make a slot in some helper object that lives in main thread and is a member of the class and dispatch the timer creation and start to it from your class (and optionally check if the current thread is not the event loop since then it's safe to start the timer there) ? So then when you need to create the timer you would just fire a signal to your helper object in main thread and it should work.

Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • Well Thanks. I will try this. And is there any possibility to post event from my class?? so that cal can go to main thread and event will be dispatched from loop right ?? – Dev Oct 10 '12 at 08:54
  • I think that should be fine, since that's the whole idea with slots/signals that they span across threads. But I think you need to remember to do moveToThread(
    ) on the helper memeber object if your class is created in a thread that is not main since then you won't get what you need and the slot will be called in the thread where your class instance was created.
    – Rudolfs Bundulis Oct 10 '12 at 09:24
  • Thanks! I tried with moveToThread() and it fulfilled my expectation. – Dev Oct 12 '12 at 04:37