2015-03-31 857 views
0

我试图更改QMenu*对象的背景颜色,它不适用于setStyleSheet()setPalette()QMenu更改背景颜色

我读this article但那家伙说我应该加入这一行:

app.setStyle(QStyleFactory::create("fusion")); 

我不知道什么是app,我尝试了几种组合,但它不工作。

谢谢!

回答

0

你可能忘了设置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(); 
} 

enter image description here