I wanted to create a new tab dynamically when a button is clicked in the mainwindow.Let this button be NEW. I have placed a QWidget
in the mainwindow where I would like the QTabWidget
to be visible. I have added a new form with class named TabWidget (QTabWidget
) containing my 1st page design.
I did this:
page1 = new TabWidget(ui->widget); // TabWidget inherits QTabWidget
this->centralWidget()->layout()->addWidget(page1);
Got the 1st page in my QWidget
. This is done.
I tried to implement traversing from one tab to other using BACK
and NEXT
button.
I designed the 2nd tab in a widget (named form2) instead of a QTabWidget
and I called this form from my 1st tab using:
page2 = new Form2(); // Form2 contains the tab2 design
TabWidget::addTab(page2,"Scalar Parameters"); // Only this works, I couldn't use objects
TabWidget::setCurrentWidget(page2);
Here comes the problem:
I could not do the same thing for the 3rd tab.
page3 = new Form3(); // Form3 contains the tab3 design
TabWidget *t= new TabWidget();
t->addTab(page3,"Table Parameters");
t->setCurrentWidget(page3);
What am I missing here?
I have these files,
mainframe.h,
tabwidget.h (inherits QTabWidget
) This is also my 1st tab,
tabwidget2.h (inherits QWidget
) 2nd tab,
tabwidget3.h (inherits QWidget
) 3rd tab.
I have included tabwidget.h in mainframe.h and created an object for it, page1. So when a button is clicked on the mainframe, i do this,
TabWidget *page1 = new TabWidget();
this->centralWidget()->layout()->addWidget(page1);
This creates a Tab with my design on the mainframe. This works perfectly. Then I have a NEXT button in this tab page1.So when I click it, I want to add a new tab page2 to this pag1. For this I included tabwidget2.h in tabwidget.h and created an object page2. Now when a button is clicked in my page1, I do this,
tabwidget2 *page2 = new tabwidget2();
tabwidget *t= new tabwidget();
t->addTab(page2,"Scalar Parameters");
t->setCurrentWidget(page2);
But this doesn't work. So I did this,
tabwidget2 *page2 = new tabwidget2(t);
tabwidget::addTab(page2,"Scalar Parameters");
tabwidget::setCurrentWidget(page2);
This worked and I got the 2nd page added to my 1st tab. The problem arises when I add the 3rd page when a NEXT button is clicked from the page2. I did this,
tabwidget3 *page3 = new tabwidget3();
tabWidget *t= new tabWidget();
t->addTab(page3,"Table Parameters");
t->setCurrentWidget(page3);
and didn't work. Program compiles without any error.
Mainframe -> (New button) -> page1( which is a QTabWidget
displays on Widget on mainframe) -> (NEXT button) -> page2 ( QWidget
added as a new tab to page1) -> (NEXT button ) -> nothing happens
I have tried my Best to explain this. Please Help me out.
Code: mainframe.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "tabwidget.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
TabWidget *page1;
~MainWindow();
private slots:
void on_action_About_triggered();
void on_action_New_triggered();
private:
Ui::MainWindow *ui;
};
Tabwidget.h
#ifndef TABWIDGET_H
#define TABWIDGET_H
#include <QTabWidget>
#include "tabwidget2.h"
namespace Ui {
class TabWidget;
}
class TabWidget : public QTabWidget
{
Q_OBJECT
public:
explicit TabWidget(QWidget *parent = 0);
~TabWidget();
tabwidget2 *page2;
private slots:
void on_pushButton_6_clicked();
private:
Ui::TabWidget *ui;
};
#endif // TABWIDGET_H
tabwidget2.h
#ifndef TABWIDGET2_H
#define TABWIDGET2_H
#include <QWidget>
#include "tabwidget3.h"
namespace Ui {
class tabwidget2;
}
class tabwidget2 : public QWidget
{
Q_OBJECT
public:
explicit tabwidget2(QWidget *parent = 0);
~tabwidget2();
tabwidget3 *page3;
private slots:
void on_pushButton_2_clicked();
private:
Ui::tabwidget2 *ui;
};
#endif // TABWIDGET2_H
mainframe.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QtWidgets"
#include "qtabwidget.h"
#include "qdialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_action_New_triggered()
{
TabWidget *page1 = new TabWidget();
this->centralWidget()->layout()->addWidget(page1);
}
tabwidget.cpp
#include "tabwidget.h"
#include "ui_tabwidget.h"
TabWidget::TabWidget(QWidget *parent) :
QTabWidget(parent),
ui(new Ui::TabWidget)
{
ui->setupUi(this);
}
TabWidget::~TabWidget()
{
delete ui;
}
void TabWidget::on_pushButton_6_clicked()
{
tabwidget2 *page2 = new tabwidget2();
this->addTab(page2,"Scalar Parameters");
this->setCurrentWidget(page2);
}
tabwidget2.cpp
#include "tabwidget2.h"
#include "ui_tabwidget2.h"
#include "tabwidget.h"
tabwidget2::tabwidget2(QWidget *parent) :
QWidget(parent),
ui(new Ui::tabwidget2)
{
ui->setupUi(this);
}
tabwidget2::~tabwidget2()
{
delete ui;
}
void tabwidget2::on_pushButton_2_clicked()
{
tabwidget3 *page3 = new tabwidget3();
TabWidget *t= new TabWidget();
t->addTab(page3,"Table Parameters");
t->setCurrentWidget(page3);
}
I just cant bring in my page3.