Creating a tab widget with QTableView in it dynamically



  • Hi

    I have a QDialog in which i have one TabWidget with 4 tabs in it.
    In 4th tab i want to create one more tabwidget dynamically and number of tabs for this tabwidget is decide at runtime and in each tab we create i want to create a Tableview in it dynamically.

    How to do this?



  • What you mean by 'dynamically' ?
    If you mean that the number of tabs is variable, you can use a QList<newtab *> to store the tabs and add them using
    QTabWidget::addTab ( QWidget * page, const QString & label ) at runtime.



  • Hi

    Dynamically means i want to create it inside the code not in the .ui file.



  • @
    [pseudo code]
    QWidget *parentTab = parentTabWidget->widget(index);

    TabWidget *childTabWidget = new TabWidget(parentTab);
    // change style, layout, etc

    // read more: int addTab ( QWidget * page, const QString & label )
    childTabWidget->addTab(new Tab, "New Tab");

    @



  • HI stima

    I did the same way you told and it is working but I am not able to set the layout dudeto which the child tabwidget is smaller and is not expanding even is if maximise the mainwindow can you please help me to set the layout.



  • Hi, this may be will work :

    @

    MainWindow::MainWindow()
    {

    m_layout = new QVBoxLayout;

    // QTabWidget that contains 4 tabs
    m_parentTabWidget = new QTabWidget;
    m_layout->addWidget(m_parentTabWidget);

    // set layout to the parent widget of QTabWidget to expand the TabWidget
    // to the wall mainwindow (this is your problem)!
    this->setLayout(m_layout);

    tab1 = new QWidget;
    m_layout1 = new QVBoxLayout;
    tab1->setLayout(m_layout1);

    tab2 = new QWidget;
    m_layout2 = new QVBoxLayout;
    tab2->setLayout(m_layout2);

    tab3 = new QWidget;
    m_layout3 = new QVBoxLayout;
    tab3->setLayout(m_layout3);

    m_childTabWidget = new QTabWidget;

    m_parentTabWidget->addTab(tab1, "tab 1");
    m_parentTabWidget->addTab(tab2, "tab 2");
    m_parentTabWidget->addTab(tab3, "tab 3");
    m_parentTabWidget->addTab(m_childTabWidget, "tab 4");

    //QObject::connect( , SIGNAL(), , SLOT(addtab()));
    addtab();
    }

    // slot
    void MainWindow::addtab()
    {

    QWidget * tab = new QWidget;
    QVBoxLayout * layout = new QVBoxLayout;
    tab->setLayout(layout);
    m_childTabWidget->addTab(tab, "New Tab");

    }

    @

    I will send you the code if you want :)



  • HI

    Below is my code i tried to do same way you told me but its not working

    @ QWidget *parentTab = ui.tabWidget->widget(1);

    //Create child tab in parent tab
    QTabWidget *childTabWidget = new QTabWidget(parentTab);

    GeneralTab *obj = new GeneralTab(this);
    disconnect(obj->tablemodel_nameVariables, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),obj, SLOT(dataChangedParameterValue(const QModelIndex&, const QModelIndex&)));

    QStandardItem *temp1 = new QStandardItem("1");
    obj->tablemodel_nameVariables->setItem( 0, 0 ,temp1 );
    temp1->setData("Inder",Qt::UserRole+1);
    temp1->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled |Qt::ItemIsEditable);

    QStandardItem *temp2 = new QStandardItem("2");
    obj->tablemodel_nameVariables->setItem( 0,1 ,temp2 );
    temp2->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled |Qt::ItemIsEditable);

    QStandardItem *temp3 = new QStandardItem("3");
    obj->tablemodel_nameVariables->setItem( 0, 2,temp3 );
    temp3->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled |Qt::ItemIsEditable);

    connect(obj->tablemodel_nameVariables, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),obj, SLOT(dataChangedParameterValue(const QModelIndex&, const QModelIndex&)));

    GeneralTab *obj1 = new GeneralTab(this);
    disconnect(obj1->tablemodel_nameVariables, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),obj1, SLOT(dataChangedParameterValue(const QModelIndex&, const QModelIndex&)));

    QStandardItem *temp11 = new QStandardItem("11");
    obj1->tablemodel_nameVariables->setItem( 0, 0 ,temp11 );
    temp11->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled |Qt::ItemIsEditable);

    QStandardItem *temp22 = new QStandardItem("22");
    obj1->tablemodel_nameVariables->setItem( 0,1 ,temp22 );
    temp22->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled |Qt::ItemIsEditable);

    QStandardItem *temp33 = new QStandardItem("33");
    obj1->tablemodel_nameVariables->setItem( 0, 2,temp33 );
    temp33->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled |Qt::ItemIsEditable);

    connect(obj1->tablemodel_nameVariables, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),obj1, SLOT(dataChangedParameterValue(const QModelIndex&, const QModelIndex&)));

    childTabWidget->addTab(obj, tr("General"));
    childTabWidget->addTab(obj1, tr("General2"));@

    Note :- GeneralTab is my QWidget class on which TableView is present.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.