I want to add tabs dynamically in a Qt application depending on user inputs.
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");
I want to promote this tab programatically as well. Is that possible?
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.