0

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.

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
  • What's `TabWidget` class? Is it inheriting after `QTabWidget` ? Also `addTab` and `setCurrentWidget` are not static methods. – prajmus Jan 17 '14 at 22:31
  • Yes, TabWidget inherits QTabWidget. Can you just give me sample code to do this? – user3188471 Jan 18 '14 at 04:40
  • Looking at your `TabWidget::addTab` call, you seem to have created a static method there (if it compiles). This seems very wrong... If it can do anything meaningful, then your data members must be static, which is also very bad design (though it will work as long as you have just one of them). Please study static vs. non-static members (methods and variables) of C++ OOP a bit more. – hyde Jan 19 '14 at 08:24
  • Another way to say what I say above is, to code in Qt/C++ you need to learn C++ first (except you don't *need* exceptions, or creating templates yourself, or any `std` stuff like `std::string` or `std::cout`). Jumping into Qt widget programming without knowing something as basic as static vs non-static method will be... painful. – hyde Jan 19 '14 at 08:28
  • I dont think I have used static members anywhere. I have posted my code. – user3188471 Jan 19 '14 at 18:03

1 Answers1

1

You have some errors in your code. This is the way you would add tabs to QTabWidget

QTabWidget *tabwidget = new QTabWidget(ui->centralWidget);
tabwidget->setObjectName(QString("tabbar"));

QWidget *tab = new QWidget();
tabwidget->addTab(tab, "tab1");

QWidget *tab2 = new QWidget();
tabwidget->addTab(tab2, "tab2");
tabwidget->setCurrentWidget(tab2);
// etc.

So in your case it will probably be:

page1 = new TabWidget(ui->centralWidget);

page2 = new Form2();
page1->addTab(page2, "Scalar Parameters");
page1->setCurrentWidget(page2);

page3 = new Form3();
page1->addTab(page3, "Table Parameters");
page1->setCurrentWidget(page3);

You have to add tabs (your Form classes) to the QTabWidget which means adding them to page1 object.

If you want to add Tabs from a place where you can't access page1, I'd add ObjectName to page1 so that you can search it later.

TabWidget *page1 = new TabWidget();
this->centralWidget()->layout()->addWidget(page1);
page1->setObjectName("TabWidget"); //name it whatever you want

And then when adding new Tab do:

tabwidget3 *page = new tabwidget3()
TabWidget *temporary = ui->centralWidget->findChild<TabWidget *>("TabWidget");
temporary->addTab(page3, "name");
temoprary->setCurrentWidget(page3);

Don't create new TabWidget. QTabWidget is like a container for tabs, if you want to add a tab you have to add it to and existing one.

prajmus
  • 3,171
  • 3
  • 31
  • 41
  • Thanks for your detailed answer, still am not able to get it. I have reframed my question better. Please have a look at it. – user3188471 Jan 18 '14 at 20:15
  • it says TabWidget has no member named centralwidget. Please bear with me. I am really new to this. – user3188471 Jan 18 '14 at 20:26
  • @user3188471 you have to post some code from your classes, otherwise I don't know what the problem is. And at least the line where the error occurs – prajmus Jan 18 '14 at 20:33
  • @user3188471 look at the second part of my post, the one with `setObjectName` you have to do it to your `page1` when adding, and than do `findChild` when adding `page3` (not two as I've written), I'll edit my answer a bit right away – prajmus Jan 20 '14 at 18:19
  • H:\Qt\Tools\QtCreator\bin\basinwindow\tabwidget2.cpp:22: error: 'class Ui::tabwidget2' has no member named 'centralWidget' ^ – user3188471 Jan 20 '14 at 20:10
  • How do I invoke mainframe's central widget here? – user3188471 Jan 20 '14 at 20:11
  • @user3188471 take a look now, you don't have to blindly copy the code, I updated it once again – prajmus Jan 20 '14 at 20:20
  • I have done this, this->centralWidget()->layout()->addWidget(page1); It should be there to load my page in the widget. But i cannot access ui->central widget of mainframe in tabwidget2.cpp. – user3188471 Jan 20 '14 at 20:33
  • the ui->centralWidget was just an example, I'm not writing your program, but helping you. Try deleting it. – prajmus Jan 20 '14 at 20:37