Im trying to display 3 forms(calender, history, statistics) which has 3 classes into a MainWindow Class which has three tabs created using the designer. When the application runs for the first time, it displays the history form into the tab. But when it is being run the second time, the form is displayed over the tabs that they are not visible.
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CalenderForm *pCal = new CalenderForm();
lay = new QVBoxLayout(ui->tab);
lay->addWidget(pCal);
connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(a(int)));
}
void MainWindow::a(int index)
{
switch(index)
{
case 0:
callCal();
break;
case 1:
callHist();
break;
default:
break;
}
}
void MainWindow::callHist()
{
HistoryForm *pHis = new HistoryForm();
pHis->DisplayHistory();
pHis->show();
lay2 = new QVBoxLayout(ui->tab_2);
lay2->addWidget(pHis);
}
Everytime you switch the tab index, you create a new form. I am not sure if this is the cause of your problem, but it definitely is a problem.
You should ensure the forms are only created once. For example you may create them in the constructor of MainWindow and store pointers to them in member variables. You should also already assign any required layouts there.
When you switch the index, you just call the DisplayHistory() or equivalent method.
PS: If you still want to understand your code next year, you should probably find a more speaking name than “a” for that slot 😉
[Edit]
Here is a sample header and cpp file. Be adviced that it might not compile as is and you might have to do some adjustments, but I wanna show you the general idea.
Header file
CPP File: