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. [Advice wanted] Launch (show) widget from button click slot of the main window.

[Advice wanted] Launch (show) widget from button click slot of the main window.

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 697 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.
  • Carlos DiazC Offline
    Carlos DiazC Offline
    Carlos Diaz
    wrote on last edited by Carlos Diaz
    #1

    EDIT:
    See my first reply for a better approach

    Hi,

    I'm using fbs to deploy a test application, i'm trying to open/load a child widget on a slot called when i click a button located on the main window. The test doesn't work, but my main question is: Should i load the ui file of the second widget on a custom class as i first tried?

    #!/usr/bin/env python3
    
    from fbs_runtime.application_context import ApplicationContext
    
    from PySide2.QtCore import *
    from PySide2.QtWidgets import *
    
    from PySide2.QtUiTools import QUiLoader
    
    import sys
    import time
    
    class child_form(QObject, ApplicationContext):
    
        def __init__(self):
            QObject.__init__(self)
    
            self.ui = self.get_resource('child_widget.ui')
            self.window = QFile(self.ui)
            self.window.open(QFile.ReadOnly)
    
    class AppContext(ApplicationContext):
        def run(self):
    
            self.app.setStyle('Fusion')
    
            main_window_ui = self.get_resource('main_window.ui')
            
            self.main_window = QFile(main_window_ui)
            self.main_window.open(QFile.ReadOnly)
    
            # Child form
            self.child_form = child_form()
    
            self.loader = QUiLoader()
            self.window = self.loader.load(self.main_window)
    
            self.window.pushButton.clicked.connect(self.on_push_button_clicked)
    
            self.window.show()
            return self.app.exec_()
    
        @Slot()
        def on_push_button_clicked(self):
            self.ui = self.get_resource('child_widget.ui')
            self.child_window = QFile(self.ui)
            self.child_window.open(QFile.ReadOnly)
            self.child = self.loader.load(self.child_window, self.window)
            self.child.show()
            
            time.sleep(100)
    
    if __name__ == '__main__':
        appctxt = AppContext()
        exit_code = appctxt.run()
        sys.exit(exit_code)
    
    

    Regards

    1 Reply Last reply
    0
    • Carlos DiazC Offline
      Carlos DiazC Offline
      Carlos Diaz
      wrote on last edited by
      #2

      After some thought i can open a second widget from the button clicked slot of the main window, is this the best approach?

      #!/usr/bin/env python3
      
      from fbs_runtime.application_context import ApplicationContext
      
      from PySide2.QtCore import *
      from PySide2.QtWidgets import *
      
      from PySide2.QtUiTools import QUiLoader
      
      import sys
      
      class child_form(QObject):
      
          def __init__(self, ui_resource):
              QObject.__init__(self)
      
              self.ui_file = QFile(ui_resource)
              self.ui_file.open(QFile.ReadOnly)
      
              loader = QUiLoader()
      
              self.window = loader.load(self.ui_file)
              self.ui_file.close()
      
              self.window.pushButton.clicked.connect(self.on_push_button_clicked)
      
          @Slot()
          def on_push_button_clicked(self):
              print('Hi from child')
      
          def show(self):
              self.window.show()
      
      class AppContext(ApplicationContext):
          def run(self):
      
              self.app.setStyle('Fusion')
      
              main_window_ui = self.get_resource('main_window.ui')
      
              loader = QUiLoader()
              # Child form
              child_ui = self.get_resource('child_widget.ui')
              self.child = child_form(child_ui)
              
              self.window_file = QFile(main_window_ui)
              self.window_file.open(QFile.ReadOnly)
      
              self.window = loader.load(self.window_file)
              self.window_file.close()
      
              self.window.pushButton.clicked.connect(self.on_push_button_clicked)
      
              self.window.show()
              return self.app.exec_()
      
          @Slot()
          def on_push_button_clicked(self):
              self.child.show()
      
      if __name__ == '__main__':
          appctxt = AppContext()
          exit_code = appctxt.run()
          sys.exit(exit_code)
      
      1 Reply Last reply
      0
      • DenniD Offline
        DenniD Offline
        Denni
        wrote on last edited by Denni
        #3

        I have a question - with this child window implementation -- why do you not use a QDockWidget along with QMainWindow? Aka why re-invent the wheel so-to-speak

        madness... is like gravity, all takes is a little... push -- like from an unsolvable bug

        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