PDA

View Full Version : How to set a fixed size to QPushButton on stylesheet? Adding padding is resizing it



ricardosole
17th April 2019, 19:08
Hi,
I'm trying to set a fixed size to a QPushButton by setting it on stylesheet, but when adding a padding-left to the button, it's being resized to fixed-width + padding-left. I want it to keep the fixed size that i set to the button. And i want to avoid calling SetFixedWidth and SetFixedSize methods because i want it to be loaded of a external stylesheet file. I Already try to set sizePolicy to fixed but ii keeps the same behavior.

Here are my definitions:

button->setStyleSheet("QPushButton {"
" text-align:left;"
" margin: 0px;"
" padding-left: 32px;"
" border: none;"
" min-height: 48px;"
" max-height: 48px;"
" min-width: 248px;"
" max-width: 248px;"
" background-color: #4000FF99;"
"}");

ChrisW67
22nd April 2019, 03:52
The min-width, max-width, min-height, and max-height pertain to the content area of the box model (see https://doc.qt.io/qt-5/stylesheet-reference.html#max-width-propl and https://doc.qt.io/qt-5/stylesheet-customizing.html#box-model)
Left padding puts empty space outside the content box making the overall size larger. Padding, border width and margins all default to zero.

If you want the total size at the outside of the margin to be fixed then you need some basic arithmetic to ensure the four components add to a fixed value.