Hamzah Akhtar
Hamzah Akhtar

Reputation: 525

How do I select items in MenuBar (PyQt/Python)

I have created the menu bar shown below as part of a QMainWindow Class and i wanted to run another class or def when i click on 'Save as...' in the Menubar. How could i edit the code below to allow me to do that?? By The way, when i click on Quit, it works (it closes the MainWindow).

def createMenusAndToolbars(self):
        fileMenu = self.menuBar().addMenu("File")
        for text in (("Save As..."), ("Quit")):
            action = QtGui.QAction(text, self)
            if text == "Save As...":
                text.clicked.connect(self.save)
            if text == "Quit":
                self.connect(action, QtCore.SIGNAL("triggered()"), self.close)
            fileMenu.addAction(action)
def save(self):
    save = SaveTest(self)

Upvotes: 0

Views: 3948

Answers (1)

ekhumoro
ekhumoro

Reputation: 120568

The code you posted is essentially correct, except for one obviously wrong line. Here's a simplified version which should work as you intended:

def createMenusAndToolbars(self):
    fileMenu = self.menuBar().addMenu('File')
    fileMenu.addAction('Save As...', self.save)
    fileMenu.addAction('Quit', self.quit)

def save(self):
    save = SaveTest(self)

NB: the addAction method returns the action it creates, which would allow you to set other properties, if necessary.

Upvotes: 2

Related Questions