yonutix
yonutix

Reputation: 2439

QMenu change background color

I'm trying to change the background color of a QMenu* object it does not work with setStyleSheet(), nor with setPalette().

I read this article but the guy says that I should add this line:

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

I'm not sure what is app, I tried several combinations but it does not work.

Thanks!

Upvotes: 0

Views: 3677

Answers (1)

svlasov
svlasov

Reputation: 10456

You probably forgot to set QMenu's parent:

#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

Upvotes: 2

Related Questions