Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Inherited Qt classes programming doubt (newbie)

    Language Bindings
    3
    3
    996
    Loading More Posts
    • 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
      MSDavid last edited by

      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 Reply Quote 0
      • JKSH
        JKSH Moderators last edited by

        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 Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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 Reply Quote 0
          • First post
            Last post