Access variables from MainWindow in separate cpp file

  • Hello,

    What I want to achieve? My program contains QStackedWidget inside MainWindow, so now all logic is in MainWindow, but I would like to separate a logic for every page in QStackedWidget to separate cpp, because of "cleaner code". But I would like to have access to some objects in MainWindow like ui and some variables (because I want change ui and variables from separate file).

    What is the best way doing this?

    My idea is that I will create a objects (from cpp) of every page inside MainWindow and pass a pointers of objects and variables, something like:

    Separate_code1* page1 = new Separate_code1(&ui, &var1, &var2); // code for 1st page of QStackedWidget
    Separate_code2* page2 = new Separate_code2(&ui, &var1, &var2); // code for 2nd page of QStackedWidget 
    Separate_code3* page3 = new Separate_code3(&ui, &var1, &var2); // code for 3rd page of QStackedWidget

    Is this good aproach, or is something better?

    Thank you

  • Lifetime Qt Champion


    While your idea is sane, your implementation indentation are less good. You should not try to give access to your MainWindow's internals to your "pages", that creates tight coupling which is bad for several reasons including maintenance. You should rather create a proper interface for them that you will wire to your MainWindow.

  • Interfaces? Could I get some example?

    Do you mean that MainWindow class will contain

    virtual void some_MainWindow_Function() = 0;

    and separate cpp will contain

    public MainWindow


  • Lifetime Qt Champion

    No, I mean define what interaction you would like to have between your pages and your MainWindow and then define the matching interface so you can build your pages based on that.

  • Lifetime Qt Champion

    Just a note on interfaces. If you google it, there are many kinds.
    In your context/this post
    Think of it as defined way of getting data.

    Instead of letting the Pages know all sorts of internal details, simply define some function to return
    the needed data to them, without they knowing how its stored or what widgets are involved.

    Say mainwindow have a textEdit called UserName

    Its temping from some other window to do try something like
    QString user = mainwin->ui->UserName.text();
    to read it directly off the widget.

    That is not optimal as it is a sure way to a fragile program.
    Say you change the textedit to a combobox to allow to select some predefined user.
    Then suddenly the code in other windows needs to be change to take the text from
    a combobox.

    However, if you define a new function in mainwindow
    QString GetUserName();

    and only does
    QString user = mainwin_ptr->GetUserName();

    you have defined an interface to get that piece of info.
    You are then free to change anything in mainwindow without affecting
    other places in the program.

    Hope it make sense :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.