Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Access variables from MainWindow in separate cpp file
Forum Updated to NodeBB v4.3 + New Features

Access variables from MainWindow in separate cpp file

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    t0msk
    wrote on last edited by t0msk
    #1

    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

    Student who loves C/C++

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • T Offline
        T Offline
        t0msk
        wrote on last edited by
        #3

        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
        

        ?

        Student who loves C/C++

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            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 :)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved