How to add a separator to the menu bar?
-
wrote on 9 Sept 2020, 13:17 last edited by
# Menu: View menuView = self.menuBar().addMenu('View') ... # Separator menuSeparator = self.menuBar().addMenu('|') menuSeparator.setEnabled(False) # Menu: Help menuHelp = self.menuBar().addMenu('Help') ...
Is this the only way to add a separator to the menu bar?
Thanks
-
# Menu: View menuView = self.menuBar().addMenu('View') ... # Separator menuSeparator = self.menuBar().addMenu('|') menuSeparator.setEnabled(False) # Menu: Help menuHelp = self.menuBar().addMenu('Help') ...
Is this the only way to add a separator to the menu bar?
Thanks
@NonNT
As far as I know QMenuBar has an addSeparator function.
https://doc.qt.io/qt-5/qmenubar.html#addSeparator -
wrote on 9 Sept 2020, 14:11 last edited by
Yeah, I know. I've tried it before, but it doesn't work.
-
wrote on 9 Sept 2020, 14:19 last edited by
KDevelop uses separators in the menu bar and they use actions for that
kdevelop/kdevplatform/shell/mainwindow.cpp:
//KDevelop needs to ensure that separators defined as <Separator style="visible" /> //are always shown in the menubar. For those, we create special disabled actions //instead of calling QMenuBar::addSeparator() because menubar separators are ignored if (element.tagName().compare(QLatin1String("separator"), Qt::CaseInsensitive) == 0 && element.attribute(QStringLiteral("style")) == QLatin1String("visible")) { if ( auto* bar = qobject_cast<QMenuBar*>( parent ) ) { auto* separatorAction = new QAction(QStringLiteral("|"), this); bar->insertAction( before, separatorAction ); separatorAction->setDisabled(true); return separatorAction; } }
-
wrote on 9 Sept 2020, 14:20 last edited by
@NonNT said in How to add a separator to the menu bar?:
Yeah, I know. I've tried it before, but it doesn't work.
According to https://www.qtcentre.org/threads/13910-why-addSeparator()-doesn-t-work-on-QMenuBar, from 2008(!), this depends on the style you use....
If KDevelop does their workaround, I guess that's the way you should go....
-
wrote on 9 Sept 2020, 18:04 last edited by
Thanks
I use KDE Breeze.
And
QAction('|', self)
is the better choice thanaddMenu('|')
because in a vertical menuBar deactivated QAction disappear whileaddMenu('|')
doesn't look so nice.But in a horizontal menuBar both look the same.
-
wrote on 10 Sept 2020, 14:46 last edited by NonNT 9 Oct 2020, 14:47
I still found this:
We create the Tools, Options, and Help menus in a similar fashion. We insert a separator between the Options and Help menus. In Motif and CDE styles, the separator pushes the Help menu to the right; in other styles, the separator is ignored. Figure 3.5 shows both cases.
C++ GUI Programming with Qt4, 2nd Edition
https://www.informit.com/articles/article.aspx?p=1405225&seqNum=2 -
I still found this:
We create the Tools, Options, and Help menus in a similar fashion. We insert a separator between the Options and Help menus. In Motif and CDE styles, the separator pushes the Help menu to the right; in other styles, the separator is ignored. Figure 3.5 shows both cases.
C++ GUI Programming with Qt4, 2nd Edition
https://www.informit.com/articles/article.aspx?p=1405225&seqNum=2wrote on 10 Sept 2020, 17:49 last edited by@NonNT said in How to add a separator to the menu bar?:
In Motif and CDE styles, the separator pushes the Help menu to the right;
in other styles, the separator is ignored. Figure 3.5 shows both cases.That is why I pointed you at https://www.qtcentre.org/threads/13910-why-addSeparator()-doesn-t-work-on-QMenuBar and said it depends on the style you use.
3/8