PDA

View Full Version : QPushButton width and height don't correspond after applying a qstylesheet



jepessen
2nd June 2018, 12:45
I've this code:


#include <QApplication>
#include <QPushButton>

int main(int argc, char **argv) {
QApplication a(argc, argv);
QWidget *widget = new QWidget();
widget->setFixedWidth(600);
widget->setFixedHeight(600);
QPushButton* button = new QPushButton(widget);
button->setText("Test");
button->setStyleSheet("QPushButton { background: red; width: 400px; height: 200px; }");
int w = button->width();
int h = button->height();

QObject::connect(button, &QPushButton::clicked, button, [=]() { button->setText("W: " + QString::number(w) + " H: " + QString::number(h)); });

widget->show();
return a.exec();
}

The button is shown correctly (color is red and dimensions are right), but `w` and `h` variables are `100` and `30`, respectively. It seems that `width()` and `height()` does not take the updated values after applying the stylesheet.
How can I retrieve dimensions of a `QPushButton` correctly after applying a stylesheet?


I'm using Qt 5.11.0 on Visual Studio 2017 (64 bit).