Witht QtCreator set up as default when you create a widget the class for it creates the form as private to the class. So you have for example something like this:
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
//...
private:
Ui::MainWindow *ui;
};
Now what I want to know is how you connect to the signals within the ui. For example if this widget was embedded into a QStackedWidget and when a button is pressed the page displayed needs to change. I would have considered connecting to the button but ui is private so I cannot.
Do I have to create signals in MainWindow and then within that connect the ‘ui’ signals to them and thus bubble up the hierarchy? Or have I missed something simple?
You can’t have an unrelated object connect to signals/slots of aggregate components, even if you could it would break encapsulation and become a maintenance nightmare.
You need to expose the signals/slots of the aggregate components by adding them to the
MainWindowAPI, and then call the relativeuicomponent method in the definition.For example, in the
MainWindowdefinition, add:And then in constructor, do:
This way, your
MainWindowclass propagates signals from it’s aggregates – but finetuned to exactly how you intend the class to be used.