Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Language Bindings
  4. [SOLVED][Python/PyQt] How to change style of QMenuBar?

[SOLVED][Python/PyQt] How to change style of QMenuBar?

Scheduled Pinned Locked Moved Language Bindings
6 Posts 2 Posters 18.7k 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
    needhelp_gh
    wrote on last edited by
    #1

    hi everyone,

    How can I change the style of QMenuBar QMenus (i.e., File, Edit, Help)?

    !http://i44.tinypic.com/mrfzpj.jpg(gui)!

    I did this:

    @self.mainMenu = QtGui.QMenuBar()
    self.mainMenu.setStyleSheet("background-color: rgb(49,49,49); color: rgb(255,255,255); border: 1px solid #000")
    self.fileMenu = self.mainMenu.addMenu("&File")
    self.fileMenu.setStyleSheet("QMenu::item::selected { background-color: rgb(30,30,30) } QMenu { background-color: rgb(0,0,0) }")@

    But as you can see in the snapshot, "File", "Edit", & "Help" QMenus are unaffected...The QActions under each QMenu, on the other hand, get the style applied.

    Any ideas?

    Thanks so much!


    http://abstrusegoose.com/432

    1 Reply Last reply
    0
    • N Offline
      N Offline
      needhelp_gh
      wrote on last edited by
      #2

      I also tried adding a QPalette to the QMenu & QMenubar & same results...

      Please help :)


      http://abstrusegoose.com/432

      1 Reply Last reply
      0
      • jazzycamelJ Offline
        jazzycamelJ Offline
        jazzycamel
        wrote on last edited by
        #3

        I think the stylesheet in the following example should do what you want:

        @
        from PyQt4.QtCore import *
        from PyQt4.QtGui import *

        class MainWindow(QMainWindow):
        def init(self, parent=None, **kwargs):
        QMainWindow.init(self, parent=None, **kwargs)

            self.mainMenu=self.menuBar()
            self.setStyleSheet("""
                QMenuBar {
                    background-color: rgb(49,49,49);
                    color: rgb(255,255,255);
                    border: 1px solid #000;
                }
        
                QMenuBar::item {
                    background-color: rgb(49,49,49);
                    color: rgb(255,255,255);
                }
        
                QMenuBar::item::selected {
                    background-color: rgb(30,30,30);
                }
        
                QMenu {
                    background-color: rgb(49,49,49);
                    color: rgb(255,255,255);
                    border: 1px solid #000;           
                }
        
                QMenu::item::selected {
                    background-color: rgb(30,30,30);
                }
            """)
        
            self.fileMenu=self.mainMenu.addMenu('&File')
            self.fileMenu.addAction(QAction("Action 1", self))
            self.fileMenu.addAction(QAction("Action 2", self))
            self.fileMenu.addAction(QAction("Action 3", self))
        
            self.editMenu=self.mainMenu.addMenu('&Edit')
            self.editMenu.addAction(QAction("Action 1", self))
            self.editMenu.addAction(QAction("Action 2", self))
            self.editMenu.addAction(QAction("Action 3", self))
        
            self.helpMenu=self.mainMenu.addMenu('&Help')
            self.helpMenu.addAction(QAction("Action 1", self))
            self.helpMenu.addAction(QAction("Action 2", self))
            self.helpMenu.addAction(QAction("Action 3", self))
        

        if name=="main":
        from sys import argv, exit

        a=QApplication(argv)
        m=MainWindow()
        m.show()
        m.raise_()
        exit(a.exec_())
        

        @

        The "docs":http://qt-project.org/doc/qt-4.8/stylesheet-reference.html have some really good examples on applying stylesheets just for future reference.

        Hope this helps ;o)

        For the avoidance of doubt:

        1. All my code samples (C++ or Python) are tested before posting
        2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
        1 Reply Last reply
        0
        • N Offline
          N Offline
          needhelp_gh
          wrote on last edited by
          #4

          I am using a QDialog, instead of a QMainWindow. No particular reason, the last developer started it that way. Is there a way for a QDialog?

          If not, I can try to switch it to a QMainWindow since that works as I would like it to.

          Thanks for your help , Jazzy :)


          http://abstrusegoose.com/432

          1 Reply Last reply
          0
          • jazzycamelJ Offline
            jazzycamelJ Offline
            jazzycamel
            wrote on last edited by
            #5

            The stylesheet should be exactly the same, the only thing that will differ is the way in which the menubar is constructed and added to the widget. I think the following should do what you with a QDialog:

            @
            from PyQt4.QtCore import *
            from PyQt4.QtGui import *

            class Dialog(QDialog):
            def init(self, parent=None, **kwargs):
            QDialog.init(self, parent=None, **kwargs)

                l=QVBoxLayout(self)
                l.setContentsMargins(0,0,0,0)
            
                self.mainMenu=QMenuBar(self)
                l.addWidget(self.mainMenu)
            
            
                self.setStyleSheet("""
                    QMenuBar {
                        background-color: rgb(49,49,49);
                        color: rgb(255,255,255);
                        border: 1px solid #000;
                    }
            
                    QMenuBar::item {
                        background-color: rgb(49,49,49);
                        color: rgb(255,255,255);
                    }
            
                    QMenuBar::item::selected {
                        background-color: rgb(30,30,30);
                    }
            
                    QMenu {
                        background-color: rgb(49,49,49);
                        color: rgb(255,255,255);
                        border: 1px solid #000;           
                    }
            
                    QMenu::item::selected {
                        background-color: rgb(30,30,30);
                    }
                """)
            
                self.fileMenu=self.mainMenu.addMenu('&File')
                self.fileMenu.addAction(QAction("Action 1", self))
                self.fileMenu.addAction(QAction("Action 2", self))
                self.fileMenu.addAction(QAction("Action 3", self))
            
                self.editMenu=self.mainMenu.addMenu('&Edit')
                self.editMenu.addAction(QAction("Action 1", self))
                self.editMenu.addAction(QAction("Action 2", self))
                self.editMenu.addAction(QAction("Action 3", self))
            
                self.helpMenu=self.mainMenu.addMenu('&Help')
                self.helpMenu.addAction(QAction("Action 1", self))
                self.helpMenu.addAction(QAction("Action 2", self))
                self.helpMenu.addAction(QAction("Action 3", self))
            
                l.addWidget(QLabel("<Your exciting dialog widgets here>"))
            
                bb=QDialogButtonBox(
                    QDialogButtonBox.Ok|QDialogButtonBox.Cancel,
                    parent=self,
                    accepted=self.accept,
                    rejected=self.reject
                )
                l.addWidget(bb)
            

            if name=="main":
            from sys import argv, exit

            a=QApplication(argv)
            d=Dialog()
            d.show()
            d.raise_()
            exit(a.exec_())
            

            @

            Hope this helps ;o)

            For the avoidance of doubt:

            1. All my code samples (C++ or Python) are tested before posting
            2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
            1 Reply Last reply
            0
            • N Offline
              N Offline
              needhelp_gh
              wrote on last edited by
              #6

              I was missing these parts:

              @
              ...
              l=QVBoxLayout(self)
              l.setContentsMargins(0,0,0,0)
              ...
              l.addWidget(self.mainMenu)
              ...
              @

              Works :) ! Thanks so much!


              http://abstrusegoose.com/432

              1 Reply Last reply
              0

              • Login

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