QPushButton resizes after color change

I want to change the background color of QPushButtons. The problem is that changing the background color using setStyleSheet("QPushButton { background-color: #00FF00 }");, the button size also scales.

before

enter image description here

after

enter image description here

I understand that by changing the background color, the entire button stylesheet is overwritten and reset to default ( How to override only one pair of properties: value in Qt StyleSheet ).

My question is: how can I set the button size so that it is the same size as the original?

I use MacOSX and I have tried all combinations of height, minimum height, indentation.

+4
source share
2 answers

, , min-width min-height .

:

, .

, .

.. ( UI pushButton):

QString width ("min-width: " +
               QString::number(ui.pushButton->size().width()) +
               " px; " +
               "max-width: " +
               QString::number(ui.pushButton->size().width()) +
               " px;");
QString height ("min-height: " +
                QString::number(ui.pushButton->size().height()) +
                " px; " +
                "max-height: " +
                QString::number(ui.pushButton->size().height()) +
                " px;");

QString style ("#pushButton { " + width + height +
               "background-color: black; }");

qApp->setStyleSheet(style);

, reference

, max-width .

, . - :

int width = ui.pushButton->size().width();
int height = ui.pushButton->size().height();

QString style ("#pushButton { background-color: black; }");

qApp->setStyleSheet(style);

ui.pushButton->resize(width, height);
+1

, QPalette .

QPushButton button;
QPalette palette = button.palette();
palette.setColor(QPalette::Background, QColor("#00FF00");
button.setPalette(palette);
+2

All Articles