Qt access parent from page of stackedWidget created as separate class in QtDesigner

StackOverflow https://stackoverflow.com/questions/10823203

  •  11-06-2021
  •  | 
  •  

Question

Because of some bug in QtDesigner integrated into QtCreator 2.4.1 I created separated definition as a class inheriting from QWidget. This solve the problem with lying out widgets in Layouts on pages of QStackedWidget. Additionally this nicely separate complex UI.

However with this solution I fall into problem of main form and the page being separate classes. In the inner class I do not see elements (widgets, slots) of the main form/window, while the logic requires operations there (enabling/disabling some buttons, binding popups, etc.)

I am looking for a good pattern how such problem should be solved. I tried follow some advices found on Google with accessing through parent pointer, however I failed and crashed the application :-) This is not what I am looking for.

Was it helpful?

Solution

Typical Qt-way of solving such a problem is done via signals.

In your case you can emit signals with some data from page, while main form has an associated function(-s) connected to the object signals. Something like:

Definition

class page : public QObject
{
     Q_OBJECT

...

    signals:
        void my_custom_signal(int value);
};

Implementation

void page::function()
{
    // some code

    emit my_custom_signal(FORM_SHOW_ABOUT_DIALOG);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top