我有一个 QMenu
作为上下文菜单,如下所示:
Menu
- information_A
- information_B
- information_C
现在我希望将条目 information_B
涂成不同的颜色。我该如何存档?
最佳答案
编辑:我在这篇文章中找到了最佳解决方案: link 在您的情况下,它会很简单:
QMenu contextMenu(this);
QString menuStyle(
"QMenu::item{"
"color: rgb(0, 0, 255);"
"}"
);
contextMenu.setStyleSheet(menuStyle);
有关更多选项和可能性,请查看我上面提供的链接中的答案。
以前的解决方案:
您可以使用 QWidgetAction
而不是 QAction
,并使用您想要的文本和样式表定义您的 QLabel
,然后将其分配给您的 QWidgetAction
。但请记住,您必须调整 QLabel
的 width 和 height,以使其看起来与 相同QAction
可以。
示例代码:
// 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);
关于c++ - QMenu : Set text color for specific QAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29939522/