SOLVED: Set Tab Icon with code
-
wrote on 3 Jul 2013, 00:48 last edited by
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.mainwindow.cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);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");
}
MainWindow::~MainWindow()
{
delete ui;
}@Thanks in advance
-
wrote on 3 Jul 2013, 01:15 last edited by
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.
Regards. -
wrote on 3 Jul 2013, 01:17 last edited by
Read the manual please:
@
int QTabWidget::insertTab(int index, QWidget * page, const QIcon & icon, const QString & label)
@ -
or even QTabWidget::setTabIcon() ...
-
wrote on 3 Jul 2013, 06:41 last edited by
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" -
@QIcon(“:/image/icon.png”)@
-
wrote on 3 Jul 2013, 09:57 last edited by
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?
-
wrote on 3 Jul 2013, 10:36 last edited by
Please show you full code and resource file
-
wrote on 3 Jul 2013, 13:38 last edited by
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.
1/9