向Qt样式表添加特定性时丢失样式

Losing style when adding specificity to a Qt stylesheet

本文关键字:样式 Qt 添加      更新时间:2023-10-16

这是我的代码:

const QString STYLE_SHEET = "background-color: rgba(x,x,x,y);"
                            "border: 1px solid gray;"
                            "border-radius: 0px;"
                            "border-top: 1px solid red;"
                            "border-bottom: 1px solid blue;"
                            "border-radius: 0px;";

SS稍后应用于代码中,如下所示:

QWidget * myWidget;
myWidget = new QWidget(parent);
myWidget.setObjectName("myWidgetName");
myWidget.setStyleSheet(STYLE_SHEET);

它像冠军一样工作,只是边界渗入了子QFrame。如果我通过将STYLE_SHEET与以下内容括起来来为其添加特异性:

const QString QWidget#myWidgetName { }

STYLE_SHEET不再适用。它解决了子QFrame的流血问题,但我的QWidget失去了它的风格。没有风格的QWidget有什么用?

const QString STYLE_SHEET = "QWidget#myWidgetName {n"
                            "    background-color: rgba(x,x,x,y);n"
                            "    border: 1px solid gray;n"
                            "    border-radius: 0px;n"
                            "    border-top: 1px solid red;n"
                            "    border-bottom: 1px solid blue;n"
                            "    border-radius: 0px;n"
                            "}n";