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. PySide Menu not working
Forum Updated to NodeBB v4.3 + New Features

PySide Menu not working

Scheduled Pinned Locked Moved Language Bindings
3 Posts 2 Posters 4.2k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    SDrake
    wrote on last edited by
    #1

    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 MW

    if 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 TB

    class 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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SDrake
      wrote on last edited by
      #2

      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 ToolBar

      class 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.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        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