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. Switch screens (.ui) on same window without opening new window with C++
QtWS25 Last Chance

Switch screens (.ui) on same window without opening new window with C++

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.5k Views
  • 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
    aabdn
    wrote on last edited by
    #1

    How to open multiple UI screen on same window with pushbutton click (without opening new window)? when I click button on first.ui, it should bring second.ui and when I click pushbutton on second.ui, it shoud bring first.ui, wthout opening new window. I have found information about doing this on PyQt5 but I want to do it in c++, can someone help me in this regards, also please share a sample code as I can know, I am beginner, i might need example code to understand.
    Thanks!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      Syntax aside, this is the same in C++ or Python.

      The cleanest method is to put the two widgets, one for each Designer form, in a QStackedWidget and switch between them with the QPushbuttons (or whatever other mechanism you like). Alternatively use a QTabWidget to contain the two forms.

      A 1 Reply Last reply
      2
      • C ChrisW67

        Syntax aside, this is the same in C++ or Python.

        The cleanest method is to put the two widgets, one for each Designer form, in a QStackedWidget and switch between them with the QPushbuttons (or whatever other mechanism you like). Alternatively use a QTabWidget to contain the two forms.

        A Offline
        A Offline
        aabdn
        wrote on last edited by
        #3

        @ChrisW67 Can you please share any example code just as reference, I got your meaning what i need to do, if there will be some example code, it will help me alot as i am beginner in Qt.

        jsulmJ mrjjM 2 Replies Last reply
        0
        • A aabdn

          @ChrisW67 Can you please share any example code just as reference, I got your meaning what i need to do, if there will be some example code, it will help me alot as i am beginner in Qt.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @aabdn There is already example code in the documentation @ChrisW67 gave you

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • A aabdn

            @ChrisW67 Can you please share any example code just as reference, I got your meaning what i need to do, if there will be some example code, it will help me alot as i am beginner in Qt.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @aabdn

            Hi
            You just place the QStackedWidger on the main window.
            Then right-click and use "Add page"

            Then on the buttons where you want to switch between the pages,
            you call
            ui->stackedWidget->setCurrentIndex(1);
            with index 0 or 1 to switch between the pages.

            And that is it :)

            A 1 Reply Last reply
            2
            • mrjjM mrjj

              @aabdn

              Hi
              You just place the QStackedWidger on the main window.
              Then right-click and use "Add page"

              Then on the buttons where you want to switch between the pages,
              you call
              ui->stackedWidget->setCurrentIndex(1);
              with index 0 or 1 to switch between the pages.

              And that is it :)

              A Offline
              A Offline
              aabdn
              wrote on last edited by
              #6

              @mrjj this is already done by me, I am sharing a link, I want to switch my first.ui and second.ui in this way with pushbutton, both should be displayed on same window.
              https://www.youtube.com/watch?v=82v2ZR-g6wY&t=585s

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #7

                Complete example with dummy page widgets. This, I think, is a better example than the Python in the video.

                #include <QApplication>
                #include <QWidget>
                #include <QVBoxLayout>
                #include <QStackedWidget>
                #include <QPushButton>
                #include <QLabel>
                
                class Page1: public QLabel
                {
                    Q_OBJECT
                public:
                    Page1(QWidget *p = nullptr):
                        QLabel(p)
                    {
                        setText("Screen 1");
                    }
                };
                
                class Page2: public QLabel
                {
                    Q_OBJECT
                public:
                    Page2(QWidget *p = nullptr):
                        QLabel(p)
                    {
                        setText("Screen 2");
                    }
                };
                
                class MainWindow: public QWidget {
                    Q_OBJECT
                public:
                    MainWindow(QWidget *p = nullptr):
                        QWidget(p)
                    {
                        QVBoxLayout *layout = new QVBoxLayout(this);
                
                        m_stack = new QStackedWidget(this);
                        layout->addWidget(m_stack);
                
                        QWidget *page1 = new Page1(m_stack);
                        m_stack->addWidget(page1);
                
                        QWidget *page2 = new Page2(m_stack);
                        m_stack->addWidget(page2);
                
                        QPushButton *button = new QPushButton("Switch page", this);
                        layout->addWidget(button);
                
                        connect(button, &QPushButton::clicked, this, &MainWindow::switchPage);
                    }
                public slots:
                    void switchPage()
                    {
                        if (m_stack->currentIndex() == 0)
                            m_stack->setCurrentIndex(1);
                        else
                            m_stack->setCurrentIndex(0);
                    }
                private:
                    QStackedWidget *m_stack;
                };
                
                int main(int argc, char **argv) {
                	QApplication app(argc, argv);
                
                    MainWindow mainWindow;
                    mainWindow.show();
                    return app.exec();
                }
                #include "main.moc"
                
                A 1 Reply Last reply
                2
                • C ChrisW67

                  Complete example with dummy page widgets. This, I think, is a better example than the Python in the video.

                  #include <QApplication>
                  #include <QWidget>
                  #include <QVBoxLayout>
                  #include <QStackedWidget>
                  #include <QPushButton>
                  #include <QLabel>
                  
                  class Page1: public QLabel
                  {
                      Q_OBJECT
                  public:
                      Page1(QWidget *p = nullptr):
                          QLabel(p)
                      {
                          setText("Screen 1");
                      }
                  };
                  
                  class Page2: public QLabel
                  {
                      Q_OBJECT
                  public:
                      Page2(QWidget *p = nullptr):
                          QLabel(p)
                      {
                          setText("Screen 2");
                      }
                  };
                  
                  class MainWindow: public QWidget {
                      Q_OBJECT
                  public:
                      MainWindow(QWidget *p = nullptr):
                          QWidget(p)
                      {
                          QVBoxLayout *layout = new QVBoxLayout(this);
                  
                          m_stack = new QStackedWidget(this);
                          layout->addWidget(m_stack);
                  
                          QWidget *page1 = new Page1(m_stack);
                          m_stack->addWidget(page1);
                  
                          QWidget *page2 = new Page2(m_stack);
                          m_stack->addWidget(page2);
                  
                          QPushButton *button = new QPushButton("Switch page", this);
                          layout->addWidget(button);
                  
                          connect(button, &QPushButton::clicked, this, &MainWindow::switchPage);
                      }
                  public slots:
                      void switchPage()
                      {
                          if (m_stack->currentIndex() == 0)
                              m_stack->setCurrentIndex(1);
                          else
                              m_stack->setCurrentIndex(0);
                      }
                  private:
                      QStackedWidget *m_stack;
                  };
                  
                  int main(int argc, char **argv) {
                  	QApplication app(argc, argv);
                  
                      MainWindow mainWindow;
                      mainWindow.show();
                      return app.exec();
                  }
                  #include "main.moc"
                  
                  A Offline
                  A Offline
                  aabdn
                  wrote on last edited by
                  #8

                  @ChrisW67 It worked for me, Thank you Chris

                  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