Problem size policy in QStackedWidget



  • 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 ?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you show the result and what you get with designer ?



  • This is the result in Qt Designer (sorry for the pixelization effect, i can't show you the entire application) :
    0_1535706261208_qt-designer.PNG

    And this the result in the application :
    0_1535706280360_result-xact.png



  • @Loic-B.

    In 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 the QStackedWidget itself, which is what you claimed?



  • @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.


  • Lifetime Qt Champion

    Are you doing any modification to that widget in the constructor ?



  • 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.


  • Lifetime Qt Champion

    I would add a stretch factor of two to the QTableWidget so that it should take more place naturally.



  • @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.



  • The only solution that i see is too put two labels and hide/show them for change the text.
    No others ideas ?


  • Lifetime Qt Champion

    Without sharing your application, can you provide a minimal compile example that shows that behaviour ?



  • 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>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p align=&quot;center&quot;&gt;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.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</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>
    


  • 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 ?


  • Lifetime Qt Champion

    You're correct and that's a good question. What version of Designer are you using ?


Log in to reply