drawComplexControl and transparent background
-
Hi
I have an combobox with a paintevent.
I try to paint the arrow on it but faces this issue
that the background is not transparent but filled so
all i drew before is erased.I draw the arrow like this:
QStyleOptionComboBox comboOptions;
comboOptions.subControls = QStyle::SC_ComboBoxArrow;
QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboOptions, &painter);I tried setting NoBrush on the painter but it had no effect.
Anyone, knows how to make it transparent ? (if possible)
(and no, i cant paint arrow first as other drawing will clear also)
-
@mrjj
you need to set the palette of the passed styleoption accordingly.
Probably you need to set theQPalette::Base
orQPalette::Window
role of it.
The QStyle initializes the painter on it's own.Alternatively instead of drawing the whole complex control you could also just paint the sub control
QStyle::SC_ComboBoxArrow
OrQStyle::SC_ComboBoxFrame
first, then your painting then theQStyle::SC_ComboBoxArrow
-
@raven-worx said in drawComplexControl and transparent background:
The QStyle initializes the painter on it's own.
Ah, that is why nothing really had any effect.
-Alternatively instead of drawing the whole complex control you could also just paint the sub control QStyle::SC_ComboBoxArrow
Or QStyle::SC_ComboBoxFrame first, then your painting then the QStyle::SC_ComboBoxArrowHmm. First i draw via a delegate so it looks the same open and closed.
QStyleOptionComboBox comboOptions; initStyleOption(&comboOptions); // draw as delegate const QModelIndex modelIdx = view()->model()->index(currentIndex(), modelColumn()); view()->itemDelegate()->paint(&painter, delegateFromCombo(comboOptions, modelIdx), modelIdx); // then draw arrow comboOptions.subControls = QStyle::SC_ComboBoxArrow; QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboOptions, &painter);
So not sure I can split it as you suggest.
is there another draw function that take QStyle::SC_ComboBoxArrow , other than
drawComplexControl ?I will try the palette. Thank you.
-
@mrjj said in drawComplexControl and transparent background:
So not sure I can split it as you suggest.
is there another draw function that take QStyle::SC_ComboBoxArrow , other than
drawComplexControl ?my fault sry. I wrongly remembered a QStyle::drawSubControl()
Didn't the palette approach work? -
Well QPalette::Background had no effect and
QPalette::Window seems to be used as border ?QPalette bgpal = palette(); bgpal.setColor (QPalette::Window, Qt::red); setPalette(bgpal);
But your idea is very valid. Just need to find out what role it uses :)
ps: i also think i saw drawSubControl() but it was drawControl
-
@mrjj said in drawComplexControl and transparent background:
what i meant was rather something like this:QStyleOptionComboBox comboOptions; QPalette p = comboOptions.palette; p.setColor(QPalette::Base, Qt::transparent); comboOptions.palette = p; QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboOptions, &painter);
-
@raven-worx said in drawComplexControl and transparent background:
Thank you.
It have no effect.
I will try other QPalette::keysI tried
QPalette bgpal = palette(); for (int cc=0; cc < QPalette::NColorRoles; cc++ ) bgpal.setColor (cc, Qt::red); setPalette(bgpal);
and got
But clearly it should be via comboOptions.palette as you suggest.
Update::
Oddly, setting all palette to trans, givesQPalette p = comboOptions.palette; for (int cc = 0; cc < QPalette::NColorRoles; cc++ ) { p.setColor (cc, Qt::transparent); } comboOptions.palette = p;
Time to use the source, luke, i think :)
Thank you for your input. Clearly its something like that.
-
@mrjj said in drawComplexControl and transparent background:
Time to use the source, luke, i think :)
^^
Thank you for your input. Clearly its something like that.
or maybe not. QStyle sometimes isn't that cooperative, especially when using the native style.
Mostly you have the best chances to update such things by using the qstylesheet style. But thats also sometimes not an option when the native look is needed. -
Well it seems that fusion style
kinda do not use palette and then again, it does affect it.But i never really fooled around with QStyle so not giving up yet.
However, I can also just draw the arrow myself since i can ask for the rect/placement it seems.
So maybe that is how to do it.Update:
Turned out SC_ComboBoxArrow was something else.
So i solved it by using
QApplication::style()->drawPrimitive(QStyle::PE_IndicatorArrowDown,&comboOptions, &painter);
To draw arrow myself.