c++ - 如何使用样式表为 Qt Widget 而不是其子项设置样式?

标签 c++ qt widget

我有一个:

class Box : public QWidget

它有

this->setLayout(new QGridLayout(this));

我试过:

this->setStyleSheet( "border-radius: 5px; "
                     "border: 1px solid black;"
                     "border: 2px groove gray;"
                     "background-color:blue;");

this->setStyleSheet( "QGridLayout{"
                         "background-color:blue;"
                         "border-radius: 5px; "
                         "border: 1px solid black;"
                         "border: 2px groove gray;"
                     "}"
                   );

this->setObjectName(QString("Box"));
this->setStyleSheet( "QWidget#Box {"
                         "background-color:blue;"
                         "border-radius: 5px; "
                         "border: 1px solid black;"
                         "border: 2px groove gray;"
                     "}"
                   );

但第一个只影响添加的项目,其他两个什么都不做。我希望盒子本身有圆角和边框(关于如何在行之间做线条的奖励)。

如何让样式表影响 Box 小部件,而不是它的子部件?

最佳答案

更准确地说,我可以使用:

QWidget#idName {
    border: 1px solid grey;
}

Box {
    border: 1px solid grey;
}

在我看来,后者更容易,因为它不需要使用 id 名称。

为什么这些不起作用的主要问题是因为这被认为是自定义小部件,因此需要自定义绘制事件:

 void Box::paintEvent(QPaintEvent *) {
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
 }

这取自:Qt Stylesheet for custom widget

关于c++ - 如何使用样式表为 Qt Widget 而不是其子项设置样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12116413/

相关文章:

android - 使用应用程序更新 ListView 小部件

css - 编辑 Wordpress 小部件 CSS

arrays - 在 Flutter Cards 中插入 JSON 信息?

c++ - arr[0] 的大小怎么是 8 个字节?

c++ - 运算符 << 和继承/组合

qt - QwtPlot - 多个 Y 轴

c++ - QLineEdit editingFinished信号在改变焦点时两次?

c++ - C/C++ : -msse and -msse2 Flags do not have any effect on the binaries?

c++ - 如何正确删除已知二维大小的二维数组?

Qt 在禁用的表中保持滚动条启用