Results 1 to 4 of 4

Thread: Styling a Single Item on a QMenuBar

  1. #1
    Join Date
    Mar 2011
    Posts
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Styling a Single Item on a QMenuBar

    I already know what you're going to say, so please don't chastise me about user interface consistency/standards. I know this isn't a good technique, but I'm porting an application that was done this way (and no, it wasn't my idea). I'm looking for technical answers about how to do this, not reasons why I shouldn't do it.

    I have a situation where an item on the menu bar is not selectable and doesn't produce a menu. In the existing application, this item is updated to show different brief, status information related to the information currently on display. The background color of the item changes along with the text of the menu item.

    In Qt, creating the empty menu isn't a problem, nor is changing the text, but attempting to style the menu item with setStyleSheet doesn't seem to work, either programmatically or by experimenting manually in QtCreator. My best guess is that the style of the items on the menu bar is controlled by the menu bar and attempts to override that style are ignored. The code line I'm using is below; I've tried both the background and foreground color. Neither seems to have any effect. The style string is generated by the style editor in QtCreator.

    this->ui->menuContext->setStyleSheet ("background-color: rgb(255, 85, 0);\ncolor: rgb(0, 170, 255)");


    If so, that's fine, and I can go to my team and say "hey, we can't do it this way anymore" and I'm fine with that. If not, and I'm doing something wrong technically, at the very least, it would be nice to understand.

    The old application is Motif-based, and this would be the first thing I've come across that I can't do in Qt. I'm not saying that's a bad thing, but I am a bit surprised.

    Thanks,
    Doug

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Styling a Single Item on a QMenuBar

    Maybe you can create a subclass of QMenu, reimplement paintEvent, do all custom painting there and set it to menu bar via QMenuBar::addMenu ?

  3. #3
    Join Date
    Mar 2011
    Posts
    25
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Styling a Single Item on a QMenuBar

    Thanks for the suggestion, but after discussing with a colleague, I've decided to move these items onto a toolbar where they more properly belong.

    Thanks,
    Doug

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Styling a Single Item on a QMenuBar

    I would try to make a custom widget. Widget will contain a menubar and a custom widget(label?) in a horizontal layout. Add this widget in place of your menubar.

Similar Threads

  1. Styling a QAbstractItemView item
    By Berryblue031 in forum Newbie
    Replies: 2
    Last Post: 24th March 2011, 09:25
  2. Styling QMenubar in Qmainwindow in symbian has no effect
    By jaffers in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 30th July 2010, 16:36
  3. Styling QMenubar???
    By anupamgee in forum Qt Programming
    Replies: 5
    Last Post: 29th April 2009, 10:01
  4. Removing the selection of a menu item from QMenuBar
    By febil in forum Qt Programming
    Replies: 2
    Last Post: 18th March 2009, 09:31
  5. Styling each item in a QComboBox
    By rishid in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2008, 09:10

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.