PySide Menu not working
-
I'm trying to create a simple dropdown menu on a menubar with PySide, using Idle for editing. Have created a basic Window with menubar, on it one menu -- "File", and one action beneath it -- "New...". Whenever the script is run (either through Idle or Python) the window appears as configured but "File" does absolutely nothing. I.e. it will not dropdown and display "New...". Nor does it give any indication mouse is pointing to it or has been clicked.
Python version is 3.3.2. PySide is 1.2.0 for Windows 3.3, AMD64, with Mingw compiler. Computer uses Windows 7 os... if any of that is useful.
The script is launched with:
@#!/usr/bin/env python3
import sys, time
from PySide.QtGui import *
from PySide.QtCore import *
from ProjectInformation import *
from MainWindow import MainWindow as MWif name=='main':
app = QApplication(sys.argv)
window = MW()
window.show()
sys.exit(app.exec_())@MW is:
@#!/usr/bin/env python3
import sys, time
from PySide.QtGui import *
from PySide.QtCore import *
#from ProjectInformation import *
from MenuBar import MenuBar as MB
#from ToolBar import ToolBar as TBclass MainWindow(QMainWindow):
def init(self, parent=None):
QMainWindow.init(self, parent)
#QMainWindow.setWindowTitle(self, get_Project_Name_and_Version())self.MB = MB(self) QMainWindow.setMenuBar(self, self.MB) #self.TB = TB(self) #QMainWindow.addToolBar(self, self.TB) self.centralwidget = QWidget(self)@
and finally the menubar is defined with:
@from PySide.QtGui import *
from PySide.QtCore import *class MenuBar(QMenuBar):
def init(self, parent=None):
QMenuBar.init(self, parent)self.newFileQAction = QAction(self) self.newFileQAction.text = 'New...' self.newFileQAction.triggered.connect(self.newFileAction) self.newFileQMenu = QMenu('&File') self.newFileQMenu.addAction(self.newFileQAction) self.newFileMenu = self.addMenu(self.newFileQMenu) #self.addMenu('&Data') #self.addMenu('&Edit') #self.addMenu('&Windows') #self.addMenu('&About') def newFileAction(self): print("A") # just a print event to see if the function is called by the menu action above@
I've been trying to figure out why nothing is happening but with no results. Checked several pyside menu tutorials and seem to be on the right track but am missing something. Anyone see what I am doing wrong?
Thank You
-
Solved It!
Turns out the last line in the MainWindow class definition
@self.centralwidget = QWidget(self)@was causing the problem. My guess is it prevents the main window from getting fully defined which means the menu 'effects' such as dropping down to display actions are not implemented.
In its' working form, MainWindow class is written as:
@#!/usr/bin/env python3
import sys, time
from PySide.QtGui import *
from PySide.QtCore import *
from ProjectInformation import *
from MenuBar import MenuBar
from ToolBar import ToolBarclass MainWindow(QMainWindow):
def init(self, parent=None):
QMainWindow.init(self, parent)
QMainWindow.setWindowTitle(self, get_Project_Name_and_Version())MB = MenuBar(self) self.setMenuBar(MB) TB = ToolBar(self) self.addToolBar(TB) CW = QWidget(self) self.setCentralWidget(CW)@
and the MenuBar class is written as:
@from PySide.QtGui import *
from PySide.QtCore import *class MenuBar(QMenuBar):
def init(self, parent=None):
QMenuBar.init(self, parent)newFileQAction = QAction(u'New...', self) newFileQAction.triggered.connect(self.newFileAction) newFileMenu = self.addMenu('&File') newFileMenu.addAction(newFileQAction) #self.addMenu('&Data') #self.addMenu('&Edit') #self.addMenu('&Windows') #self.addMenu('&About') def newFileAction(self): print("A")@
I hope this is useful for someone else.
-
Hi,
IIRC from my pythonic days, you were replacing the function centralWidget with you widget object.
As a side note, QMainWindow has a default menu bar that you can use so you don't need to create you own.