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



  • 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



  • 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)
    


  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.