Congratulations to our 2022 Qt Champions!

Inherited Qt classes programming doubt (newbie)

  • Hi!
    I'm quite new to Python and I'm just approaching Python and Pyside GUI programming with Qt.
    I read many PySide tutorial, either on line or printed, and I wonder why in every code example, a new class (inheriting the Qt class) is defined for every used widget.

    For example:

    @class MainWindow(QMainWindow):
    def init(self):
    self.setWindowTitle("Main Window")
    self.setGeometry(300, 250, 400, 300)
    mainWindow = MainWindow()

    I would use the following, instead:

    @mainWindow = QMainWindow()
    mainWindow.setWindowTitle("Main Window")
    mainWindow.setGeometry(300, 250, 400, 300)@

    Why don't just use Qt classes as is ?
    I'm not new to the OOP paradigm, but I need to build a very small application; I don't need to extend Qt base classes and I would like to keep my code simple and clean.

    Can some one give me hints, suggestions, or show me some valid online tutorial?


  • Moderators

    Hi, and welcome to the Qt Dev Net!

    QMainWindow is empty. It has no menu items, no buttons, no text fields, etc.

    I don't know about Python, but in C++ the usual way to use QMainWindow is to subclass it to add the elements you want. For example, you add a button to your subclassed QMainWindow, and add a method to make your subclass handle button clicks.

  • Lifetime Qt Champion


    To add to JKSH, the same principles applies to Python. This way you keep your code clean by having your MainWindow related code inside the class. So It's not your application main function that is responsible for the MainWindow setup. It will also make your code easily maintainable.

Log in to reply