Results 1 to 6 of 6

Thread: QThread clarification

  1. #1
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QThread clarification

    Hi peeps.
    I'm creating a worker thread to keep my app responsive while accessing a database etc,
    and had a couple of questions.
    1. can i call slots in my worker thread from my main thread before i start the thread?
    and if so
    2. how do i stop this from happening?

    also why can't i create a pointer to my thread i.e.
    Qt Code:
    1. QThreadClass *thread;
    To copy to clipboard, switch view to plain text mode 
    I get a sigsev when i try to connect signals and slots, i have to pass a reference to the connect function i.e.
    Qt Code:
    1. QThreadClass thread;
    2. connect(&thread, SIGNAL(signal(arg)), this, SLOT(slot(arg)));
    To copy to clipboard, switch view to plain text mode 
    hope i'm not being too daft.....

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QThread clarification

    I recommend to use this kind of pattern:
    1. Create simple class inheriting from QObject which handles data base and emits signals and have some slots (lets call it DataBaseHandler).
    2. then create thread like that
      Qt Code:
      1. QThread *thread = new QThread(this);
      2. DataBaseHandler *database = new DataBaseHandler(thread);
      3. database->moveToThread(thread);
      4.  
      5. // example connections:
      6. connect(this, SIGNAL(buttonWasPressed()),
      7. database, SLOT(featchSomeData()));
      8.  
      9. connect(database, SIGNAL(dataReady(QString)),
      10. someLabel, SLOT(setText(QString)));
      11.  
      12. thread->start();
      To copy to clipboard, switch view to plain text mode 


    Qt will do all tricks required for such simple case.

  3. The following user says thank you to MarekR22 for this useful post:

    janorcutt (5th January 2011)

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

    Default Re: QThread clarification

    Quote Originally Posted by janorcutt View Post
    1. can i call slots in my worker thread from my main thread before i start the thread?
    You mean slots of the worker thread or slots of the worker thread object? As for the former - no, because the thread doesn't exist yet so no objects live in that thread. As for the latter - yes but the thread object is associated with the thread that created the object and not with the thread controlled by the object.


    also why can't i create a pointer to my thread i.e.
    Qt Code:
    1. QThreadClass *thread;
    To copy to clipboard, switch view to plain text mode 
    What do you mean you "can't"?

    i have to pass a reference to the connect function i.e.
    Qt Code:
    1. QThreadClass thread;
    2. connect(&thread, SIGNAL(signal(arg)), this, SLOT(slot(arg)));
    To copy to clipboard, switch view to plain text mode 
    It's not a reference, it's an address (aka temporary pointer).
    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.


  5. #4
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread clarification

    ok thanks for the info.
    as for the slots, i'm not going to worry about it too much as i only need to emit signals which i can already do, as long as i register the types e.g.
    Qt Code:
    1. qRegisterMetaType<object>();
    To copy to clipboard, switch view to plain text mode 

    as for the pointer, i should have clarified, i can create the pointer however my program sigsev'd at run time at the following code:
    Qt Code:
    1. connect(thread, SIGNAL(signal(int)), this, SLOT(slot(int)));
    To copy to clipboard, switch view to plain text mode 
    whereas the following worked
    Qt Code:
    1. // header
    2. QThreadClass thread;
    3.  
    4. //implimentation
    5. connect(&thread, SIGNAL(signal(arg)), this, SLOT(slot(arg)));
    To copy to clipboard, switch view to plain text mode 

    i'm going to have to read my c++ book again

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

    Default Re: QThread clarification

    Quote Originally Posted by janorcutt View Post
    as for the pointer, i should have clarified, i can create the pointer however my program sigsev'd at run time at the following code:
    Qt Code:
    1. connect(thread, SIGNAL(signal(int)), this, SLOT(slot(int)));
    To copy to clipboard, switch view to plain text mode 
    Because you created a pointer and not an object. It's like you had a parking space without a car in it. Try opening a door of an empty parking lot
    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.


  7. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QThread clarification

    Because you created a pointer and not an object.
    To clear possible confusion, just creating an object will not do either - you need the pointer to point (have an address) of a created object - this is called "initializing a pointer".

    If initializing pointers is something new to you, or how pointers work, I really suggest you don't jump straight in to threads, which is a complex issue on its own.
    First be comfortable with the language, then try more complex stuff.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Clarification on how to use Qt's debugging techniques
    By hackerNovitiate in forum Newbie
    Replies: 5
    Last Post: 21st June 2010, 04:08
  2. Replies: 0
    Last Post: 21st September 2009, 17:25
  3. Clarification about dataChanged
    By airbites in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2009, 16:02
  4. need some clarification on QImage and QRgb usage
    By kona in forum Qt Programming
    Replies: 5
    Last Post: 6th September 2008, 14:20
  5. Replies: 4
    Last Post: 26th June 2008, 18:41

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.