How to create a Qt Widget style, and not its children with style sheets?

I have:

class Box : public QWidget 

and has

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

I tried to do:

 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;" "}" ); 

but the first only affects the added elements, the other two do nothing. I want the box to have rounded corners and a border (a bonus for making lines between lines).

How to make a stylesheet affect a Box widget, and not its children?

+8
c ++ qt widget
source share
2 answers

To be more precise, I could use:

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

or

 Box { border: 1px solid grey; } 

The latter is simpler, in my opinion, since it does not require the use of name names.

The main problem with why they do not work is that it is considered custom widgets and therefore requires a custom drawing 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 style sheets for a custom widget

+9
source share

You need to define an object class and instance, for example, in plain CSS.

 QWidget#BoxName { border-radius: 5px; border: 1px solid black; border: 2px groove gray; } 

This is the same answer as here: Get the variable name Qt Widget (for use in a stylish worksheet)?

 box->setStyleSheet(QString::fromUtf8("QWidget#box\n" "{\n" " border-radius: 5px;\n" " border: 1px solid black;\n" " border: 2px groove gray;\n" "}\n" "")); 
+5
source share

All Articles