Qt World Summit: Submit your Presentation

SOLVED: Set Tab Icon with code

  • Hello Developers,

    I have been trying to set an icon for a tab in a tabWidget using code but failed so far. Its easier with the form designer but I need to use code to make my app dynamic.
    Here's the code for mainwindow.cpp, the rest of the files are the default for a MainWindow project.
    Please show me how to set icons with size 20x20p for both tab1 and tab2 below.

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    QTabWidget *tabWidget = new QTabWidget;
    QWidget *tab1 = new QWidget;
    QWidget *tab2 = new QWidget;
    tabWidget->insertTab(0, tab1, "Tab 1");
    tabWidget->insertTab(1, tab2, "Tab 2");


    delete ui;

    Thanks in advance

  • Hi Qkato.
    Did you try insert tabs with this option?

    @int QTabWidget::insertTab(int index, QWidget * page, const QIcon & icon, const QString & label)@

    Hope it's util.

  • Read the manual please:

    int QTabWidget::insertTab(int index, QWidget * page, const QIcon & icon, const QString & label)

  • Moderators

    or even QTabWidget::setTabIcon() ...

  • Yes, I can see the parameters but cant seem to get it right.
    Please illustrate how I can add an icon that's a resource with path "image/icon.png"

  • Moderators


  • Tried the example, still can't get the icon on the tab. Is it about having the size of the image defined too, and if so how is it done?

  • Please show you full code and resource file

  • Thanks for all your posts, When mcosta talked of resource file I took a look at it and there was a prefix for images. Its why the icon wasn't visible all along. Now all is fine.

    Thanks so much for the help.

Log in to reply