Qt stylesheet bug!!
-
hi everyone
Qt Ver: Qt5.12.12
OS:Windows10;
MinGW
I found a bug in Stylesheet:
In the main function, I call qApp->setstylesheet to set the global style of QGroupBox, the sytelsheet is as follows
"QGroupBox{
border:1px solid rgb(255,0,0);
border-radius:6px;
}
QGroupBox[title]{
margin-top: 10px;
border:1px solid rgb(0,0,255);
}
QGroupBox::title {
subcontrol-origin: margin;
}"
Let me explain briefly:
QGroupBox[title]: Set the margin-top for those whose title attribute is not empty to 10px;
In this case, those QGroupBoxes whose title is not empty will have 10px blank space
It turns out that those QGroupBoxes that are not children of QStackWidget are correct,
But if a QGoupbox is a child of QStackWidget, its margin-top is 0px!!!Laterlly, I found that if I call setstylesheet after w.show(); both is right!!
it is a bug
bellow is the project
https://nc.321205.xyz/s/sJYGb9sajCaLq3R/download/untitled4.rar -
Please provide a minimal, compilable example and use the code - tags to format your code so other can properly read it.
-
@Jodie said in Qt stylesheet bug!!:
those two group share a same "y",the height of each "border-top" should be same!!!
Why? Also it does not help to adding more '!' to your sentences - a proper problem description including a testcase is much more helpful.
-
sorry,here is the code
#include <QApplication> #include <QFile> #include <QTextStream> #include <QtCore/QVariant> #include <QtWidgets/QApplication> #include <QtWidgets/QDialog> #include <QtWidgets/QGroupBox> #include <QtWidgets/QStackedWidget> #include <QtWidgets/QWidget> #include <QDialog> int main(int argc, char *argv[]) { QApplication a(argc, argv); QFile f(":/zhang.qss"); if (f.open(QFile::ReadOnly | QFile::Text)) { QTextStream ts(&f); qApp->setStyleSheet(ts.readAll()); f.close(); } QGroupBox *groupBox_a,*groupBox_b; QStackedWidget *stackedWidget; QWidget *page; QDialog *dlg=new QDialog(); groupBox_a = new QGroupBox("group_a",dlg); groupBox_a->setGeometry(QRect(0, 0, 100,100)); stackedWidget = new QStackedWidget(dlg); stackedWidget->setGeometry(QRect(110, 0, 110, 80)); page = new QWidget(); groupBox_b = new QGroupBox(page); groupBox_b->setGeometry(QRect(0, 0, 100, 80)); groupBox_b->setTitle("group_b"); //if "groupBox_b->setTitle" here,the margin-top is 10px; showing in picture a stackedWidget->addWidget(page); //groupBox_b->setTitle("group_b"); //if "groupBox_b->setTitle" here,the margin-top is 0px; showing in picture b dlg->show(); return a.exec(); }
stylesheet file zhang.qss
QGroupBox{ border:1px solid rgb(255,0,0); border-radius:6px; } QGroupBox[title]{ margin-top: 10px; border:1px solid rgb(0,0,255); } QGroupBox::title { subcontrol-origin: margin; }
picture a
picture b
group_b should look the same as group_a. Because their Stylesheet is the same, but the test results confuse me
-
QString group_style_sheet = QString("QGroupBox{border: 2px solid gray;border-radius: 5px;margin-top: %1px;}" ) .arg( font_height * 1.6 );
Set proper margin top in the stylesheet. You can adjust 1.6 to fit your use.
int getReferenceFontHeight( const QFont & font )
{
QRect bounding_rect = QFontMetrics( font ).tightBoundingRect( QString( "MWLPGHXYZmwlpghxyz" ) );
return bounding_rect.height();
} -
Re: Qt stylesheet bug!!
there are 3 diffdrence between picture a and b;
1 group_b font size
2 group_b border color
3 group_b margin top
accoding to the stylesheet group b should be blue,because its title is not emptyQGroupBox[title]{ margin-top: 10px; border:1px solid rgb(0,0,255); }
-
The problem here is that dynamic properties, like title, get cached and don't update automatically. It's not a real dynamic binding.
When you call setTitle before you add the widget to the stacked widget the style gets cached with the title applied. When you call setTitle after, the style is already cached with no title applied so there's no change.
To workaround it, after you set the title you can force the style to be re-evaluated for that widget:
qApp->style()->unpolish(groupBox_b); qApp->style()->polish(groupBox_b);
-
The problem here is that dynamic properties, like title, get cached and don't update automatically. It's not a real dynamic binding.
When you call setTitle before you add the widget to the stacked widget the style gets cached with the title applied. When you call setTitle after, the style is already cached with no title applied so there's no change.
To workaround it, after you set the title you can force the style to be re-evaluated for that widget:
qApp->style()->unpolish(groupBox_b); qApp->style()->polish(groupBox_b);
@Chris-Kawa said in Qt stylesheet bug!!:
The problem here is that dynamic properties, like title, get cached and don't update automatically. It's not a real dynamic binding.
When you call setTitle before you add the widget to the stacked widget the style gets cached with the title applied. When you call setTitle after, the style is already cached with no title applied so there's no change.
To workaround it, after you set the title you can force the style to be re-evaluated for that widget:
qApp->style()->unpolish(groupBox_b);
qApp->style()->polish(groupBox_b);This may be the better way for it. My way works fine as well and sometimes I want to adjust the gap between the title and border of the group a bit.
-
Re: Qt stylesheet bug!!
there are 3 diffdrence between picture a and b;
1 group_b font size
2 group_b border color
3 group_b margin top
accoding to the stylesheet group b should be blue,because its title is not emptyQGroupBox[title]{ margin-top: 10px; border:1px solid rgb(0,0,255); }
-
sorry,here is the code
#include <QApplication> #include <QFile> #include <QTextStream> #include <QtCore/QVariant> #include <QtWidgets/QApplication> #include <QtWidgets/QDialog> #include <QtWidgets/QGroupBox> #include <QtWidgets/QStackedWidget> #include <QtWidgets/QWidget> #include <QDialog> int main(int argc, char *argv[]) { QApplication a(argc, argv); QFile f(":/zhang.qss"); if (f.open(QFile::ReadOnly | QFile::Text)) { QTextStream ts(&f); qApp->setStyleSheet(ts.readAll()); f.close(); } QGroupBox *groupBox_a,*groupBox_b; QStackedWidget *stackedWidget; QWidget *page; QDialog *dlg=new QDialog(); groupBox_a = new QGroupBox("group_a",dlg); groupBox_a->setGeometry(QRect(0, 0, 100,100)); stackedWidget = new QStackedWidget(dlg); stackedWidget->setGeometry(QRect(110, 0, 110, 80)); page = new QWidget(); groupBox_b = new QGroupBox(page); groupBox_b->setGeometry(QRect(0, 0, 100, 80)); groupBox_b->setTitle("group_b"); //if "groupBox_b->setTitle" here,the margin-top is 10px; showing in picture a stackedWidget->addWidget(page); //groupBox_b->setTitle("group_b"); //if "groupBox_b->setTitle" here,the margin-top is 0px; showing in picture b dlg->show(); return a.exec(); }
stylesheet file zhang.qss
QGroupBox{ border:1px solid rgb(255,0,0); border-radius:6px; } QGroupBox[title]{ margin-top: 10px; border:1px solid rgb(0,0,255); } QGroupBox::title { subcontrol-origin: margin; }
picture a
picture b
group_b should look the same as group_a. Because their Stylesheet is the same, but the test results confuse me