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. Issue with layout of pages of QStackedWidget
Forum Updated to NodeBB v4.3 + New Features

Issue with layout of pages of QStackedWidget

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 3.7k Views 1 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.
  • KiraK Offline
    KiraK Offline
    Kira
    wrote on last edited by
    #1

    I am using qt-5.7 and qt creator which comes along with it

    The steps are as follow:

    1)Create a new project as QWindow Widget

    Added QStackedWidget
    

    3.Set the layout of the central wide QHBoxLayout

    Which led to resizing of my qstackedWidget to full screen

    Then i set the first page QHboxLayout and added button and QEdit

    Did the same for the second page
    

    5.When i select the first page and run the application the layout on 1st page gets reflected properly but when i click on button to call second page the layout get lost

    Same happens when i select the second page and call the first page
    

    The behavior of stackedWidget is normal when i dont select layout for pages.

    A 1 Reply Last reply
    0
    • KiraK Kira

      I am using qt-5.7 and qt creator which comes along with it

      The steps are as follow:

      1)Create a new project as QWindow Widget

      Added QStackedWidget
      

      3.Set the layout of the central wide QHBoxLayout

      Which led to resizing of my qstackedWidget to full screen

      Then i set the first page QHboxLayout and added button and QEdit

      Did the same for the second page
      

      5.When i select the first page and run the application the layout on 1st page gets reflected properly but when i click on button to call second page the layout get lost

      Same happens when i select the second page and call the first page
      

      The behavior of stackedWidget is normal when i dont select layout for pages.

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @Kira Can we see the code where you create the GUI and layouts?

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      1 Reply Last reply
      0
      • KiraK Offline
        KiraK Offline
        Kira
        wrote on last edited by
        #3

        @ambershark : I have created the design using qt creator do you want me to share the layout screen short or something

        A 1 Reply Last reply
        0
        • KiraK Kira

          @ambershark : I have created the design using qt creator do you want me to share the layout screen short or something

          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @Kira Actually if you could just post the .ui file we should be able to see it in that.

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          1
          • KiraK Offline
            KiraK Offline
            Kira
            wrote on last edited by
            #5

            @ambershark :
            Sharing the contents of ui file:
            <?xml version="1.0" encoding="UTF-8"?>
            <ui version="4.0">
            <class>MainWindow</class>
            <widget class="QMainWindow" name="MainWindow">
            <property name="geometry">
            <rect>
            <x>0</x>
            <y>0</y>
            <width>424</width>
            <height>305</height>
            </rect>
            </property>
            <property name="windowTitle">
            <string>MainWindow</string>
            </property>
            <widget class="QWidget" name="centralWidget">
            <property name="maximumSize">
            <size>
            <width>16777215</width>
            <height>246</height>
            </size>
            </property>
            <layout class="QVBoxLayout" name="verticalLayout">
            <item>
            <widget class="QStackedWidget" name="stackedWidget">
            <property name="currentIndex">
            <number>0</number>
            </property>
            <widget class="QWidget" name="page">
            <layout class="QHBoxLayout" name="horizontalLayout_4">
            <item>
            <widget class="QWidget" name="widget" native="true">
            <layout class="QHBoxLayout" name="horizontalLayout">
            <item>
            <widget class="QPushButton" name="pushButton">
            <property name="text">
            <string>1</string>
            </property>
            </widget>
            </item>
            <item>
            <widget class="QLineEdit" name="lineEdit"/>
            </item>
            </layout>
            </widget>
            </item>
            </layout>
            </widget>
            <widget class="QWidget" name="page_2">
            <layout class="QHBoxLayout" name="horizontalLayout_3">
            <item>
            <widget class="QWidget" name="widget_2" native="true">
            <layout class="QHBoxLayout" name="horizontalLayout_2">
            <item>
            <widget class="QPushButton" name="pushButton_2">
            <property name="text">
            <string>2</string>
            </property>
            </widget>
            </item>
            <item>
            <widget class="QLineEdit" name="lineEdit_2"/>
            </item>
            </layout>
            </widget>
            </item>
            </layout>
            </widget>
            </widget>
            </item>
            </layout>
            </widget>
            <widget class="QMenuBar" name="menuBar">
            <property name="geometry">
            <rect>
            <x>0</x>
            <y>0</y>
            <width>424</width>
            <height>20</height>
            </rect>
            </property>
            </widget>
            <widget class="QToolBar" name="mainToolBar">
            <attribute name="toolBarArea">
            <enum>TopToolBarArea</enum>
            </attribute>
            <attribute name="toolBarBreak">
            <bool>false</bool>
            </attribute>
            </widget>
            <widget class="QStatusBar" name="statusBar"/>
            <widget class="QToolBar" name="toolBar">
            <property name="windowTitle">
            <string>toolBar</string>
            </property>
            <attribute name="toolBarArea">
            <enum>TopToolBarArea</enum>
            </attribute>
            <attribute name="toolBarBreak">
            <bool>false</bool>
            </attribute>
            </widget>
            </widget>
            <layoutdefault spacing="6" margin="11"/>
            <resources/>
            <connections/>
            </ui>

            1 Reply Last reply
            0
            • A Offline
              A Offline
              ambershark
              wrote on last edited by
              #6

              Ok so I threw your ui into a quick project of my own and all your layouts work properly. What exactly is the problem?

              Can you show it with a quick screencast or at least a screen shot?

              To test changing widgets in the QStackedWidget I just added 2 menu items to your mainwindow and coded them like so:

              MainWindow::MainWindow()
              {
                      ui_.setupUi(this);
                      connect(ui_.actionOne, &QAction::triggered, [=](){ this->ui_.stackedWidget->setCurrentIndex(0); });
                      connect(ui_.actionTwo, &QAction::triggered, [=](){ this->ui_.stackedWidget->setCurrentIndex(1); });
              }
              

              Everything worked fine for me. You'll have to describe the issue better or show a cast/screen of the problem.

              My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

              KiraK 1 Reply Last reply
              2
              • A ambershark

                Ok so I threw your ui into a quick project of my own and all your layouts work properly. What exactly is the problem?

                Can you show it with a quick screencast or at least a screen shot?

                To test changing widgets in the QStackedWidget I just added 2 menu items to your mainwindow and coded them like so:

                MainWindow::MainWindow()
                {
                        ui_.setupUi(this);
                        connect(ui_.actionOne, &QAction::triggered, [=](){ this->ui_.stackedWidget->setCurrentIndex(0); });
                        connect(ui_.actionTwo, &QAction::triggered, [=](){ this->ui_.stackedWidget->setCurrentIndex(1); });
                }
                

                Everything worked fine for me. You'll have to describe the issue better or show a cast/screen of the problem.

                KiraK Offline
                KiraK Offline
                Kira
                wrote on last edited by
                #7

                @ambershark : Thanks a lot you saved my day.

                Would like to elaborate the problem so that you could provide an explanation.
                Actually there was no issue with the ui file, i think the problem was in invoking the widget.

                I had used the following event on Qpushbutton to call the widgets:

                void MainWindow::on_pushButton_clicked()
                {
                ui->page->setVisible(false);
                ui->page_2->setVisible(true);

                }

                Which created the problem of elements getting rearranged and all the layout getting lost.
                After invoking the widgets by setting the index everything worked fine.

                Can you please provide an explanation to the problem!!

                Thanks and regards,
                Kira

                A 1 Reply Last reply
                0
                • KiraK Kira

                  @ambershark : Thanks a lot you saved my day.

                  Would like to elaborate the problem so that you could provide an explanation.
                  Actually there was no issue with the ui file, i think the problem was in invoking the widget.

                  I had used the following event on Qpushbutton to call the widgets:

                  void MainWindow::on_pushButton_clicked()
                  {
                  ui->page->setVisible(false);
                  ui->page_2->setVisible(true);

                  }

                  Which created the problem of elements getting rearranged and all the layout getting lost.
                  After invoking the widgets by setting the index everything worked fine.

                  Can you please provide an explanation to the problem!!

                  Thanks and regards,
                  Kira

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

                  @Kira Oh yea.. when you give up control of those widgets to the QStackedWidget you do not want to show/hide them any more manually. So basically the QStackedWidget would then manage visible states. You however were trying to show/hide them without telling the QStackedWidget to change pages for you.

                  So basically in your on_pushButton_clicked() you would just use ui->stackedWidget->setCurrentIndex(0) or substitute 0 for whatever page in the widget you want to show. Which I think you figured out from my test code there. :)

                  As far as layouts go, showing and hiding widgets can disfigure layouts quite regularly. So it's no surprise that forcing thing on and off like you did would make it pretty nasty looking.

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  1 Reply Last reply
                  2
                  • KiraK Offline
                    KiraK Offline
                    Kira
                    wrote on last edited by
                    #9

                    @ambershark : Thanks for your help :)

                    1 Reply Last reply
                    1

                    • Login

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