Important: Please read the Qt Code of Conduct -

QMenuBar in QMainWindow Styling issue

  • Hi, Im fairly new to Qt and I'm trying to style the QMenuBar in my QMainWindow. However when I add a QMenu to the QMenuBar the styling doesn't seem to follow through. I have tried multiple approaches including style sheets at palettes using examples listed elsewhere. Here is the what the bar looks like currently and some code. Any help as to why this is happening is appreciated.

    ! area under 'File' should also be gradient)!

    mFileMenu is a QMenu
    myMenuBar is a QMenuBar

    mFileMenu = new QMenu(tr("File"));
    QString s1("QMenuBar{ background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,stop:0 darkgrey, stop:1 black); color: white}");

    I then use setMenuBar in my QMainWindow to set myMenuBar the menuBar

  • Please check the image link. cannot see it here.

  • Hi Dark4326

    I tried to fix your link, but seems there is nog image there. Are you sure that you uploaded an image there or is it private?

    "The forum help gives some more assistance":

  • I think I have fixed the image issue, thanks for your replies.

  • In my case, the highlited part of hovered menu item doesn't look like yours. It seems to be relevant to desktop configuration of your OS. In my old windows xp desktop, I can change the background color of 'File' menu when mouse hovering over there via control panel > display > ....
    BTW, do we have a kind of 'transparent' higlighted menu item attribute? such things would be helpful.

  • Though quite old topic, I found how to control the menubar's painting via palette in this case.
    The color of "File" menu while mouse is over it is QPalette::Higlight color, so you just can change it.
    see.. in the qwindowsxpstyles.cpp code

    void QWindowsXPStyle::drawControl(ControlElement element, const QStyleOption *option, QPainter *p,
    const QWidget *widget) const
    case CE_MenuBarItem:
    if (const QStyleOptionMenuItem *mbi = qstyleoption_cast<const QStyleOptionMenuItem *>(option))
    if (mbi->menuItemType == QStyleOptionMenuItem::DefaultItem)

            bool act = mbi->state & State_Selected;
            bool dis = !(mbi->state & State_Enabled);
            QBrush fill = mbi->palette.brush(act ? QPalette::Highlight : QPalette::Button);
            QPalette::ColorRole textRole = dis ? QPalette::Text:
                                           act ? QPalette::HighlightedText : QPalette::B


    The 'fill' brush is the one that make your menubar ugly in your case(but it is due to your desktop configuration not Qt, i think).

    I just did

    QPalette palette = this->menuBar()->palette();
    palette.setBrush(QPalette::Highlight, QBrush(QColor(100,100,100,100)));

    and feel like it is much better in the gradient background.

Log in to reply