Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to add a separator to the menu bar?
Forum Update on Monday, May 27th 2025

How to add a separator to the menu bar?

Scheduled Pinned Locked Moved Solved Qt for Python
qt for python
8 Posts 3 Posters 6.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NonNT
    wrote on last edited by
    #1
    # 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

    J.HilkJ 1 Reply Last reply
    0
    • N NonNT
      # 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

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @NonNT
      As far as I know QMenuBar has an addSeparator function.
      https://doc.qt.io/qt-5/qmenubar.html#addSeparator


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • N Offline
        N Offline
        NonNT
        wrote on last edited by
        #3

        Yeah, I know. I've tried it before, but it doesn't work.

        JonBJ 1 Reply Last reply
        0
        • N Offline
          N Offline
          NonNT
          wrote on last edited by
          #4

          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;
                  }
              }
          
          1 Reply Last reply
          0
          • N NonNT

            Yeah, I know. I've tried it before, but it doesn't work.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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....

            1 Reply Last reply
            0
            • N Offline
              N Offline
              NonNT
              wrote on last edited by
              #6

              Thanks

              I use KDE Breeze.

              And QAction('|', self) is the better choice than addMenu('|') because in a vertical menuBar deactivated QAction disappear while addMenu('|') doesn't look so nice.

              But in a horizontal menuBar both look the same.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                NonNT
                wrote on last edited by NonNT
                #7

                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

                JonBJ 1 Reply Last reply
                0
                • N NonNT

                  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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved