Results 1 to 2 of 2

Thread: QMenu exclusive items oddity

  1. #1
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default QMenu exclusive items oddity

    Hello. I've noticed a strange behavior of exclusive menu items. Take a look at the following sample:

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QActionGroup>
    4.  
    5. int main(int argc, char* argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QMenu menu;
    10. QObject::connect(&menu, SIGNAL(aboutToHide()), &app, SLOT(quit()));
    11.  
    12. QActionGroup actionGroup(&menu);
    13. actionGroup.setExclusive(true);
    14.  
    15. for (int i = 0; i < 3; ++i)
    16. {
    17. QAction *action = actionGroup.addAction(QString("Action %1").arg(i));
    18. action->setCheckable(true);
    19. action->setChecked(i == 1);
    20. }
    21.  
    22. menu.addActions(actionGroup.actions());
    23. menu.show();
    24.  
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    This works as expected and produces the following menu with exclusive item selection list:
    Clipboard01.png

    Now I want to apply a very basic stylesheet to the menu, just to change a color. Here goes the qss script:
    Qt Code:
    1. {
    2. background-color: grey;
    3. color: #000000;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Now apply the stylesheet (only 3 lines of code added):
    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3. #include <QActionGroup>
    4.  
    5. int main(int argc, char* argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. // apply stylesheet
    10. QFile qssFile(":/stylesheet/style.qss");
    11. qssFile.open(QIODevice::ReadOnly);
    12. app.setStyleSheet(qssFile.readAll());
    13.  
    14. QMenu menu;
    15. QObject::connect(&menu, SIGNAL(aboutToHide()), &app, SLOT(quit()));
    16.  
    17. QActionGroup actionGroup(&menu);
    18. actionGroup.setExclusive(true);
    19.  
    20. for (int i = 0; i < 3; ++i)
    21. {
    22. QAction *action = actionGroup.addAction(QString("Action %1").arg(i));
    23. action->setCheckable(true);
    24. action->setChecked(i == 1);
    25. }
    26.  
    27. menu.addActions(actionGroup.actions());
    28. menu.show();
    29.  
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 

    Exclusive indicators are changed and look like non-exclusive:
    Clipboard02.png

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

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QMenu exclusive items oddity

    To avoid that use palette or you'll need to manually style all children AFAIK.

Similar Threads

  1. Replies: 2
    Last Post: 16th February 2012, 10:43
  2. QMenu clear items
    By user_mail07 in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2011, 22:17
  3. Replies: 5
    Last Post: 7th September 2011, 12:59
  4. QTreeView -- Alternating Row Colors Oddity
    By chuckshaw in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 19:41
  5. exclusive checkBox
    By mickey in forum Qt Programming
    Replies: 6
    Last Post: 4th October 2007, 18:26

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.