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

Using min-width or min-height in QSS allows cropping any QWidget contents



  • Setting either min-width or min-height in QSS for any QWidget-derived UI control results in the ability to crop its contents in horizontal or vertical direction respectively upon downsizing it interactively. It is as if the QSS code is discarding the UI control's size hint whereas it should just add an additional restriction that the size shouldn't be smaller in either of the directions. Simplest case:

    int main( int argc, char* argv[] )
    {
        QApplication a( argc, argv );
    
        QWidget w;
        QHBoxLayout* const layout = new QHBoxLayout( & w );
        QPushButton* const pb = new QPushButton( "0 1234567890 1 1234567890 2 1234567890" );
        pb->setStyleSheet( "min-width: 10px;" );
        layout->addWidget( pb );
    
        w.show();
    
        return a.exec();
    }
    

    After running the code, squeeze the window horizontally and see that the button's text would get cropped where if the QSS was missing no such thing would happen. The same happens when using C++ code for the same purpose:
    pb->setMinimumWidth( 10 );

    Q: Is there a way to prevent a given QWidget from being squeezed below its size hint aside from:
    pb->setSizePolicy( QSizePolicy::Minimum, pb->sizePolicy().verticalPolicy() );

    which fails to work. Currently, the QPushButton is put inside a hollow QWidget because otherwise the former could be squeezed horizontally even without setting a minimum width.


  • Lifetime Qt Champion

    Hi,

    It works on macOS with Qt 5.12.

    Just one thing, "10px" is not really wide for a button. What output are you expecting ?



  • @SGaist I've already written my expectations about how it should behave above:

    It is as if the QSS code is discarding the UI control's size hint whereas it should just add an additional restriction that the size shouldn't be smaller in either of the directions.

    And from your first sentence it appears that you have understood it:

    It works on macOS with Qt 5.12.


  • Lifetime Qt Champion

    What I meant was: what are you expecting from a 10px wide button ?



  • @SGaist I wasn't expecting it to be squeezable below its size hint. I expected that the explicitly specified minimum width wouldn't be taken into consideration due to being lower than the size hint.


  • Lifetime Qt Champion

    As the name suggests size hint is a hint and only that. If you want boundaries, then it's the role of the min/max size/width/height methods.


Log in to reply