[Help] : How to style sub-control for inherited classes?
-
Hey there, hope all's well - I couldn't find any obvious online so I'm seeking help here.
I implemented a subclass for QTabBar because I needed to override the
tabSizeHintfunction.Unfortunately after implementing the new class the style sheet, my style sheet no longer works for sub-controls, the style for custom-class works however. I'd like to mention I also have both
Q_OBJECTmacro and have overridden theQWidget::PaintEventin my class.Appreciate any help - thanks in advance :].
// This works! NodeViewTab { // My custom class inheriting QTabBar background-color: #353535; border: 0; padding: 0; margin: 0; outline: 0; border-bottom: 2 solid #505050; } // This does NOT! NodeViewTab::tab, QTabBar::tab { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #454545, stop: 1.0 #353535); color: #c0c0c0; min-width: 8ex; padding: 2px; max-width: 80ex; } -
When you paint yourself the stylesheet can not be in effect. What do you do in the paint event?
-
hey Christian - I've looked at other topics and they said it's required to override the
paintEventin order forQWidgetsub-classes to use style sheets. And in my experience this has proven true - as without it the stylesheet wouldn't work to begin with.void MyQWidget::paintEvent(QPaintEvent* e){ QStyleOption opt; opt.initFrom(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); }HOWEVER, I realized I didn't actually need a subclass for
QTabBarsince there was a func calledsetExpandingthat I needed to set to false. I just deleted my subclass and I'm using the QTabBar style sheets and.. I guess things just work as I want it to for now.Having said that, do you know what would be the proper method to set the style of subcontrols for custom classes?
-
So you call the painting stuff and the style sheet can apply. But you don't style MyWidget but NodeViewTab.