[SOLVED][Python/PyQt] How to change style of QMenuBar?
-
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!
-
I also tried adding a QPalette to the QMenu & QMenubar & same results...
Please help :)
-
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, exita=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)
-
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 :)
-
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, exita=QApplication(argv) d=Dialog() d.show() d.raise_() exit(a.exec_())
@
Hope this helps ;o)
-
I was missing these parts:
@
...
l=QVBoxLayout(self)
l.setContentsMargins(0,0,0,0)
...
l.addWidget(self.mainMenu)
...
@Works :) ! Thanks so much!