Results 1 to 6 of 6

Thread: Change background color of one menubar item eg Help in blue

  1. #1
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Change background color of one menubar item eg Help in blue

    Hello All,

    I'm new to QT and I'm stuck at one point.
    I have some of my menu bar items in right side (using this code http://qt-project.org/faq/answer/how...in_the_menubar). And I want the background color of those menu bar items will be blue/some icon.
    Is this possible in QT ? If so then could anyone provide me a way to do so ?

    Thanks in advance.

    --Maulik Patel

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

    Default Re: Change background color of one menubar item eg Help in blue

    you can try something like this:
    Qt Code:
    1. #include <QApplication>
    2. #include <QMainWindow>
    3. #include <QWidgetAction>
    4. #include <QMenuBar>
    5. #include <QMenu>
    6. #include <QLabel>
    7. #include <QStyle>
    8. #include <QDebug>
    9.  
    10. int
    11. main (int argc,
    12. char **argv)
    13. {
    14. QApplication app(argc, argv);
    15. app.setStyleSheet("QLabel#label1 { background-color: blue }" \
    16. "QLabel#label2 { background-color: yellow }");
    17.  
    18.  
    19. QLabel *label1 = new QLabel("Action1");
    20. label1->setObjectName("label1");
    21. QWidgetAction action1(&win);
    22. action1.setDefaultWidget(label1);
    23.  
    24. QLabel *label2 = new QLabel("Action2");
    25. label2->setObjectName("label2");
    26. QWidgetAction action2(&win);
    27. action2.setDefaultWidget(label2);
    28.  
    29. QMenu menu("Edit");
    30. menu.addAction(&action1);
    31. menu.addAction(&action2);
    32.  
    33. win.menuBar()->addMenu(&menu);
    34. win.show();
    35.  
    36. return app.exec();
    37. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Change background color of one menubar item eg Help in blue

    Thanks for the reply. Sorry, but it didnt help.

    Let me describe my problem again. I want to have different colored "Menu Bar Items" not "Menu Item".
    I have some Menu bar items like File, Edit etc in left site.. those will be in default color. And Some in right side say Help.. and that HELP itself should be in blue color(or have some BG image/style) not it's items(help->show help, Help->About).

    Something like this..
    2012-04-05_1341.jpg

    Is this possible ?

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

    Default Re: Change background color of one menubar item eg Help in blue

    This seems to be a hole in Qt. I didn't find a way to do that. Maybe it is possible if you reimplement QMenuBar or QStyle and provide your custom rendering. As a workaround I can propose you to use QToolBar instead of QMenuBar, where you can add labels or button via QWidgetAction and style them like in the sample above. With that approach you will have to display menu manually, but that should not be difficult.

  5. The following user says thank you to mentalmushroom for this useful post:

    maulik_887 (6th April 2012)

  6. #5
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Change background color of one menubar item eg Help in blue

    Okay.. Will try to implement it as you suggested.

  7. #6
    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: Change background color of one menubar item eg Help in blue

    AFAIK you can change background color of all items in the menu bar but not some/specific items
    Qt Code:
    1. QMenuBar:item { background-color: rgb(255, 37, 186); }
    To copy to clipboard, switch view to plain text mode 

    To do what you want you would need to inherit from menu bar and paint it the way you want.

Similar Threads

  1. Replies: 1
    Last Post: 17th August 2010, 16:17
  2. Change frame background color
    By Ishmael in forum Newbie
    Replies: 1
    Last Post: 7th June 2010, 05:38
  3. Change QPushButton Background Color, Qt4.3
    By Rayven in forum Qt Programming
    Replies: 5
    Last Post: 4th July 2009, 07:14
  4. how to change background color of QMenu??
    By anupamgee in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2009, 12:41
  5. how to change QTextEdit background color ?
    By mismael85 in forum Qt Programming
    Replies: 9
    Last Post: 26th June 2008, 22:05

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