How do I style a Qt Widget not its children with stylesheets?

前端 未结 2 790
一生所求
一生所求 2020-12-08 06:14

I have a:

class Box : public QWidget

and it has

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

I tried doin

2条回答
  •  余生分开走
    2020-12-08 06:38

    To be more precise I could have used:

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

    or

    Box {
        border: 1px solid grey;
    }
    

    The latter is easier, in my opinion, as it doesn't require the use of id names.

    The main problem with why these weren't working though is because this is considered a custom widget and therefore needs a custom paint event:

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

    This was taken from: Qt Stylesheet for custom widget

提交回复
热议问题