Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. New window not spawning

New window not spawning

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 566 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.
  • F Offline
    F Offline
    Fuchsiaff
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • F Fuchsiaff

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • F Offline
        F Offline
        Fuchsiaff
        wrote on last edited by Fuchsiaff
        #3

        @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

        jsulmJ 1 Reply Last reply
        0
        • F Fuchsiaff

          @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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @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
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          F 1 Reply Last reply
          2
          • jsulmJ jsulm

            @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
            
            F Offline
            F Offline
            Fuchsiaff
            wrote on last edited by
            #5

            @jsulm

            Got it, thanks!

            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