Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PySide6 - how to properly display central widget
Forum Updated to NodeBB v4.3 + New Features

PySide6 - how to properly display central widget

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 2 Posters 3.5k 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.
  • T Offline
    T Offline
    tilz0R
    wrote on last edited by
    #1

    Tools and SW:

    • python: 3.10.5
    • QTCreator 9.0.0 Community
    • PySide 6.4.1

    I have this setup in QTCreator:
    49c5e99f-0d23-4541-abb2-c7e8462db27c-image.png

    My code starts with:

    class MainApp():
        def __init__(self, qapp = None, qargs = None):
            self.ui = MainWindow(self)
            self.ui.show()
    
    if __name__ == '__main__':
        faulthandler.enable()
        app = QApplication(sys.argv)
        mainapp = MainApp(qapp = app, qargs = sys.argv)
        sys.exit(app.exec())
    

    I have a custom MainWindow class, that extends QMainWindow.

    # Main window class
    class MainWindow(QMainWindow):
        def __init__(self, app):
            super().__init__()
            self.app = app
            self.load_ui() # This is default code from Qt to load .UI file
            self.load_widgets() # Loads widgets to memory
    
            # This is where the funny code comes
    
        # Loads widgets from UI to fw
        def load_widgets(self):
            self.w_top_widget = self.findChildren(QMainWindow, 'MainWindow')[0]
            self.w_top_central_widget = self.findChildren(QWidget, 'centralwidget')[0]
            # Other widgets...
    

    In the above example, you can find the # This is where the funny code comes where I am facing the issue.
    Running the code as is, I only see empty screen.

    7e48fcf2-5129-47cc-b586-319d149ecb56-image.png


    If I add the code (to the placeholder part), then I get 2 screens

    # This is where the funny code comes
    self.centralWidget().show()
    

    c0ce2c2e-2d24-4091-965f-71b08530ceb2-image.png


    If I add code below, I again receive empty screen

    # This is where the funny code comes
    self.setCentralWidget(self.w_top_widget)
    

    If I set central widget the one that is part of top-top widget, I get screen, but no menu and status bar.

    # This is where the funny code comes
    #self.setCentralWidget(self.w_top_widget)
    self.setCentralWidget(self.w_top_central_widget)
    

    No menu:
    cafda57d-dc58-478e-a118-677a05c872e4-image.png


    How should I properly set central widget to show one window only, with top menu and statusbar, and with windows toolbar?

    One more thing. If I do not call self.ui.show() after MainWindow, then window properly pops up, but there is no windows taskbar icon.

    The code

    class MainApp():
        def __init__(self, qapp = None, qargs = None):
            self.ui = MainWindow(self)
            # Not calling this, project window will properly show, but no windows taskbar (Windows 11)
            #self.ui.show()
    
    class MainWindow(QMainWindow):
        def __init__(self, app):
            super().__init__()
            self.app = app
            self.load_ui()
            self.load_widgets()
    
            self.setWindowTitle('MainWindow')
            self.resize(1280, 800)
    
            # This is where the funny code comes
            self.setCentralWidget(self.w_top_widget)
            self.w_top_widget.show()
    
        def load_widgets(self):
            self.w_top_widget = self.findChildren(QMainWindow, 'MainWindow')[0]
            self.w_top_central_widget = self.findChildren(QWidget, 'centralwidget')[0]
    

    produces following (no taskbar icon)
    41b199de-7019-4825-b3ab-741c793a4bed-image.png

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

      Hi,

      What is the point of the MainApp class ?
      Why is w_top_widget a QMainWindow ?
      Can you provide a schema of the design you want to achieve ?

      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
      • T Offline
        T Offline
        tilz0R
        wrote on last edited by tilz0R
        #3

        Next to the GUI, project includes some other functionality too. I'm in the Qt learning process, I simply thought it might be better to separate true UI activity from the rest of lower-level parts.

        MainApp: Class that initializes specific DLL for low-level communication with serial port and starts UI
        MainWindow: Is an extension of QMainWindow, a top-view class that was set by QtCreator when I started new project.

        w_top_widget is a very top widget in the UI.

        Not sure I fully understand the "schema of the design". You mean UI file or application design structure?

        Or better to ask - what is the correct way of doing it?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tilz0R
          wrote on last edited by
          #4

          I started a new project in QtCreator 9.0.0 Community, with dynamic UI load:
          04091cfe-2ab1-4893-8978-b5d7ed9eec85-image.png

          Adding button and vertical orientation of the central widget, default generated Qt code won't show anything on screen.

          I made a video, that first creates new project (unfortunately Win screen capture did not shot wizard). A window only shows when I add more code. And in some cases I receive 2 windows, in some cases only one (but then no windows taskbar icon..).

          https://www.youtube.com/watch?v=S7HTFvJCQJ8

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tilz0R
            wrote on last edited by
            #5

            Any luck from anyone?

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

              Sorry, I lost track of this thread.

              Even though you generated the project, can you post the files content here so we can be sure people are testing the same code you have ?

              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