PDA

View Full Version : QMenu exclusive items oddity



mentalmushroom
29th March 2012, 16:07
Hello. I've noticed a strange behavior of exclusive menu items. Take a look at the following sample:



#include <QApplication>
#include <QtGui>
#include <QActionGroup>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

QMenu menu;
QObject::connect(&menu, SIGNAL(aboutToHide()), &app, SLOT(quit()));

QActionGroup actionGroup(&menu);
actionGroup.setExclusive(true);

for (int i = 0; i < 3; ++i)
{
QAction *action = actionGroup.addAction(QString("Action %1").arg(i));
action->setCheckable(true);
action->setChecked(i == 1);
}

menu.addActions(actionGroup.actions());
menu.show();

return app.exec();
}


This works as expected and produces the following menu with exclusive item selection list:
7540

Now I want to apply a very basic stylesheet to the menu, just to change a color. Here goes the qss script:


QMenu
{
background-color: grey;
color: #000000;
}


Now apply the stylesheet (only 3 lines of code added):


#include <QApplication>
#include <QtGui>
#include <QActionGroup>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

// apply stylesheet
QFile qssFile(":/stylesheet/style.qss");
qssFile.open(QIODevice::ReadOnly);
app.setStyleSheet(qssFile.readAll());

QMenu menu;
QObject::connect(&menu, SIGNAL(aboutToHide()), &app, SLOT(quit()));

QActionGroup actionGroup(&menu);
actionGroup.setExclusive(true);

for (int i = 0; i < 3; ++i)
{
QAction *action = actionGroup.addAction(QString("Action %1").arg(i));
action->setCheckable(true);
action->setChecked(i == 1);
}

menu.addActions(actionGroup.actions());
menu.show();

return app.exec();
}


Exclusive indicators are changed and look like non-exclusive:
7541

Am I doing something wrong? How can this behavior be fixed?

Spitfire
30th March 2012, 16:29
To avoid that use palette or you'll need to manually style all children AFAIK.