Problem size policy in QStackedWidget
-
wrote on 30 Aug 2018, 06:47 last edited by
Hello,
I have a problem in my application with, i guess, size policy. Here is the situation :
I have a QStackedWidget organized in a QVBoxLayout which contains
- A QStackedWidget with vertical size policy : Fixed
- A QGroupBox with vertical size policy : Fixed
- A QTableWidget with vertical size policy : MinimumExpanding
- A QStackedWidget with vertical size policy : Fixed
The objectif is that the QTableWidget takes the most space possible. In Qt Designer, it works well but when i start my application, there is big space at top and bottom of the first QStackedWidget.
I tried to use setContentMargins(0,0,0,0) in the code but it hasn't effect.
What am i doing wrong ?
-
Hi and welcome to devnet,
Can you show the result and what you get with designer ?
-
wrote on 31 Aug 2018, 09:05 last edited by
-
This is the result in Qt Designer (sorry for the pixelization effect, i can't show you the entire application) :
And this the result in the application :
wrote on 31 Aug 2018, 09:22 last edited byIn Qt Designer, it works well but when i start my application, there is big space at top and bottom of the first QStackedWidget.
Where in the second screenshot is there "big space at top and bottom"? I see space above & below
Test running...
(is that your "A QGroupBox with vertical size policy : Fixed"?), but not at the top/bottom of theQStackedWidget
itself, which is what you claimed? -
wrote on 31 Aug 2018, 09:40 last edited by
@JonB said in Problem size policy in QStackedWidget:
Where in the second screenshot is there "big space at top and bottom"? I see space above & below Test running... (is that your "A QGroupBox with vertical size policy : Fixed"?), but not at the top/bottom of the QStackedWidget itself, which is what you claimed?
Yes there is space above and below "Test running ...". This is a QStackedWidget with another page containing a QLabel.
-
Are you doing any modification to that widget in the constructor ?
-
wrote on 3 Sept 2018, 07:31 last edited by
Hello,
@SGaist said in Problem size policy in QStackedWidget:
Are you doing any modification to that widget in the constructor ?
No, not in this page.
-
I would add a stretch factor of two to the QTableWidget so that it should take more place naturally.
-
wrote on 4 Sept 2018, 10:05 last edited by
@SGaist said in Problem size policy in QStackedWidget:
I would add a stretch factor of two to the QTableWidget so that it should take more place naturally.
It has no effect. This is weird, when i add a stretch factor of 2 to the QTableWidget and a stretch factor of 1 to the QStackedWidget in QtDesigner, i have the same effect that in the application. I don't think there is a link but i would to mention it.
-
wrote on 7 Sept 2018, 09:08 last edited by
The only solution that i see is too put two labels and hide/show them for change the text.
No others ideas ? -
Without sharing your application, can you provide a minimal compile example that shows that behaviour ?
-
wrote on 10 Sept 2018, 09:20 last edited by Loic B. 9 Oct 2018, 09:32
Here is an example :
main.cpp :#include <QApplication> #include <QWidget> #include "ui_test.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget parent; Ui_Form form; form.setupUi(&parent); QObject::connect(form.switchButton, &QPushButton::clicked, [=]() { form.testStackedWidget->setCurrentIndex(form.testStackedWidget->currentIndex() == 0 ? 1 : 0); }); parent.show(); return app.exec(); }
And test.ui :
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>Form</class> <widget class="QWidget" name="Form"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>306</width> <height>517</height> </rect> </property> <property name="windowTitle"> <string>Form</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <property name="margin"> <number>0</number> </property> <item> <widget class="QStackedWidget" name="mainStackedWidget"> <property name="currentIndex"> <number>0</number> </property> <widget class="QWidget" name="page"> <layout class="QVBoxLayout" name="verticalLayout_2"> <item> <widget class="QStackedWidget" name="testStackedWidget"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="currentIndex"> <number>0</number> </property> <widget class="QWidget" name="page_3"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <layout class="QVBoxLayout" name="verticalLayout_3"> <item> <widget class="QLabel" name="label"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="text"> <string><html><head/><body><p align="center">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc ut enim justo. Quisque vitae massa pellentesque, tristique arcu id, elementum urna. Proin sit amet lectus quam. Maecenas ullamcorper elit eget neque volutpat scelerisque. In hac habitasse platea dictumst. Donec convallis dui a vehicula feugiat.</p></body></html></string> </property> <property name="wordWrap"> <bool>true</bool> </property> </widget> </item> </layout> </widget> <widget class="QWidget" name="page_4"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <layout class="QVBoxLayout" name="verticalLayout_4"> <item> <widget class="QLabel" name="label_2"> <property name="sizePolicy"> <sizepolicy hsizetype="Preferred" vsizetype="Fixed"> <horstretch>0</horstretch> <verstretch>0</verstretch> </sizepolicy> </property> <property name="text"> <string>Test running...</string> </property> <property name="wordWrap"> <bool>true</bool> </property> </widget> </item> </layout> </widget> </widget> </item> <item> <widget class="QTableWidget" name="tableWidget"> <property name="sizePolicy"> <sizepolicy hsizetype="Expanding" vsizetype="Expanding"> <horstretch>0</horstretch> <verstretch>1</verstretch> </sizepolicy> </property> <property name="showGrid"> <bool>true</bool> </property> <attribute name="horizontalHeaderDefaultSectionSize"> <number>80</number> </attribute> <attribute name="horizontalHeaderStretchLastSection"> <bool>true</bool> </attribute> <column> <property name="text"> <string>A</string> </property> </column> <column> <property name="text"> <string>B</string> </property> </column> <column> <property name="text"> <string>C</string> </property> </column> </widget> </item> <item> <widget class="QPushButton" name="switchButton"> <property name="text"> <string>Switch</string> </property> </widget> </item> </layout> </widget> <widget class="QWidget" name="page_2"/> </widget> </item> </layout> </widget> <resources/> <connections/> </ui>
-
wrote on 10 Sept 2018, 10:08 last edited by
I think this is normal. The text in the first page of testStackedWidget takes more lines so the global height is take from here. However, why Qt Designer don't show the same behavior ?
-
You're correct and that's a good question. What version of Designer are you using ?
7/14