[Solved] QComboBox change style
-
Hi,
A QComboBox renders the following output when disabled/enabled
disabled
!http://img209.imageshack.us/img209/5448/discombo.png(disabled_combo)!enabled
!http://img28.imageshack.us/img28/7385/encombo.png(enabled)!Is there any way that I can render the enabled style even when setEnable is false for QComboBox. My requirement is to disable item selection/showPopup for the combobox on a particular condition.
-
Everything is possible, but why do you want to confuse your users by making it look like your combo is enabled when it is not?
-
I have the list that contains processed and unprocessed items with different background color. The user is not allowed to change any entry if item is already processed but i want to render the enabled style for combobox as there are other components that are enabled.
Whereas for unprocessed items the user is allowed to make changes as per requirement.
-
It still sounds like a Bad Idea(TM) to me. However, one way is to use a proxy style on your combo box and simply manipulate the appropriate flag. Something like this should work:
@
class EnabledStyle: public QProxyStyle {
public:
virtual void drawComplexControl ( ComplexControl control, const QStyleOptionComplex * option, QPainter * painter, const QWidget * widget = 0 ) const;
}void EnabledStyle::drawComplexControl ( ComplexControl control, const QStyleOptionComplex * option, QPainter * painter, const QWidget * widget ) const
{
if (control == CC_ComboBox && option) {
QStyleOptionComboBox enabledOption(*option);
enabledOption.state |= QStyle::State_Enabled;
QProxyStyle::drawComplexControl(control, &enabledOption, painter, widget);
} else {
//simply pass through
QProxyStyle::drawComplexControl(control, option, painter, widget);
}
}
@ -
bq. It still sounds like a Bad Idea™ to me.
Thats's the specification I got :)
@void EnabledStyle::drawComplexControl(QStyle::ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
{
if (control == CC_ComboBox && option)
{
QStyleOptionComboBox enabledOption;
enabledOption.palette = option->palette;
enabledOption.rect = option->rect;
enabledOption.state |= QStyle::State_Enabled;
painter->setPen(QColor(Qt::black));QProxyStyle::drawComplexControl(control, &enabledOption, painter, widget); } else { //simply pass through QProxyStyle::drawComplexControl(control, option, painter, widget); }
}@
The code works perfect , I owe you a beer :)
Thanks.