QPushButton text between icons/images
-
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):
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. -
Hi and welcome to devnet,
You can create a subclass and reimplement the paintEvent method to handle your custom look.
-
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])
-
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])
-
@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.
-
Hi and welcome to devnet,
You can create a subclass and reimplement the paintEvent method to handle your custom look.
@SGaist Thanks, solved the issue by re-implementing the paintEvent method