I'm running into a strange one here. I have a QMenuBar attached to a QMainWindow with multiple QMenus and QActions contained in it. When the program is first started, and one of the menus is clicked, the resulting drop-down is too short. But if the menu is opened a second time, it's just fine, and looks as expected as the program continues to be used. But, if the program is restarted, the first click on the menu results in a too-short drop-down again.
First click:
Subsequent clicks:
This occurs on all of the menus (File, Edit, Record, and Help). Anyone run into this before and know why this is happening?
Here's some of the pertinent code:
The QMainWindow:
class Win(QMainWindow):
from PySide6.QtGui import QCloseEvent
gui = None
def __init__(self, gui):
QMainWindow.__init__(self)
self.gui = gui
self.setWindowTitle('Sermon Prep Database')
self.setStyleSheet('background-color: white')
self.resize(1000, 800)
def closeEvent(self, event:QCloseEvent) -> None:
event.ignore()
self.gui.doExit()
def keyPressEvent(self, event:QKeyEvent):
if event..modifiers() & Qt.ControlModifier and event.key() == Qt.Key_B:
self.gui.setBold()
..lotsa different handlers...
event.accept()
Then, from GUI(), the Menubar is created like this:
def buildMenuBar(self):
menubar = self.win.menuBar() #win is the instance of the Win() class
menubar.setStyleSheet('QMenuBar::item:selected { background: WhiteSmoke; }')
filemenu = menubar.addMenu('File')
filemenu.setStyleSheet('QMenu::item:selected { background: WhiteSmoke; color: black}')
saveaction = filemenu.addAction('Save (Ctrl-S)')
saveaction.triggered.connect(self.spd.saveRec)
printaction = filemenu.addAction('Print (Ctrl-P)')
printaction.setStatusTip('Print the current record')
printaction.triggered.connect(self.printRec)
backupaction = filemenu.addAction('Create Backup')
backupaction.setStatusTip('Manually save a backup of your database')
backupaction.triggered.connect(self.dobackup)
filemenu.addSeparator()
exitaction = filemenu.addAction('Exit (Ctrl-Q)')
exitaction.setStatusTip('Quit the program')
exitaction.triggered.connect(self.doExit)
... and more items...
This is in Windows 11, by the way. I haven't tried it on any other platforms.
Edit: Here is a minimum reproduceable example that shows the same behavior: impor
import sys
from PySide6.QtWidgets import QApplication, QBoxLayout, QWidget, QMainWindow
class SermonPrepDatabase:
gui = None
def __init__(self):
self.startup()
def startup(self):
self.app = QApplication(sys.argv)
self.gui = GUI(self)
self.gui.initComponents()
self.gui.win.show()
sys.exit(self.app.exec())
class GUI():
accentColor = '#202050'
backgroundColor = '#f0f0ff'
spd = None
win = None
def __init__(self, spd):
self.spd = spd
def initComponents(self):
self.win = Win(self)
self.layout = QBoxLayout(QBoxLayout.TopToBottom)
self.mainwidget = QWidget()
self.mainwidget.setStyleSheet('background-color: ' + self.backgroundColor + ';')
self.win.setCentralWidget(self.mainwidget)
self.mainwidget.setLayout(self.layout)
self.buildMenuBar()
def buildMenuBar(self):
menubar = self.win.menuBar()
menubar.setStyleSheet('QMenuBar::item:selected { background: WhiteSmoke; }')
filemenu = menubar.addMenu('File')
filemenu.setStyleSheet('QMenu::item:selected { background: WhiteSmoke; color: black; }')
saveaction = filemenu.addAction('Save (Ctrl-S)')
saveaction.triggered.connect(self.afunction)
printaction = filemenu.addAction('Print (Ctrl-P)')
printaction.setStatusTip('Print the current record')
printaction.triggered.connect(self.afunction)
backupaction = filemenu.addAction('Create Backup')
backupaction.setStatusTip('Manually save a backup of your database')
backupaction.triggered.connect(self.afunction)
filemenu.addSeparator()
exitaction = filemenu.addAction('Exit (Ctrl-Q)')
exitaction.setStatusTip('Quit the program')
exitaction.triggered.connect(self.afunction)
editmenu = menubar.addMenu('Edit')
editmenu.setStyleSheet('QMenu::item:selected { background: WhiteSmoke; color: black}')
cutaction = editmenu.addAction('Cut (Ctrl-X)')
cutaction.triggered.connect(self.afunction)
copyaction = editmenu.addAction('Copy (Ctrl-C)')
copyaction.triggered.connect(self.afunction)
pasteaction = editmenu.addAction('Paste (Ctrl-V)')
pasteaction.triggered.connect(self.afunction)
configmenu = editmenu.addMenu('Configure')
bgColorAction = configmenu.addAction('Change Accent Color')
bgColorAction.setStatusTip('Choose a different color for accents and borders')
bgColorAction.triggered.connect(self.afunction)
fgColorAction = configmenu.addAction('Change Background Color')
fgColorAction.setStatusTip('Choose a different color for the background')
fgColorAction.triggered.connect(self.afunction)
renameAction = configmenu.addAction('Rename Labels')
renameAction.setStatusTip('Rename the labels in this program')
renameAction.triggered.connect(self.afunction)
fontaction = configmenu.addAction('Change Font')
fontaction.setStatusTip(('Change the font and font size used in the program'))
fontaction.triggered.connect(self.afunction)
recordmenu = menubar.addMenu('Record')
recordmenu.setStyleSheet('QMenu::item:selected { background: WhiteSmoke; color: black}')
firstrecaction = recordmenu.addAction('Jump to First Record')
firstrecaction.triggered.connect(self.afunction)
prevrecaction = recordmenu.addAction('Go to Previous Record')
prevrecaction.triggered.connect(self.afunction)
nextrecaction = recordmenu.addAction('Go to Next Record')
nextrecaction.triggered.connect(self.afunction)
lastrecaction = recordmenu.addAction('Jump to Last Record')
lastrecaction.triggered.connect(self.afunction)
recordmenu.addSeparator()
newrecaction = recordmenu.addAction('Create New Record')
newrecaction.triggered.connect(self.afunction)
delrecaction = recordmenu.addAction('Delete Current Record')
delrecaction.triggered.connect(self.afunction)
def afunction(self):
pass
class Win(QMainWindow):
from PySide6.QtGui import QCloseEvent
gui = None
def __init__(self, gui):
QMainWindow.__init__(self)
self.gui = gui
self.setWindowTitle('Sermon Prep Database')
self.setStyleSheet('background-color: white')
self.move(50, 50)
self.resize(1000, 800)
def closeEvent(self, event:QCloseEvent) -> None:
event.ignore()
self.doExit()
def doExit(self):
sys.exit(0)
SermonPrepDatabase()