Reputation: 8367
Im building an application where a few objets outside the mainWindow will need to talk to be connected to the ui elements, just simple on click events triggering member functions. I was wondering whether to Add slots to the main window that connect to each ui element so that I only ever need to interface my external objects with one ui object, the main window, or whether I should directly connect my objects signals to the individual ui elements slots.
Also could someone share an example of how to connect a signal from an external object into the mainWindow, I'm very new to this and until now I've always added all my code to the mainWindow to keep things simple but i know thats bad practice so i think its time to move on.
Thanks all
j
Upvotes: 2
Views: 889
Reputation: 206729
There is no difference in syntax between connecting an "external" object or an "internal" one to a slot. The connect
call takes two object pointer (from/to) and doesn't really care about where they are (except if you're using threads).
The Qt Signals and slots documentation has all you need to know about these connections, and the examples show real code that uses signals. (The analog clock for instance uses a QTimer which is "external" to its main window.)
There is no one-size-fits-all answer to the "where should I put my public slots" question. Do what is easier to maintain/more logical for your particular scenario. Having all your "external" signals routed to a single object that dispatches them is "clean" from an interface (API) point of view, but it means you'll need to maintain more code than if you routed the signals directly to the object that needs to react on them.
Upvotes: 2