QMenu: Set text color for specific QAction

て烟熏妆下的殇ゞ 提交于 2019-12-11 03:28:56

问题


I have a QMenu as context menu that looks like this:

Menu
- information_A
- information_B
- information_C

Now i want the entry information_B to be painted in a different color. How can i archive this?


回答1:


EDIT: I found the best solution in this post: link In your case it would be as simple as:

QMenu contextMenu(this);
QString menuStyle(
        "QMenu::item{"      
        "color: rgb(0, 0, 255);"
        "}"
    );
contextMenu.setStyleSheet(menuStyle);

For more options and possibilities take a look at the answer in the link I provided above.

PREVIOUS SOLUTION:
You can use QWidgetAction instead of QAction, and define your QLabel with text and stylesheet you want, and then assign it to your QWidgetAction. But keep in mind that you have to tweak the width and height of your QLabel, in order for it to look the same as QAction does.

Sample code:

// label
QLabel *text = new QLabel(QString("your text here"), this);
text->setStyleSheet("color: blue"); 
// init widget action
QWidgetAction *widAct= new QWidgetAction(this);
widAct->setDefaultWidget(text);
contextMenu.addAction(widAct);


来源:https://stackoverflow.com/questions/29939522/qmenu-set-text-color-for-specific-qaction

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!