Search code examples
pythonpyqtpyqt5qtabwidgetqicon

Add Icon to Tab (QTabWidget)?


I'm working with a QTabWidget with three Tabs.
One of the tab is about Information/Help. So I want to use a picture of a Question Mark (.png) instead of the Text "help".

Is this Possible? I'm working with Python/PyQt and I can only find solutions for C++


Solution

  • int QTabWidget::addTab(QWidget *page, const QIcon &icon, const QString &label)

    Adds a tab with the given page, icon, and label to the tab widget, and returns the index of the tab in the tab bar. Ownership of page is passed on to the QTabWidget.


    void QTabWidget::setTabIcon(int index, const QIcon &icon)

    Sets the icon for the tab at position index.

    import sys
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Window(QtWidgets.QMainWindow):
        def __init__(self, parent=None):
            super(Window, self).__init__(parent)
            
            self.qtabwidget = QtWidgets.QTabWidget(self) 
            
            widget  = QtWidgets.QPlainTextEdit("QPlainTextEdit 1")
            label   = 'Tab &1'
            widget2 = QtWidgets.QPlainTextEdit("QPlainTextEdit 2")
            
            tab_index1 = self.qtabwidget.addTab(widget, label)
            
            tab_index2 = self.qtabwidget.addTab(widget2, 'Tab &2')
            self.qtabwidget.setTabIcon(tab_index2, QtGui.QIcon('im.png'))                # <---  
            self.qtabwidget.setIconSize(QtCore.QSize(32, 32)) 
            
            self.qtabwidget.addTab(
                    QtWidgets.QLabel("QLabel Tab &3", alignment=QtCore.Qt.AlignCenter), 
                    QtGui.QIcon('Ok.png'),                                               # < ---
                    'Tab &3')
            
            self.qtabwidget.addTab(None, "No Widget")
            
            self.qtabwidget.setTabsClosable(True)  
            self.qtabwidget.tabCloseRequested.connect(self.qtabwidget_tabcloserequested)
            self.qtabwidget.setTabShape(QtWidgets.QTabWidget.Triangular)
            self.qtabwidget.setTabPosition(QtWidgets.QTabWidget.East)  
            self.qtabwidget.setTabEnabled(0, False)         # disable tab
            self.qtabwidget.setTabEnabled(1, True)          # enable tab
            self.qtabwidget.currentChanged.connect(self.qtabwidget_currentchanged)
           
            self.setCentralWidget(self.qtabwidget)
    
        @QtCore.pyqtSlot(int)
        def qtabwidget_tabcloserequested(self, index):
            # gets the widget
            widget = self.qtabwidget.widget(index)
            # if the widget exists
            if widget:
                widget.deleteLater()
            # removes the tab of the QTabWidget
            self.qtabwidget.removeTab(index)
            
        @QtCore.pyqtSlot(int)
        def qtabwidget_currentchanged(self, index):
            print(f"\n New index of current page: {index}")
    
    
    if __name__ == '__main__':
        application = QtWidgets.QApplication(sys.argv)
        window = Window()
        window.setWindowTitle('QTabWidget')
        window.resize(400, 400)
        window.show()
        sys.exit(application.exec_())        
    

    enter image description here