PDA

View Full Version : Best way to expose a thread in 'child' classes?



ngoonee
2nd December 2014, 09:51
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/60968-StackedWidget-single-window-UI-how-to-separate-code-for-readability

anda_skoa
2nd December 2014, 10:48
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,
_

ngoonee
4th December 2014, 03:58
Doesn't passing a QThread create a (separate) copy though? Or passing by (const) reference wouldn't do that?

anda_skoa
4th December 2014, 07:23
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,
_