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. Help overwriting a PyQT widget class and adding a new property
Forum Updated to NodeBB v4.3 + New Features

Help overwriting a PyQT widget class and adding a new property

Scheduled Pinned Locked Moved Solved Qt for Python
11 Posts 3 Posters 1.9k 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.
  • M Offline
    M Offline
    Mike Taco
    wrote on last edited by
    #1

    I'm trying to create my own "ComboBox" class that inherits the QCombobox widget so I can overwrite and specifically control the pop up events. When I do that alone, the app works fine. However, when I try to add the ability to pass in a property(position) to the ComboBox class, the window opens, but the widget does not show within. I believe it has something to do with my init constructor and how I'm inheriting things incorrectly, but I don't truly know for sure. Any help?

    from PyQt5 import QtCore, QtWidgets,QtGui
    
    class ComboBox(QtWidgets.QComboBox):
        popupAboutToBeShown = QtCore.pyqtSignal()
    
        def __init__(self,position):
            super(QtWidgets.QComboBox,self).__init__()
            self.position = position
    
        def showPopup(self):
            self.setGeometry(QtCore.QRect(10, 10, 480, 41))
            self.popupAboutToBeShown.emit()
            super(ComboBox, self).showPopup()
    
        def hidePopup(self):
            self.setGeometry(QtCore.QRect(10, 10, 21, 41))
            self.popupAboutToBeShown.emit()
            super(ComboBox, self).hidePopup()
    
    class Window(QtWidgets.QWidget):
        def __init__(self):
            super(Window, self).__init__()
            self.resize(916, 916)
            self.combo = ComboBox(9)
            self.combo.setGeometry(QtCore.QRect(10, 10, 21, 41))
            self.combo.addItems(["ITEM 1","ITEM 2","ITEM 3"])
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        window = Window()
        window.show()
        sys.exit(app.exec_())
    eyllanescE JonBJ 2 Replies Last reply
    0
    • M Mike Taco

      I'm trying to create my own "ComboBox" class that inherits the QCombobox widget so I can overwrite and specifically control the pop up events. When I do that alone, the app works fine. However, when I try to add the ability to pass in a property(position) to the ComboBox class, the window opens, but the widget does not show within. I believe it has something to do with my init constructor and how I'm inheriting things incorrectly, but I don't truly know for sure. Any help?

      from PyQt5 import QtCore, QtWidgets,QtGui
      
      class ComboBox(QtWidgets.QComboBox):
          popupAboutToBeShown = QtCore.pyqtSignal()
      
          def __init__(self,position):
              super(QtWidgets.QComboBox,self).__init__()
              self.position = position
      
          def showPopup(self):
              self.setGeometry(QtCore.QRect(10, 10, 480, 41))
              self.popupAboutToBeShown.emit()
              super(ComboBox, self).showPopup()
      
          def hidePopup(self):
              self.setGeometry(QtCore.QRect(10, 10, 21, 41))
              self.popupAboutToBeShown.emit()
              super(ComboBox, self).hidePopup()
      
      class Window(QtWidgets.QWidget):
          def __init__(self):
              super(Window, self).__init__()
              self.resize(916, 916)
              self.combo = ComboBox(9)
              self.combo.setGeometry(QtCore.QRect(10, 10, 21, 41))
              self.combo.addItems(["ITEM 1","ITEM 2","ITEM 3"])
      
      if __name__ == '__main__':
          import sys
          app = QtWidgets.QApplication(sys.argv)
          window = Window()
          window.show()
          sys.exit(app.exec_())
      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @Mike-Taco I don't understand your question or what you want to get. "position" you just store it in a variable and nothing else. What do you want to get?

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      M 1 Reply Last reply
      1
      • M Mike Taco

        I'm trying to create my own "ComboBox" class that inherits the QCombobox widget so I can overwrite and specifically control the pop up events. When I do that alone, the app works fine. However, when I try to add the ability to pass in a property(position) to the ComboBox class, the window opens, but the widget does not show within. I believe it has something to do with my init constructor and how I'm inheriting things incorrectly, but I don't truly know for sure. Any help?

        from PyQt5 import QtCore, QtWidgets,QtGui
        
        class ComboBox(QtWidgets.QComboBox):
            popupAboutToBeShown = QtCore.pyqtSignal()
        
            def __init__(self,position):
                super(QtWidgets.QComboBox,self).__init__()
                self.position = position
        
            def showPopup(self):
                self.setGeometry(QtCore.QRect(10, 10, 480, 41))
                self.popupAboutToBeShown.emit()
                super(ComboBox, self).showPopup()
        
            def hidePopup(self):
                self.setGeometry(QtCore.QRect(10, 10, 21, 41))
                self.popupAboutToBeShown.emit()
                super(ComboBox, self).hidePopup()
        
        class Window(QtWidgets.QWidget):
            def __init__(self):
                super(Window, self).__init__()
                self.resize(916, 916)
                self.combo = ComboBox(9)
                self.combo.setGeometry(QtCore.QRect(10, 10, 21, 41))
                self.combo.addItems(["ITEM 1","ITEM 2","ITEM 3"])
        
        if __name__ == '__main__':
            import sys
            app = QtWidgets.QApplication(sys.argv)
            window = Window()
            window.show()
            sys.exit(app.exec_())
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Mike-Taco
        Are you really sure this has anything to do with your position parameter at all? You simply do not use it. If you remove that parameter (and self.position = position), and do not pass it from caller, are you saying this code does work?

        I'm confused by your/Python's use of super(ComboBox, self).... If you change all of your super(..., self)s to plain super() does it work?

        Normally, a QWidget like QComboBox takes an extra optional parameter of a QWidget for the parent. Just maybe there is some issue with your alternative definition of the second parameter. Does declaring it like

        def __init__(self, position, parent=None):
        

        make any difference?

        M 1 Reply Last reply
        1
        • JonBJ JonB

          @Mike-Taco
          Are you really sure this has anything to do with your position parameter at all? You simply do not use it. If you remove that parameter (and self.position = position), and do not pass it from caller, are you saying this code does work?

          I'm confused by your/Python's use of super(ComboBox, self).... If you change all of your super(..., self)s to plain super() does it work?

          Normally, a QWidget like QComboBox takes an extra optional parameter of a QWidget for the parent. Just maybe there is some issue with your alternative definition of the second parameter. Does declaring it like

          def __init__(self, position, parent=None):
          

          make any difference?

          M Offline
          M Offline
          Mike Taco
          wrote on last edited by
          #4

          @JonB Sorry for not clarifying. I am not currently using the position parameter in the above code, but I want to be able to in the future . I also go as far as pass the number 9 to combo when I instantiate the object in the window class init. Unfortunately, changing to

          def __init__(self, position, parent=None):
          

          does not work, nor does removing any parameters from super

          super().__init__()
          

          I've also tried other variations including

          def __init__(self, position, parent=None,*args, **kwargs):  
                  super().__init__(*args, **kwargs)
                  self.position = position
          
          JonBJ 1 Reply Last reply
          0
          • M Mike Taco

            @JonB Sorry for not clarifying. I am not currently using the position parameter in the above code, but I want to be able to in the future . I also go as far as pass the number 9 to combo when I instantiate the object in the window class init. Unfortunately, changing to

            def __init__(self, position, parent=None):
            

            does not work, nor does removing any parameters from super

            super().__init__()
            

            I've also tried other variations including

            def __init__(self, position, parent=None,*args, **kwargs):  
                    super().__init__(*args, **kwargs)
                    self.position = position
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Mike-Taco
            Since position should be irrelevant at present, can you please confirm properly that by just removing that parameter, and keeping rest of code the same, it all really works?

            If so, go back in on restored code with position and just make 100% sure --- with a print() statement in your __init__() --- that is being called. just in case it is picking up the base QComboBox with the optional parent parameter instead of your constructor.

            M JonBJ 2 Replies Last reply
            1
            • JonBJ JonB

              @Mike-Taco
              Since position should be irrelevant at present, can you please confirm properly that by just removing that parameter, and keeping rest of code the same, it all really works?

              If so, go back in on restored code with position and just make 100% sure --- with a print() statement in your __init__() --- that is being called. just in case it is picking up the base QComboBox with the optional parent parameter instead of your constructor.

              M Offline
              M Offline
              Mike Taco
              wrote on last edited by
              #6

              @JonB the print statement with the position executes just fine when placed after the self.position = position line, but the widget still does not appear in the window. i.e.

              def __init__(self, position, parent=None):  
                      super().__init__()
                      self.position = position
                      print(self.position)
              
              1 Reply Last reply
              0
              • eyllanescE eyllanesc

                @Mike-Taco I don't understand your question or what you want to get. "position" you just store it in a variable and nothing else. What do you want to get?

                M Offline
                M Offline
                Mike Taco
                wrote on last edited by
                #7

                @eyllanesc Although it is not represented in my code above, I want to be able to use the position variable later. For whatever reason how I have the init function written, upon running it, the widget wont appear in the window. If I add a print statement at the end of the init function, it executes just fine, but still no widget appearance in the window,

                def __init__(self, position, parent=None):  
                        super().__init__()
                        self.position = position
                        print(self.position)
                
                eyllanescE 1 Reply Last reply
                0
                • M Mike Taco

                  @eyllanesc Although it is not represented in my code above, I want to be able to use the position variable later. For whatever reason how I have the init function written, upon running it, the widget wont appear in the window. If I add a print statement at the end of the init function, it executes just fine, but still no widget appearance in the window,

                  def __init__(self, position, parent=None):  
                          super().__init__()
                          self.position = position
                          print(self.position)
                  
                  eyllanescE Offline
                  eyllanescE Offline
                  eyllanesc
                  wrote on last edited by eyllanesc
                  #8

                  @Mike-Taco For a widget to appear as part of another widget then the widget must be a child of the other widget or one of its children. The layouts apart from establishing the size and position also establish that relationship, as you do not use it then you must use the parent that they already indicated:

                  def __init__(self, position, parent=None):  
                       super().__init__(parent) # <---
                       self.position = position
                       print(self.position)
                  

                  If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                  1 Reply Last reply
                  1
                  • M Offline
                    M Offline
                    Mike Taco
                    wrote on last edited by
                    #9

                    Found a solution from a user in another forum

                    from PyQt5 import QtCore, QtWidgets, QtGui
                    
                    class ComboBox(QtWidgets.QComboBox):
                        popupAboutToBeShown = QtCore.pyqtSignal()
                    
                    
                        def __init__(self, window, position):
                            super().__init__(window)
                            self.position = position
                            print(self.position)
                    
                        def showPopup(self):
                            self.setGeometry(QtCore.QRect(10, 10, 480, 41))
                            self.popupAboutToBeShown.emit()
                            super().showPopup()
                    
                        def hidePopup(self):
                            self.setGeometry(QtCore.QRect(10, 10, 21, 41))
                            self.popupAboutToBeShown.emit()
                            super().hidePopup()
                    
                    class Window(QtWidgets.QWidget):
                        def __init__(self):
                            super().__init__()
                            self.resize(916, 916)
                            self.combo = ComboBox(self, 9)
                            self.combo.setGeometry(QtCore.QRect(10, 10, 21, 41))
                            self.combo.addItems(["ITEM 1","ITEM 2","ITEM 3"])
                    
                    if __name__ == '__main__':
                        import sys
                        app = QtWidgets.QApplication(sys.argv)
                        window = Window()
                        window.show()
                        sys.exit(app.exec_())
                    eyllanescE 1 Reply Last reply
                    0
                    • M Mike Taco

                      Found a solution from a user in another forum

                      from PyQt5 import QtCore, QtWidgets, QtGui
                      
                      class ComboBox(QtWidgets.QComboBox):
                          popupAboutToBeShown = QtCore.pyqtSignal()
                      
                      
                          def __init__(self, window, position):
                              super().__init__(window)
                              self.position = position
                              print(self.position)
                      
                          def showPopup(self):
                              self.setGeometry(QtCore.QRect(10, 10, 480, 41))
                              self.popupAboutToBeShown.emit()
                              super().showPopup()
                      
                          def hidePopup(self):
                              self.setGeometry(QtCore.QRect(10, 10, 21, 41))
                              self.popupAboutToBeShown.emit()
                              super().hidePopup()
                      
                      class Window(QtWidgets.QWidget):
                          def __init__(self):
                              super().__init__()
                              self.resize(916, 916)
                              self.combo = ComboBox(self, 9)
                              self.combo.setGeometry(QtCore.QRect(10, 10, 21, 41))
                              self.combo.addItems(["ITEM 1","ITEM 2","ITEM 3"])
                      
                      if __name__ == '__main__':
                          import sys
                          app = QtWidgets.QApplication(sys.argv)
                          window = Window()
                          window.show()
                          sys.exit(app.exec_())
                      eyllanescE Offline
                      eyllanescE Offline
                      eyllanesc
                      wrote on last edited by
                      #10

                      @Mike-Taco Don't use window as it can be confused with the global variable assigned to Window. In my previous comment you must change self.combo = ComboBox(9, self).

                      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                      1 Reply Last reply
                      1
                      • JonBJ JonB

                        @Mike-Taco
                        Since position should be irrelevant at present, can you please confirm properly that by just removing that parameter, and keeping rest of code the same, it all really works?

                        If so, go back in on restored code with position and just make 100% sure --- with a print() statement in your __init__() --- that is being called. just in case it is picking up the base QComboBox with the optional parent parameter instead of your constructor.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @JonB said in Help overwriting a PyQT widget class and adding a new property:

                        @Mike-Taco
                        Since position should be irrelevant at present, can you please confirm properly that by just removing that parameter, and keeping rest of code the same, it all really works?

                        I still don't see that any of this has to do with you adding a position parameter, which is what you said caused the code not to work.

                        1 Reply Last reply
                        1

                        • Login

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