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. Inherited Qt classes programming doubt (newbie)
QtWS25 Last Chance

Inherited Qt classes programming doubt (newbie)

Scheduled Pinned Locked Moved Language Bindings
3 Posts 3 Posters 1.2k Views
  • 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.
  • M Offline
    M Offline
    MSDavid
    wrote on last edited by
    #1

    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):
    QMainWindow.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?

    Thanks
    Dave

    1 Reply Last reply
    0
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      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.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

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

        Hi,

        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.

        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