drawing complex controls
-
@Pl45m4 whenever i set my custom style to my app (QApplication) all the complex controls disappear).
this is the drawComplex code that i've written:void ChangeSlider(const QStyleOptionComplex *opt, QPainter* painter, const QWidget* w) { if(opt->state == QStyle::State_Active) { if(opt->activeSubControls == QStyle::SC_SliderHandle) { painter->setBrush(QColor(0xe6, 0x00, 0x00)); painter->setPen(QColor(0xe6, 0x00, 0x00)); } else if(opt->activeSubControls == QStyle::SC_SliderHandle) { painter->setBrush(QColor(0x4D, 0xA6, 0xFF)); painter->setPen(QColor(0x4D, 0xA6, 0xFF)); } } painter->drawRect(opt->rect); } void GlobalStyles::drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *w) const { switch(cc) { case QStyle::CC_Slider: ChangeSlider(opt, p, w); return; default: QCommonStyle::drawComplexControl(cc,opt,p,w); } }
what am i doing wrong here?
-
So you custom slider subclass has
GlobalStyles
style set?!
Do you only callstyle()->drawComplexControl(........)
within the sliderspaintEvent
?
Otherwise it would get overridden.If you look at
QSlider
s default implementation:
Qt6void QSlider::paintEvent(QPaintEvent *) { Q_D(QSlider); QStylePainter p(this); QStyleOptionSlider opt; initStyleOption(&opt); opt.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle; if (d->tickPosition != NoTicks) opt.subControls |= QStyle::SC_SliderTickmarks; p.drawComplexControl(QStyle::CC_Slider, opt); }
Qt5 (slightly different)
void QSlider::paintEvent(QPaintEvent *) { Q_D(QSlider); QPainter p(this); QStyleOptionSlider opt; initStyleOption(&opt); opt.subControls = QStyle::SC_SliderGroove | QStyle::SC_SliderHandle; if (d->tickPosition != NoTicks) opt.subControls |= QStyle::SC_SliderTickmarks; if (d->pressedControl) { opt.activeSubControls = d->pressedControl; opt.state |= QStyle::State_Sunken; } else { opt.activeSubControls = d->hoverControl; } style()->drawComplexControl(QStyle::CC_Slider, &opt, &p, this); }
@happ said in drawing complex controls:
whenever i set my custom style to my app (QApplication) all the complex controls disappear).
Edit:
Haven't seen that... ;-)
I assume because your style only contains information on how to draw aQSlider
.
You might have lost everything else.(not sure about that, maybe somebody else can verify that)
-
@happ Does your style inherit QCommonStyle? If you want other widgets to look like there is no custom style then you can use proxy style.
And here is how you create your style:auto proxy = new MyProxyStyle(QApplication::style()->name()); proxy->setParent(widget); // take ownership to avoid memleak widget->setStyle(proxy);
Notice that the name of the default application style is used to create a proxy style.
There are a layer of OS-dependent styles where an OS-specific drawing happens but they are private. The QCommonStyle probably contains some general drawing only.
-
I was inheriting QCommonStyle when i used QProxyStyle they started showing up but my changes aren't still being implemented.
-
@happ I mean that you can inherit your style from QProxyStyle instead of QCommonStyle
class CustomStyle : public QProxyStyle { public: CustomStyle(cosnt QString styleName): QProxyStyle(styleName) {...} virtual void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *w) const override { switch(cc) { case QStyle::CC_Slider: // draw your slider return; default: QProxyStyle::drawComplexControl(cc,opt,p,w); } } // In the code where you create the slider auto slider = new QSlider(); auto proxy = new CustomStyle(QApplication::style()->name()); proxy->setParent(slider); // take ownership to avoid memleak slider->setStyle(proxy);