4

I want to add tabs dynamically in a Qt application depending on user inputs.

  1. One tab is to be there all the time by default. For convenience, it would be great if I could create the layout and features of this tab in the graphic editor. Then I would like to transfer this layout into code, put in a class constructor and add tabs like:

    ui->tabWidget->addTab(new myTabClass(), "Tab 2");

  2. I want to promote this tab programatically as well. Is that possible?

Community
  • 1
  • 1
user3050215
  • 185
  • 1
  • 5
  • 14
  • Sounds like you want to design a custom QWidget in QT Designer which will be assigned to each tab (ie - myTabClass is your custom QWidget) – tinkertime Aug 04 '14 at 21:03
  • Perhaps. In my tab I want to include the custom widget QCustomPlot. This one is used by promoting a QWidget into a QCustomPlot. It's working in the GUI editor but I really dont know how to do it in code though. – user3050215 Aug 04 '14 at 21:19

2 Answers2

6

For adding tabs dynamically and constructed by a class, you can use an additional .ui file. This way you can do all the layout stuff with the Qt Designer GUI.

1) Create an empty tab widget in the mainwindow.ui. (eg. named myTabWidget)

2) Add to your project directory a new “Qt Design Form Class” as QWidget class, not as QTabWidget (eg. named MyTabPage):

Right click project -> Add new -> Qt -> Qt Design Form Class

3) In the mytabpage.ui you make the design as you want it to be inserted in myTabWidget.

4) The next step you can instantiate MyTabPage in the MainWindow constructer or elsewhere and add it to myTabWidget. The empty tab in myTabWidget can be removed before. To access paramaters form myNewTab you need a function declared in MyTabPage.

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    MyTabPage *myNewTab = new MyTabPage;
    ui-> myTabWidget ->removeTab(0);
    ui-> myTabWidget ->addTab(myNewTab, tr("name"))
    myNewTab->functionDeclaredInMyTabPage (); //access parameters of myNewTab
}

PS: I am aware the question is old. But I want to offer a step by step solution to others cause I had to struggle with it for my self recently.

crx
  • 153
  • 2
  • 8
  • This is a good answer.But it does ineset the new tab where the old one is. I mean the tabs are added but somehow they are buried under new ones !! – McLan Nov 09 '18 at 17:00
  • Thanx for this answer, i needed to know how to do it with designer. This was the solution i needed! – Esocoder Dec 24 '19 at 16:46
  • @McLan Because ui-> myTabWidget ->removeTab(0); removes the previous tab.... – Mecanik Dec 08 '21 at 09:18
  • Fantastic - thanks ...minor correction: Missing `;` on the end of thee `addTab()` line – BlueChip Feb 04 '22 at 22:37
1

You can insert tab by int QTabWidget::insertTab ( int index, QWidget * page, const QIcon & icon, const QString & label ) which inserts a tab with the given label, page, and icon into the tab widget at the specified index :

ui->tabWidget->insertTab(1,new myTabClass(),QIcon(QString(":/SomeIcon.ico")),"TabText");

Also removing a tab is done by QTabWidget::removeTab ( int index ).

Nejat
  • 31,784
  • 12
  • 106
  • 138