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. Newbie - subclass QPushButton missing data
Forum Updated to NodeBB v4.3 + New Features

Newbie - subclass QPushButton missing data

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 532 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.
  • S Offline
    S Offline
    StarshipJohn
    wrote on last edited by
    #1

    First post here. I am a newbie attempting to learn Python / PyQt5 and, if I understand the terminology correctly, subclass QPushButton. The code I have below creates a button, seems to assign the input data I pass in, and responds to a mouse click. However, the button shows on the main window without a label, tool tip, and sizing that I pass to "myCustomButton". Using PyCharm 2021.2 CE on a Mac.

    Why does the button not have a label, tool tip, the wrong size, as seen in the main window and how do I fix this?

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QMainWindow, QLabel
    from PyQt5.QtCore import pyqtSlot
    
    
    class myCustomButton(QPushButton):
        def __init__(self, text, tooltip, height=50, width=100, parent=None):
            QPushButton.__init__(self)
            self.button = QPushButton()
            self.button.setFixedSize(height, width)
            self.button.setText(text)
            self.button.setToolTip(tooltip)
            print("Button text:    ", self.button.text())
            print("Button tooltip: ", self.button.toolTip())
            print("Button height:  ", self.button.height())
            print("Button width:   ", self.button.width())
            print("Button created.\n")
    
    
    class App(QMainWindow):
    
        def __init__(self):
            super().__init__()
            self.title = 'PyQt5 button test'
            self.left = 500
            self.top = 300
            self.width = 320
            self.height = 200
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
            layout = QVBoxLayout()
            widget = QWidget()
            widget.setLayout(layout)
            self.setCentralWidget(widget)
            button = myCustomButton("My Custom Button", 'This is an custom button tool tip', 80, 40)
            button.clicked.connect(self.on_click)
            layout.addWidget(QLabel("Label above button."))
            layout.addWidget(button)
            layout.addWidget(QLabel("Label below button."))
            self.show()
    
        @pyqtSlot()
        def on_click(self):
            print('Custom button clicked.')
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())
    

    Thanks.

    eyllanescE 1 Reply Last reply
    0
    • S StarshipJohn

      First post here. I am a newbie attempting to learn Python / PyQt5 and, if I understand the terminology correctly, subclass QPushButton. The code I have below creates a button, seems to assign the input data I pass in, and responds to a mouse click. However, the button shows on the main window without a label, tool tip, and sizing that I pass to "myCustomButton". Using PyCharm 2021.2 CE on a Mac.

      Why does the button not have a label, tool tip, the wrong size, as seen in the main window and how do I fix this?

      import sys
      from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QMainWindow, QLabel
      from PyQt5.QtCore import pyqtSlot
      
      
      class myCustomButton(QPushButton):
          def __init__(self, text, tooltip, height=50, width=100, parent=None):
              QPushButton.__init__(self)
              self.button = QPushButton()
              self.button.setFixedSize(height, width)
              self.button.setText(text)
              self.button.setToolTip(tooltip)
              print("Button text:    ", self.button.text())
              print("Button tooltip: ", self.button.toolTip())
              print("Button height:  ", self.button.height())
              print("Button width:   ", self.button.width())
              print("Button created.\n")
      
      
      class App(QMainWindow):
      
          def __init__(self):
              super().__init__()
              self.title = 'PyQt5 button test'
              self.left = 500
              self.top = 300
              self.width = 320
              self.height = 200
              self.initUI()
      
          def initUI(self):
              self.setWindowTitle(self.title)
              self.setGeometry(self.left, self.top, self.width, self.height)
              layout = QVBoxLayout()
              widget = QWidget()
              widget.setLayout(layout)
              self.setCentralWidget(widget)
              button = myCustomButton("My Custom Button", 'This is an custom button tool tip', 80, 40)
              button.clicked.connect(self.on_click)
              layout.addWidget(QLabel("Label above button."))
              layout.addWidget(button)
              layout.addWidget(QLabel("Label below button."))
              self.show()
      
          @pyqtSlot()
          def on_click(self):
              print('Custom button clicked.')
      
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
          ex = App()
          sys.exit(app.exec_())
      

      Thanks.

      eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by
      #2

      @StarshipJohn You are creating a button within the custom button, that is not inheritance but composition (unnecessary in this case). Change to:

      class myCustomButton(QPushButton):
          def __init__(self, text, tooltip, height=50, width=100, parent=None):
              QPushButton.__init__(self)
              self.setFixedSize(height, width)
              self.setText(text)
              self.setToolTip(tooltip)
              print("Button text:    ", self.text())
              print("Button tooltip: ", self.toolTip())
              print("Button height:  ", self.height())
              print("Button width:   ", self.width())
              print("Button created.\n")
      

      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
      2
      • S Offline
        S Offline
        StarshipJohn
        wrote on last edited by
        #3

        So close, so far away, so much to learn. Thank you.!

        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