Results 1 to 7 of 7

Thread: QThread and QTimer

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread and QTimer

    Quote Originally Posted by bibhukalyana View Post
    What i have mentioned here is it true ?
    Yes.

    Is the member function of testClass belongs to testClass thread or not ?
    Functions do not belong to threads. Only objects do in the sense that a thread processes events of an object.

    I am using signal slot to communicate between two thread .But the current thread id of mainwindow and startWork() slot are same.
    That's because the QThread instance lives in the main thread so its slots are executed in the context of the main thread.

    How i solve this?
    Instead of subclassing QThread, create an instance of it, wrap the functionality you want into a QObject instance and move that object into the thread.


    Qt Code:
    1. t.start();
    2. Object *o = new Object;
    3. o->moveToThread(&t);
    4. connect(mainWin, SIGNAL(someSignal()), o, SLOT(doSomeWork()));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QTimer

    Thanks for reply.

    I tried like :

    Qt Code:
    1. testClass *tCls = new testClass();
    2.  
    3. t.start();
    4. tCls->moveToThread(&t);
    5.  
    6. QObject::connect(&w,SIGNAL(startSignal()),tCls,SLOT(startWork()),Qt::QueuedConnection);
    7. w.show();
    To copy to clipboard, switch view to plain text mode 

    I am getting segmentation fault.
    Or will I take one more class and inherits QThread and after that execute movetoThread.

    One more thing. It is working but I dont know this is corret or not .

    Qt Code:
    1. tCls->moveToThread(tCls);
    2. tCls->start(QThread::NormalPriority);
    To copy to clipboard, switch view to plain text mode 

    thanks.
    Last edited by wysota; 23rd September 2013 at 14:22.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread and QTimer

    testClass should inherit QObject and not QThread, that's for sure. If your program crashes then find the crash.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread and QTimer

    thanks it is my mistake.

    Can you please tell me how to assing memory to timer so that it will belong to testClass thread ?
    If after moveToThread i tried to call directly using object, but i get the same error.
    But current thread id of startWork() and mainclass are different (thanks.. It is working).

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QThread and QTimer

    You can never call the timer directly if you want it to be part of an object living in a different thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. When should i use QThread and QTimer
    By NathiyaAmose in forum Qt Programming
    Replies: 4
    Last Post: 5th August 2013, 11:02
  2. Replies: 15
    Last Post: 4th August 2012, 19:11
  3. QTimer within QThread
    By Eos Pengwern in forum Qt Programming
    Replies: 6
    Last Post: 23rd February 2011, 20:00
  4. QThread and QTimer
    By sivrisinek in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2009, 16:41
  5. QThread & QTimer
    By hosseinyounesi in forum Qt Programming
    Replies: 5
    Last Post: 13th April 2009, 08:22

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.