Solved Issue with layout of pages of QStackedWidget
-
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.
-
@Kira Can we see the code where you create the GUI and layouts?
-
@ambershark : I have created the design using qt creator do you want me to share the layout screen short or something
-
@Kira Actually if you could just post the .ui file we should be able to see it in that.
-
@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> -
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.
-
@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 -
@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 useui->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.
-
@ambershark : Thanks for your help :)