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. QPushButton text between icons/images
Forum Updated to NodeBB v4.3 + New Features

QPushButton text between icons/images

Scheduled Pinned Locked Moved Solved Qt for Python
6 Posts 3 Posters 2.5k Views 1 Watching
  • 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.
  • WysciguvvkaW Offline
    WysciguvvkaW Offline
    Wysciguvvka
    wrote on last edited by Wysciguvvka
    #1

    Hi,
    Using PySide6, Is there any way to add two icons to one button and the text between them?
    What I want to achieve (example):
    2010bc3f-393d-4357-b57b-a96fd2f4c6a7-image.png
    I tried to add layout with QLabel into a button, but that was kind of messy, so I'm looking for any other way (if possible).
    I will appreciate c++ / python solutions.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You can create a subclass and reimplement the paintEvent method to handle your custom look.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      WysciguvvkaW 1 Reply Last reply
      2
      • ndiasN Offline
        ndiasN Offline
        ndias
        wrote on last edited by ndias
        #3

        Hi @Wysciguvvka ,

        Since QPushButton does not support HTML (unlike Qlabel), I think the best option is to create a subclass QPushButton containing a QHBoxLayout() to hold two images and a label.

        Please find an example of it bellow:

        # Import the necessary modules required
        import sys
        
        
        from PySide2.QtWidgets import *
        from PySide2.QtGui import *
        from PySide2.QtCore import *
        
        
        class MyWidget(QWidget):
        
            def __init__(self):
                super(MyWidget, self).__init__()
                self.setGeometry(100,100, 200,200)
                self.initUI()
                self.show()
        		
            def initUI(self):
                self.button = imgButton("Resources/arrow.png", "Hello!", self)
                self.button.clicked.connect(lambda: print("Button Pressed!"))
        
                self.button.resize(100, 40)
        
        
        class imgButton(QPushButton):
            def __init__(self, img, text, parent):
                super(imgButton, self).__init__(parent)
                # Create a QHBoxLayout instance
                horizontalLayout = QHBoxLayout(self)
        
                # Add icon/image to the layout (Left)
                icon = QIcon(img)
                pixmap = icon.pixmap(10, 10, QIcon.Active, QIcon.On)
                self.iconLabelL = QLabel(self)
                self.iconLabelL.setPixmap(pixmap)
                horizontalLayout.addWidget(self.iconLabelL, 0) # (stretch factor 0)
        
                # Add label to the layout (Center)
                self.iconLabel = QLabel(text)
                self.iconLabel.setAlignment(Qt.AlignCenter)
                horizontalLayout.addWidget(self.iconLabel, 1)
        
                # Add icon/image to the layout (Rigth)
                icon = QIcon(img)
                pixmap = icon.pixmap(10, 10, QIcon.Active, QIcon.On)
                self.iconLabelR = QLabel(self)
                self.iconLabelR.setPixmap(pixmap)
                horizontalLayout.addWidget(self.iconLabelR, 0) # (stretch factor 0)
        
                self.show()
            
        
        if __name__ == "__main__":
            try:
                myApp = QApplication(sys.argv)
                myWindow = MyWidget()
                myApp.exec_()
                sys.exit(0)
            except NameError:
                print("Name Error:", sys.exc_info()[1])
            except SystemExit:
                print("Closing Window...")
            except Exception:
                print(sys.exc_info()[1])
        

        99c5016b-676f-4b37-8559-958da9f63c93-image.png

        SGaistS 1 Reply Last reply
        1
        • ndiasN ndias

          Hi @Wysciguvvka ,

          Since QPushButton does not support HTML (unlike Qlabel), I think the best option is to create a subclass QPushButton containing a QHBoxLayout() to hold two images and a label.

          Please find an example of it bellow:

          # Import the necessary modules required
          import sys
          
          
          from PySide2.QtWidgets import *
          from PySide2.QtGui import *
          from PySide2.QtCore import *
          
          
          class MyWidget(QWidget):
          
              def __init__(self):
                  super(MyWidget, self).__init__()
                  self.setGeometry(100,100, 200,200)
                  self.initUI()
                  self.show()
          		
              def initUI(self):
                  self.button = imgButton("Resources/arrow.png", "Hello!", self)
                  self.button.clicked.connect(lambda: print("Button Pressed!"))
          
                  self.button.resize(100, 40)
          
          
          class imgButton(QPushButton):
              def __init__(self, img, text, parent):
                  super(imgButton, self).__init__(parent)
                  # Create a QHBoxLayout instance
                  horizontalLayout = QHBoxLayout(self)
          
                  # Add icon/image to the layout (Left)
                  icon = QIcon(img)
                  pixmap = icon.pixmap(10, 10, QIcon.Active, QIcon.On)
                  self.iconLabelL = QLabel(self)
                  self.iconLabelL.setPixmap(pixmap)
                  horizontalLayout.addWidget(self.iconLabelL, 0) # (stretch factor 0)
          
                  # Add label to the layout (Center)
                  self.iconLabel = QLabel(text)
                  self.iconLabel.setAlignment(Qt.AlignCenter)
                  horizontalLayout.addWidget(self.iconLabel, 1)
          
                  # Add icon/image to the layout (Rigth)
                  icon = QIcon(img)
                  pixmap = icon.pixmap(10, 10, QIcon.Active, QIcon.On)
                  self.iconLabelR = QLabel(self)
                  self.iconLabelR.setPixmap(pixmap)
                  horizontalLayout.addWidget(self.iconLabelR, 0) # (stretch factor 0)
          
                  self.show()
              
          
          if __name__ == "__main__":
              try:
                  myApp = QApplication(sys.argv)
                  myWindow = MyWidget()
                  myApp.exec_()
                  sys.exit(0)
              except NameError:
                  print("Name Error:", sys.exc_info()[1])
              except SystemExit:
                  print("Closing Window...")
              except Exception:
                  print(sys.exc_info()[1])
          

          99c5016b-676f-4b37-8559-958da9f63c93-image.png

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @ndias please don't call show in your widgets init function. That's bad behaviour. A widget shall not force its own visibility. That is the role of the code creating and or managing to show a widget at the correct time.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          ndiasN 1 Reply Last reply
          2
          • SGaistS SGaist

            @ndias please don't call show in your widgets init function. That's bad behaviour. A widget shall not force its own visibility. That is the role of the code creating and or managing to show a widget at the correct time.

            ndiasN Offline
            ndiasN Offline
            ndias
            wrote on last edited by
            #5

            Hi @SGaist,
            Thanks for the advice. Luckily it was just a copy-paste lapse. :)

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi and welcome to devnet,

              You can create a subclass and reimplement the paintEvent method to handle your custom look.

              WysciguvvkaW Offline
              WysciguvvkaW Offline
              Wysciguvvka
              wrote on last edited by Wysciguvvka
              #6

              @SGaist Thanks, solved the issue by re-implementing the paintEvent method

              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