Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

New window not spawning



  • So this has always worked for me but now for some reason, when using mousePressEvent() I can't seem to spawn a new window.

    Here's my code for the Window class that is supposed to be shown.

    from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QFrame, QMainWindow
    from PyQt5.QtCore import Qt
    
    
    class Window(QWidget):
    
        def __init__(self, parent=None):
            super().__init__(parent)
    
            self.layout_main = QHBoxLayout()
    
            self.layout_main.addWidget(QPushButton("Terminate"))
    
            self.setLayout(self.layout_main)
    

    And here's the class that is supposed to show that when pressing on it:

    from PyQt5.QtWidgets import QLabel, QHBoxLayout, QFrame
    
    
    class Item(QFrame):
    
        def __init__(self, tootja, toode, jalanumber, kogus, hind, bold):
            super().__init__()
    
            if bold:
                self.setStyleSheet("QLabel { font-weight: bold; font-size: 15;}")
    
            self.layout_main = QHBoxLayout()
    
            self.tootja = tootja
            self.toode = toode
            self.jalanumber = jalanumber
            self.kogus = kogus
            self.hind = hind
    
            self.tootja_label = QLabel(self.tootja)
            self.toote_label = QLabel(self.toode)
            self.jala_label = QLabel(self.jalanumber)
            self.kogus_label = QLabel(self.kogus)
            self.hind_label = QLabel(self.hind)
    
            self.layout_main.addWidget(self.tootja_label)
            self.layout_main.addWidget(self.toote_label)
            self.layout_main.addWidget(self.jala_label)
            self.layout_main.addWidget(self.kogus_label)
            self.layout_main.addWidget(self.hind_label)
    
            self.setLayout(self.layout_main)
    
        def mousePressEvent(self, event):
            from widgets.Popup import Window
    
            l = Window()
            print(l)
            print("Shown")
            l.show()
    

    It does print "Shown" but windows are not appearing for some reason.


  • Lifetime Qt Champion

    @Fuchsiaff It's because l is a local variable and is destroyed as soon as mousePressEvent finishes...



  • @jsulm said in New window not spawning:

    @Fuchsiaff It's because l is a local variable and is destroyed as soon as mousePressEvent finishes...

    But when I added a button that when pressed, executed this code:

    from widgets.Popup import Window
    
            l = Window()
            print(l)
            print("Shown")
            l.show()
    

    Then it still didn't show after I clicked on the button


  • Lifetime Qt Champion

    @Fuchsiaff Again: l is DELETED as soon as the method finishes.
    Either make l member variable or call exec() instead of show()

    def mousePressEvent(self, event):
            from widgets.Popup import Window
    
            l = Window()
            print(l)
            print("Shown")
            l.show()
    // HERE l IS DELETED
    


  • @jsulm

    Got it, thanks!


Log in to reply