Results 1 to 4 of 4

Thread: Best way to expose a thread in 'child' classes?

  1. #1
    Join Date
    Nov 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Best way to expose a thread in 'child' classes?

    Reference - [1]

    Very basically, I have a 'base' class (and form) containing 'page1', 'page2', and 'page3' classes (and forms). Each page is a different subclass of QWidget, and 'base' is a subclass of QMainWindow which has a StackedWidget. I add 'page1', 'page2', and 'page3' to the StackedWidget in the constructor of 'base'.

    The constructor also starts two threads (QThread subclasses with 'run' reimplemented). What is the best way for me to allow the 'page1', 'page2', and/or 'page3' classes access to these threads? One thread (I'll call it 'robot') maintains communication with a robot, and has a few callable methods for obtaining information or performing actions (the actual work is done in the thread). It can be called easily from 'base', but not from 'page1', 'page2', or 'page3'.

    Signals and slots would be a workaround but they'd all have to be defined in base, which would get very unweildy very fast, even if I use signalmapper etc. etc.

    [1] - http://www.qtcentre.org/threads/6096...or-readability

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Best way to expose a thread in 'child' classes?

    Well, this is C++.
    If you want your code in "page1" to directly call a method on an object, pass that object to "page1"

    Cheers,
    _

  3. #3
    Join Date
    Nov 2014
    Posts
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Best way to expose a thread in 'child' classes?

    Doesn't passing a QThread create a (separate) copy though? Or passing by (const) reference wouldn't do that?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Best way to expose a thread in 'child' classes?

    QThread is a QObject derived class, you can't create copies of it.

    You can either pass it as a reference in the constructor, as a pointer or wrapped in a smart pointer.

    Cheers,
    _

Similar Threads

  1. Replies: 5
    Last Post: 26th May 2014, 08:13
  2. Replies: 1
    Last Post: 24th June 2011, 00:09
  3. Replies: 0
    Last Post: 22nd February 2011, 08:55
  4. Replies: 1
    Last Post: 16th September 2010, 16:57
  5. QT thread classes quidance requested
    By bajarangi in forum Qt Programming
    Replies: 0
    Last Post: 26th May 2009, 14:56

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.