[Qt-interest] context menu problem

Benjamin ikipou at gmail.com
Fri Dec 5 11:40:05 CET 2008


In fact there is definitely a workaround to do this. You simply have
to create a new QStyle for the menu, and change the color there.
On mac, I subclass QMacStyle and override drawControl:


void GreatStyle::drawControl ( ControlElement element, const
QStyleOption * option, QPainter * painter, const QWidget * widget)
const{
    if(element == QStyle::CE_MenuItem){
        const QStyleOptionMenuItem* menuOption = static_cast<const
QStyleOptionMenuItem*>(option);
        if(menuOption->text == goodAction->text()){
            QStyleOptionMenuItem newMenuOption(*menuOption);
            QPalette newPalette(option->palette);
            newPalette.setColor(QPalette::ButtonText, QColor(255, 0, 0));
            newMenuOption.palette = newPalette;
            QMacStyle::drawControl(element, &newMenuOption, painter, widget);
            return;
        }
    }
    QMacStyle::drawControl(element, option, painter, widget);
}


where goodAction is the action I want in red. I set this new style to the menu:
    menu.setStyle(new RedStyle(importantAction, this));

and this is it! Thanks to Andy Shaw for this great trick.

2008/12/4 Ankit Agarwal <ankit17.ag at gmail.com>:
> Hi,
> I have a context menu with 10 QMenu entries. Now, I want that out of those
> 10 entries, 3 entries should be visible in red and rest in the default
> colour. Please suggest how can i achive this.
>
> --
> Regards,
> Ankit Agarwal
> SW Engineer
> Vegayan Systems
> email : ankit_agarwal at vegayan.com
> Blog : http://ankit17.wordpress.com
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>



More information about the Qt-interest-old mailing list