Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Layout not respecting sizes



  • 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);
    
    


  • 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



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


  • Lifetime Qt Champion

    Hi,

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



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



  • 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);
    


  • @VRonin
    Unfortunately this gives the same result in my case



  • 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();
    }
    

Log in to reply