Results 1 to 2 of 2

Thread: Weird QMenu behavior on Mac

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Weird QMenu behavior on Mac

    Hi!

    When I set palette on QMenu it doesn't seem to be working. The following is the code

    Qt Code:
    1. Selector_Menu::Selector_Menu(QWidget *parent) : QMenu(parent)
    2. {
    3. setMinimumWidth(52);
    4.  
    5. setAutoFillBackground(true);
    6.  
    7. QPalette p = palette();
    8. p.setColor(QPalette::Window, QColor(5, 5, 5));
    9. setPalette(p);
    10. }
    To copy to clipboard, switch view to plain text mode 

    I add actions to this menu from the function where I create this object. Something like this

    Qt Code:
    1. menu = new Selector_Menu(this);
    2. currentAction = menu->addAction("A");
    3. menu->addSeparator();
    4. menu->addAction("B");
    5. menu->addSeparator();
    6. menu->addAction("C");
    To copy to clipboard, switch view to plain text mode 

    Output looks like the Picture1 attached

    When I use stylesheets I get the output which looks like the Picture2 attached

    Qt Code:
    1. RMS_Output_Assignment_Selector_Menu::RMS_Output_Assignment_Selector_Menu(QWidget *parent) : QMenu(parent)
    2. {
    3. setMinimumWidth(52);
    4.  
    5. setAutoFillBackground(true);
    6.  
    7. QString menu_ss = "QMenu { color: white; background-color: black; }" +
    8. QString("QMenu::item:selected { color: grey; background-color: black; }");
    9. setStyleSheet(menu_ss);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Any idea on why is the background not being set properly in the menu. I am using Qt 4.3.1

    Thanks a lot for the help
    Attached Images Attached Images

  2. #2
    Join Date
    Oct 2008
    Location
    Brazil, Sao Paulo - SP
    Posts
    17
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Weird QMenu behavior on Mac

    Hey Munna!

    I've already implemented applications both StyleSheet and Palette based for Mac OS X.

    The truth is: they're both (speacially the Palette approach) buggy on (specially, again) Mac.

    Up to Qt 4.4.3, some QWidgets properties (including palette) propagation has erronous behaviours. This issue seem to be addressed on Qt 4.5. But, as a workaround, you could implement some function like this:
    Qt Code:
    1. void applyPaletteTo(const QPalette &palette, QWidget *widget)
    2. {
    3. widget->setPalette(palette);
    4. foreach (QObject *object, widget->children())
    5. if (QWidget *child = (object->isWidgetType() ? dynamic_cast<QWidget *>(object) : NULL))
    6. applyPaletteTo(palette, child);
    7. }
    To copy to clipboard, switch view to plain text mode 

    and call
    Qt Code:
    1. applyPaletteTo(yourCustomPalette, yourCustomMenu);
    To copy to clipboard, switch view to plain text mode 

    Note that, for this to have effect, it must be done AFTER the widget construction (not the constructor, but the whole 'addSomething' stuff).

    Now for your StyleSheet problem, try the following:
    Qt Code:
    1. QString menu_ss = "QMenu { color: white; background-color: black; }"
    2. "QMenu::item { background-color: black; }"
    3. "QMenu::item:selected { color: grey; background-color: black; }";
    To copy to clipboard, switch view to plain text mode 

    Best regards,
    tinsukE

Similar Threads

  1. Weird behaviour of mouse events and QMenu pop-ups
    By Ishark in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 07:46

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.