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. Layout not respecting sizes

Layout not respecting sizes

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.0k 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.
  • G Offline
    G Offline
    GrahamLa
    wrote on last edited by
    #1

    Hi
    I have a horizontal layout into which I add widgets of a fixed size.
    This layout is then added to a grid layout but when displayed the sizes are not respected.
    Please can anyone tell me what I have wrong?
    Thanks

    			QHBoxLayout* l = new QHBoxLayout();
    			m_pxLatCogGraphLabel->setFixedSize(QSize(50, 50));
    			m_pxModeSelectorLabel->setFixedSize(QSize(100, 50));
    			m_pxModeSelector->setFixedSize(QSize(60, 50));
    
    			m_pxLatCogGraphLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    			m_pxModeSelectorLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    			m_pxModeSelector->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    
    			l->addWidget(m_pxLatCogGraphLabel);
    			//l->addSpacerItem(new QSpacerItem(40, 0, QSizePolicy::Fixed));
    			l->addWidget(m_pxModeSelectorLabel);
    			l->addWidget(m_pxModeSelector);
    			l->setSizeConstraint(QLayout::SetFixedSize);
    
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      The layout has to fill all the space, you just have to give it something to occupy the extra space. add l->addStretch(1); to tell it where the extra white space should go

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      G 1 Reply Last reply
      3
      • VRoninV VRonin

        The layout has to fill all the space, you just have to give it something to occupy the extra space. add l->addStretch(1); to tell it where the extra white space should go

        G Offline
        G Offline
        GrahamLa
        wrote on last edited by
        #3

        @VRonin
        I have played with the stretch factor but the widgets still remain the same size.
        Is there anything else I can try?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Can you show what you get and what you would expect ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          G 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Can you show what you get and what you would expect ?

            G Offline
            G Offline
            GrahamLa
            wrote on last edited by
            #5

            @SGaist
            Hi
            This is my current code

            	m_pxModeSelector = new LeftRightSlider(QStringLiteral("WeightAndBalanceModeSelector"));
            	m_pxModeSelector->setEnabled(true);
            	m_pxModeSelectorLabel = new QLabel(m_strModeSelectorLabel);
            
            	m_pxModeSelector->setMaximumSize(60, 60);
            	m_pxModeSelectorLabel->setMaximumSize(100, 60);
            	m_pxLatCogGraphLabel->setMaximumSize(100, 60);
            
            	QHBoxLayout* box = new QHBoxLayout();
            	box->addWidget(m_pxLatCogGraphLabel);
            	box->addWidget(m_pxModeSelectorLabel);
            	box->addWidget(m_pxModeSelector);
            
            	box->addStretch(1);
            
            

            This displays this
            0_1550650369519_471e1fdc-2a32-458d-92d4-e7fa8c08bcb9-image.png
            Changing the sizes of the labels seems to work, but not the slider button.
            I would like it to look like this
            0_1550650445176_820d5d04-1946-4287-88d2-ae9d87302fd4-image.png

            Any ideas would be appreciated

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6
              m_pxModeSelector->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
              QHBoxLayout* box = new QHBoxLayout();
              box->addWidget(m_pxLatCogGraphLabel);
              box->addItem(new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Preferred));
              box->addWidget(m_pxModeSelectorLabel);
              box->addWidget(m_pxModeSelector);
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              G 1 Reply Last reply
              0
              • VRoninV VRonin
                m_pxModeSelector->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
                QHBoxLayout* box = new QHBoxLayout();
                box->addWidget(m_pxLatCogGraphLabel);
                box->addItem(new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Preferred));
                box->addWidget(m_pxModeSelectorLabel);
                box->addWidget(m_pxModeSelector);
                
                G Offline
                G Offline
                GrahamLa
                wrote on last edited by
                #7

                @VRonin
                Unfortunately this gives the same result in my case

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  Strange...
                  Can you try this minimal example?

                  #include <QApplication>
                  #include <QSlider>
                  #include <QLabel>
                  #include <QHBoxLayout>
                  
                  int main(int argc, char ** argv)
                  {
                      QApplication app(argc,argv);
                      QWidget mainWid;
                      mainWid.setMinimumSize(600,10);
                      QLabel *label1 = new QLabel("Test1",&mainWid);
                      QLabel *label2 = new QLabel("Test2",&mainWid);
                      QSlider* slider = new QSlider(Qt::Horizontal,&mainWid);
                      slider->setRange(0,100);
                      slider->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
                      QHBoxLayout* mainLay = new QHBoxLayout(&mainWid);
                      mainLay->addWidget(label1);
                      mainLay->addItem(new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Preferred));
                      mainLay->addWidget(label2);
                      mainLay->addWidget(slider);
                      mainWid.show();
                      return app.exec();
                  }
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  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