Setting min-width/max-width in stylesheet (.qss) does not work after setting layout size constraint of QDialog to QLayout::SetFixedSize



  • Two requirements for dialog:

    1. Want the dialog can resize to suitable size when any child widget changes size, so I set layout size constraint of dialog to QLayout::SetFixedSize.
    2. Want the width of the dialog can be customized by min-width/max-width to fixed value in stylesheet.

    Issue:
    min-width/max-width in stylesheet (.qss) does not work.

    Here is the code:

    #include <qlayout.h>
    
    MyDialog::MyDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::MyDialog)
    {
        ui->setupUi(this);
        layout()->setSizeConstraint(QLayout::SetFixedSize);
        setStyleSheet("QDialog { min-width: 800px; max-width: 800px }"); // just a simple example, actually I want to customize in .qss file.
    }
    

    Questions:

    1. Is it correct behavior that width can not be customized after setting size constraint to QLayout::SetFixedSize?
    2. How can I achieve the requirements?

  • Moderators

    @xiazeyi said:

    Want the dialog can resize to suitable size when any child widget changes size, so I set layout size constraint of dialog to QLayout::SetFixedSize.

    SetFixedSize sets, well, a fixed size. It means it is set to the size hint value and can't be changed anymore by anything. So in your case this is definitely not what you want.

    Leave the size constraint at default and just specify the min and max values in the stylesheet.



  • My dialog has vertical layout and there are several QFrame widgets on the dialog, but the dialog won't resize to a smaller size when the height of its children becomes smaller.


  • Moderators

    How do you change the size of the children frames?



  • By making some widgets in QFrame visible/invisible when the user does some interaction on dialog.
    Currently, I can set the direct children to fixed width instead of the dialog after setting size constraint of dialog to QLayout::SetFixedSize, then everything is ok.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.