Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
SOLVED: Set Tab Icon with code
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.
MainWindow::MainWindow(QWidget *parent) :
QTabWidget *tabWidget = new QTabWidget; QWidget *tab1 = new QWidget; QWidget *tab2 = new QWidget; setCentralWidget(tabWidget); tabWidget->insertTab(0, tab1, "Tab 1"); tabWidget->insertTab(1, tab2, "Tab 2");
Thanks in advance
SergioDanielG last edited by
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.
dbzhang800 last edited by
Read the manual please:
int QTabWidget::insertTab(int index, QWidget * page, const QIcon & icon, const QString & label)
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"
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?
mcosta last edited by
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.