from PySide2.QtWidgets import QApplication, QMainWindow, QAction, QToolBar from PySide2.QtGui import QIcon, QPixmap app = QApplication([]) win = QMainWindow() # Create a toolbar toolbar = QToolBar() win.addToolBar(toolbar) # Create an action for the toolbar action = QAction(QIcon(QPixmap("my_icon.png")), "My Action", win) def on_action_triggered(): print("Action triggered") action.triggered.connect(on_action_triggered) toolbar.addAction(action) # Show the window win.show() app.exec_()
from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton from PySide2.QtGui import QIcon, QPixmap app = QApplication([]) widget = QWidget() # Create a pixmap from file icon = QIcon(QPixmap("my_icon.png")) # Create a button with the icon button = QPushButton(icon, "My Button") button.setToolTip("Click me!") # Add the button to a layout layout = QVBoxLayout() layout.addWidget(button) widget.setLayout(layout) # Show the widget widget.show() app.exec_()In this example, a QIcon instance is created from a pixmap loaded from the file "my_icon.png". The QIcon instance is then used to create a QPushButton instance, which is added to a QVBoxLayout instance that is set as the layout for a QWidget instance. The button has a tooltip displayed when hovering over it. Overall, the PySide2.QtGui QIcon pixmap class provides an easy way to add custom icons to various GUI elements in PySide2 applications.