Solved How to add a separator to the menu bar?
-
# 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 -
Yeah, I know. I've tried it before, but it doesn't work.
-
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; } }
-
@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....
-
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.
-
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 -
@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.