我正在尝试更改一个QMenu*对象的背景色,它不适用于setStyleSheet(),也不适用于setPalette()。
我读了this article,但那家伙说我应该加上这一行:
app.setStyle(QStyleFactory::create("fusion"));我不确定什么是app,我尝试了几种组合,但都不起作用。
谢谢!
发布于 2015-03-31 23:51:04
您可能忘了设置QMenu的父元素:
#include <QtGui>
class Window : public QWidget
{
public:
Window(QWidget *parent = 0) : QWidget(parent) {}
void contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(new QAction("Item 1", this));
menu.addAction(new QAction("Item 2", this));
menu.exec(event->pos());
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Window *window = new Window;
window->setStyleSheet("QMenu::item:selected { background-color: green; }");
window->show();
return app.exec();
}

https://stackoverflow.com/questions/29372170
复制相似问题