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. What's the best approach to update UI from another class?
Forum Updated to NodeBB v4.3 + New Features

What's the best approach to update UI from another class?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 475 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.
  • A Offline
    A Offline
    aiphae
    wrote on last edited by aiphae
    #1

    I know that this question is very old but I cannot figure out how to deal with my problem.

    I have a QStackedWidget with two widgets inside. Currently the code for both of them is contained in mainwindow.cpp and it is huge ... What I want to do is separate it into two files - one for each subwidget in QStackedWidget. What's the best way to do it considering each widget accesses the UI and the whole UI is set up in ui->setupUi(this)?

    1 Reply Last reply
    0
    • A aiphae

      I don't completely understand. I have two designed widgets in the QStackedWidget. What to to with them to handle them separately in the mainwindow?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @aiphae said in What's the best approach to update UI from another class?:

      I have two designed widgets in the QStackedWidget

      Move them out to two ui files, instantiate them in the main window and add them to your stack widget programatically.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Don't put those two widgets into the stacked widget in the designer but handle them separately and instantiate those instances in the ctor of your mainwindow and add them to the stack widget.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aiphae
          wrote on last edited by
          #3

          And what about the UI of these widgets? Do I have to write it manually? The interface is very complicated. Could you please provide some code example?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            You can create the ui in designer as normal.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • A Offline
              A Offline
              aiphae
              wrote on last edited by
              #5

              I don't completely understand. I have two designed widgets in the QStackedWidget. What to to with them to handle them separately in the mainwindow?

              Christian EhrlicherC Pl45m4P 2 Replies Last reply
              0
              • A aiphae

                I don't completely understand. I have two designed widgets in the QStackedWidget. What to to with them to handle them separately in the mainwindow?

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @aiphae said in What's the best approach to update UI from another class?:

                I have two designed widgets in the QStackedWidget

                Move them out to two ui files, instantiate them in the main window and add them to your stack widget programatically.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                1
                • A aiphae

                  I don't completely understand. I have two designed widgets in the QStackedWidget. What to to with them to handle them separately in the mainwindow?

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by
                  #7

                  @aiphae

                  do as @Christian-Ehrlicher said and then you can communicate with other widgets using signal connections when needed


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    aiphae
                    wrote on last edited by aiphae
                    #8

                    Okay so I did as you said but I cannot interact with my widget ...

                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                        , ui(new Ui::MainWindow)
                    {
                        ui->setupUi(this);
                    
                        connectDb();
                    
                        auto emailsPage = new EmailsWidget(this);
                        ui->stackedWidget->addWidget(emailsPage);
                        emailsPage->show();
                    }
                    
                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • A aiphae

                      Okay so I did as you said but I cannot interact with my widget ...

                      MainWindow::MainWindow(QWidget *parent)
                          : QMainWindow(parent)
                          , ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                      
                          connectDb();
                      
                          auto emailsPage = new EmailsWidget(this);
                          ui->stackedWidget->addWidget(emailsPage);
                          emailsPage->show();
                      }
                      
                      Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @aiphae said in What's the best approach to update UI from another class?:

                      but I cannot interact with my widget ...

                      As @Pl45m4 already wrote - use signals and slots to pass data between the mainwindow and the subwindow-

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        aiphae
                        wrote on last edited by
                        #10

                        I meant that I cannot click anything. But I got it fixed:

                        auto emailsPage = new EmailsWidget(this);
                        ui->stackedWidget->insertWidget(0, emailsPage);
                        ui->stackedWidget->setCurrentIndex(0);
                        

                        Now everything works. Thank you!

                        1 Reply Last reply
                        0
                        • A aiphae has marked this topic as solved on

                        • Login

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