Search code examples
pythonpysidemenubarqwidgetqmainwindow

Menus and toolbar


I am new in programming and I have created a simple application with one class in Python and PySide which manipulates phone bill csv files. Now I want an option for mobile too.

How can I add a menubar, when my class inherits from QWidget? Should I write another class which inherits from QMainWindow and then make an instance of my first class as a central widget? Is this the right way to do this?

class MyWidget(QtGui.QWidget):

    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)
        ....


class MyWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        widget = MyWidget()
        self.setCentralWidget(widget)
        ...

Solution

  • There's no need for a QMainWindow, you can simply create a QMenuBar in your widget.

    class MyWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            super(MyWidget, self).__init__(parent)
            self.menu=QtGui.QMenuBar()
            self.menu.addAction("do something")
            layout=QtGui.QVBoxLayout()
            layout.addWidget(self.menu)
    

    A QMainWindow is basically a widget which already has a layout with a menu bar, a toolbar, a status bar, etc. If you don't need all of those functionality, you can use a simple QWidget and add only what you want.