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. Widget not able to create new widget ?
Forum Updated to NodeBB v4.3 + New Features

Widget not able to create new widget ?

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 655 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.
  • T Offline
    T Offline
    TheJester12121
    wrote on 23 Aug 2024, 07:03 last edited by
    #1

    Hi guys,

    I must be missing something, but what Im trying to do doesnt work.

    See here.

    import sys
    
    from PyQt5.QtCore import QSize
    from PyQt5.QtWidgets import QWidget, QMainWindow, QPushButton, QApplication
    
    class TestApplicationWindow(QWidget):
    
        def __init__(self, *args, **kwargs):
            QWidget.__init__(self, *args, **kwargs)
    
            self.resize(QSize(640, 480))
            btn = QPushButton(self)
            btn.setText("Create window")
    
            btn.clicked.connect(self.create_window)
    
        def create_window(self):
            print("create_window")
    
            otherwindow = QWidget()
            otherwindow.resize(QSize(640, 480))
            otherwindow.show()
    
    if __name__ == "__main__":
    
        print("Starting")
    
        app = QApplication(sys.argv)
    
        MainWindow = TestApplicationWindow()
        MainWindow.show()
    
        sys.exit(app.exec())
    

    Can anyone explain me, why, when I click the button, the 2nd widget (otherwindow) isnt shown ?

    J 1 Reply Last reply 23 Aug 2024, 12:49
    0
    • T Offline
      T Offline
      TheJester12121
      wrote on 23 Aug 2024, 07:57 last edited by
      #2

      Hmmz.... this is it.... https://www.pythonguis.com/tutorials/creating-multiple-windows/
      Its created, but discarded.
      Thanks all !

      1 Reply Last reply
      0
      • T TheJester12121
        23 Aug 2024, 07:03

        Hi guys,

        I must be missing something, but what Im trying to do doesnt work.

        See here.

        import sys
        
        from PyQt5.QtCore import QSize
        from PyQt5.QtWidgets import QWidget, QMainWindow, QPushButton, QApplication
        
        class TestApplicationWindow(QWidget):
        
            def __init__(self, *args, **kwargs):
                QWidget.__init__(self, *args, **kwargs)
        
                self.resize(QSize(640, 480))
                btn = QPushButton(self)
                btn.setText("Create window")
        
                btn.clicked.connect(self.create_window)
        
            def create_window(self):
                print("create_window")
        
                otherwindow = QWidget()
                otherwindow.resize(QSize(640, 480))
                otherwindow.show()
        
        if __name__ == "__main__":
        
            print("Starting")
        
            app = QApplication(sys.argv)
        
            MainWindow = TestApplicationWindow()
            MainWindow.show()
        
            sys.exit(app.exec())
        

        Can anyone explain me, why, when I click the button, the 2nd widget (otherwindow) isnt shown ?

        J Offline
        J Offline
        JonB
        wrote on 23 Aug 2024, 12:49 last edited by JonB
        #3

        @TheJester12121 said in Widget not able to create new widget ?:

        otherwindow = QWidget()

        This widget has no parent, it is not added to another widget directly or (better) on a layout belonging to parent widget. And no reference is kept to it in e.g. self. Hence Python will destroy it on exit from create_window(). Other languages (e.g. C++) would not. I don't know exactly where in https://www.pythonguis.com/tutorials/creating-multiple-windows/ you claim this precise code came from.

        1 Reply Last reply
        0
        • S SGaist has marked this topic as solved on 23 Aug 2024, 19:01

        1/3

        23 Aug 2024, 07:03

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved