QT: How to trigger "bold" upon mousehover of QMenuBar's items

Viewed 128

I am using Qt6 to create a cross-platform GUI application. I am playing around with the automatically generated QMenuBar object in Designer Mode. I want to change the QMenuBar items (File, Edit, Help.. ) to bold and red.

I am using the following code in the StyleSheet editor of the MainWindow object:

QMenuBar::item:selected {

    font:bold;
    color:red;

}

The code works fine for changing the color of the text but it does not set the font to "bold" as I would like. Also, trying to change the font size does not work.

What am I missing?

Thank you in advance!!

1 Answers

QMenubar does not have a font, you need to give the font separately to QAction. I think this will solve the problem

 QMenu *fileMenu ;
 QAction *newAct = new QAction(tr("&New"), this);

 QFont f = newAct->font();
 f.setBold(true);
 newAct->setFont(f);


 fileMenu = menuBar()->addMenu(tr("&File"));
 fileMenu->addAction(newAct);

enter image description here

but if you want an active QAction then you need to

setStyleSheet("QMenu::item:selected {font: bold;}");
Related