Passing messages between objects - alternative to "connect" ?
-
This is kinda of followup on another thread ....
I am looking for an alternative to pass messages between objects.
How is this for an idea ?
Build / create QT "standard " QT Designer Form Class - let's call it "parent class " - for this discussion (only).
Build / create QT "standard " QT Designer Form Class - let's call it "child class " - for this discussion (only).
Add the "child class " as a member of the "parent class".Both classes have standard Qt form feature, each one accessible from corresponding class.
Is there a reasonably straight forward way to enable "parent" to access the child form ?
Ideally the changes in "child " form needs to be passed onto the "parent", and it would not be necessary to actually show the "child" form.
PS I did try "inheritance" but there in no easy way to inherit /add "form class " to primary form class...
.
-
This is kinda of followup on another thread ....
I am looking for an alternative to pass messages between objects.
How is this for an idea ?
Build / create QT "standard " QT Designer Form Class - let's call it "parent class " - for this discussion (only).
Build / create QT "standard " QT Designer Form Class - let's call it "child class " - for this discussion (only).
Add the "child class " as a member of the "parent class".Both classes have standard Qt form feature, each one accessible from corresponding class.
Is there a reasonably straight forward way to enable "parent" to access the child form ?
Ideally the changes in "child " form needs to be passed onto the "parent", and it would not be necessary to actually show the "child" form.
PS I did try "inheritance" but there in no easy way to inherit /add "form class " to primary form class...
.
@AnneRanch said in Passing messages between objects - alternative to "connect" ?:
I am looking for an alternative to pass messages between objects.
You can use custom event,
Define your own events and send them with:
[static] bool QCoreApplication::sendEvent(QObject *receiver, QEvent *event)Sends event event directly to receiver receiver, using the notify() function. Returns the value that was returned from the event handler.
The event is not deleted when the event has been sent. The normal approach is to create the event on the stack, for example:
QMouseEvent event(QEvent::MouseButtonPress, pos, 0, 0, 0);
QApplication::sendEvent(mainWindow, &event);@AnneRanch said in Passing messages between objects - alternative to "connect" ?:
Is there a reasonably straight forward way to enable "parent" to access the child form ?
Yes, include the ui_childName.h and in child class add a get method:
UI::ChildName* childUi() { return ui; }
some will say it's bad practice bla bla, it's up to you to decide :)The other way is to retreive each object by name:
child->findChild<SomeClass*>("objectName")Of course, the "normal/right way" is to define getters for all the objects you need,
but it is borring I know ;) -
the answer the question based on post title, there are many possibilities to have "objects" communicate with each other but why reinvent the wheel? anything custom will need to be rigorously proven, especially with regard to object syncronization. the signal/slot mechanism already takes care of the heavy lifting. The only time I'd entertain a custom object communications channel would be when IPC (interprocess communications) is necessary, or processes/objects running in their own address space...and then I'd try to choose a high level API that already exists: message queues, fifos, sockets, etc.