Add icon to PyQt5 toolbar and bind click event

The software you have recently made needs a toolbar, and then you need to perform certain operations by clicking the icon on the toolbar.

  • Create a toolbartoolBar
  • Add toolbar to main windowaddToolBar
  • Create tool buttonsrefresf_button
  • button to add icon and text
  • connect button click event
  • Toolbar add buttontoolBar.addWidget(refrefh_botton)

self.toolBar = QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
self.toolBar.setContentsMargins(QMargins(20, 0, 20, 0))
MainWindow.addToolBar(Qt.TopToolBarArea, self.toolBar)
self.refresh_button = QPushButton()
refresh_icon = QIcon()
refresh_icon.addPixmap(QPixmap('../images/refresh.png'), QIcon.Normal, QIcon.Off)
self.refresh_button.setIcon(refresh_icon)
self.refresh_button.setIconSize(QSize(25, 25))
self.refresh_button.setStyleSheet(QSS.ButtonStyle)
self.refresh_button.clicked.connect(self.refresh_treewidget)
self.toolBar.addWidget(self.refresh_button)

Leave a Comment

Your email address will not be published. Required fields are marked *