SOLVED [Find an QstandardItemModel in tabWidget !



  • hi everybody !

    i've an problem to find my QStandardItemModel in tabWidget.

    I use Tableview to see QstandardItemModel, i have created 2 tableView by using a QtabWidget.

    I would like read th item in each tableView, but i can't. It's only possible in the last created. i am using QObject::findchild(), but it doesn't operate.(it's ok if i repalce my QtableView & QStandardItemModel by Qlabel !)

    se may code below, thanks for your help.
    (be aware with my english ;-) )

    i dont have include the main here, but it is classical.

    my .cpp

    @#include "main_window.h"

    main_window::main_window(QWidget *parent) : QMainWindow(parent)
    {

    //Main Window
    QMdiArea *mdi = new QMdiArea;
    setCentralWidget(mdi);
    this->setWindowTitle("Main Window");
    this->resize(550,400);

    //command panel
    QGroupBox *cmd = new QGroupBox("Add Tab");
    cmd->setTitle("Command Panel");
    cmd->setFixedWidth(250);

    //init command panel
    lbl_cmd = new QLabel("Nom de tabulation");
    lbl_cmd->setAlignment(Qt::AlignHCenter);
    str_lbl = "empty";
    lbl_tab0 = new QLabel;
    lbl_tab1 = new QLabel;
    lblTab();
    
    //button command panel
    QPushButton *exit = new QPushButton("Exit");
    QPushButton *table_buton = new QPushButton("TableView");
    QPushButton *lbl_buton = new QPushButton("lblTab display");
    QPushButton *stdItmModel_buton = new QPushButton("stdItmModel");
    QPushButton *ouv_fichier_buton = new QPushButton("Not Use");
    

    //TableView display
    tabu = new QTabWidget;

    //layout command panel
    QHBoxLayout *h_bouton1 = new QHBoxLayout;
    h_bouton1->addWidget(table_buton);h_bouton1->addWidget(lbl_buton);

    QHBoxLayout *h_bouton2 = new QHBoxLayout;
    h_bouton2->addWidget(stdItmModel_buton);h_bouton2->addWidget(ouv_fichier_buton);
    
    QVBoxLayout *v_cmd = new QVBoxLayout;
    v_cmd->addWidget(lbl_cmd);
    v_cmd->addWidget(lbl_tab0);
    v_cmd->addWidget(lbl_tab1);
    v_cmd->addLayout(h_bouton1);
    v_cmd->addLayout(h_bouton2);
    v_cmd->addWidget(exit);
    cmd->setLayout(v_cmd);
    

    //Layout main window
    QHBoxLayout *h_lay = new QHBoxLayout;
    h_lay->addWidget(tabu);
    h_lay->addWidget(cmd);
    mdi->setLayout(h_lay);

    //Connect
    connect(table_buton,SIGNAL(clicked()),this,SLOT(tableView()));
    connect(exit,SIGNAL(clicked()),this,SLOT(close()));
    connect(lbl_buton,SIGNAL(clicked()),this,SLOT(str_lbl_chg()));
    connect(stdItmModel_buton,SIGNAL(clicked()),this,SLOT(stdItmModel()));
    }

    void main_window::tableView()
    {
    for (int l(0);l<2;l++) //create 2 tableView
    {
    table = new QTableView;
    QString str;
    modele = new QStandardItemModel(5,2);
    modele->setHeaderData(0,Qt::Horizontal,"Col 0");
    modele->setHeaderData(1,Qt::Horizontal,"Col 1");
    modele->setHeaderData(0,Qt::Vertical,"L 0");
    modele->setHeaderData(1,Qt::Vertical,"L 1");
    modele->setHeaderData(2,Qt::Vertical,"L 2");
    modele->setHeaderData(3,Qt::Vertical,"L 3");
    modele->setHeaderData(4,Qt::Vertical,"L 4");

        for (int loopA(0);loopA<5;loopA++) //filing tablView
        {
            QString strLoopA;
            stdIt = new QStandardItem;
            stdIt->setText("value tab"+str.setNum(l)+" "+strLoopA.setNum(loopA)+"x0");
            modele->setItem(loopA,0,stdIt);
            stdIt = new QStandardItem;
            stdIt->setText("value tab "+str.setNum(l)+" "+strLoopA.setNum(loopA)+"x1");
            modele->setItem(loopA,1,stdIt);
        }
        table->setModel(modele);
        tabu->addTab(table,"tab "+str.setNum(l)); //add TabWidget
    }
    

    }
    void main_window::lblTab() //display in command panel
    {
    lbl_tab0->setText("tab0 value of row 1,col 0 : "+str_lbl);
    lbl_tab1->setText("tab1 value of row 2,col 1 : "+str_lbl);
    }
    void main_window::str_lbl_chg() //mofidifcation command panel display
    {
    //str_lbl = "test ok"; // remove//before str_lbl to performed lbl modification test
    str_lbl = stdItmModel()->item(1,0)->text();
    str_lbl = stdItmModel()->item(2,1)->text();
    lblTab();
    }
    QStandardItemModel *main_window::stdItmModel() // to return QStandardItemModel in active tabWidget
    {
    return tabu->currentWidget()->findChild <QStandardItemModel *>();
    }
    @

    my .h
    @#ifndef MAIN_WINDOW_H
    #define MAIN_WINDOW_H

    #include <QApplication>
    #include <QDialog>
    #include <QFile>
    #include <QFormLayout>
    #include <QGroupBox>
    #include <QHBoxLayout>
    #include <QInputDialog>
    #include <QLabel>
    #include <QMainWindow>
    #include <QMdiArea>
    #include <QMessageBox>
    #include <QPushButton>
    #include <QStandardItem>
    #include <QStandardItemModel>
    #include <QTableView>
    #include <QTabWidget>
    #include <QTextStream>
    #include <QLineEdit>
    #include <QVBoxLayout>

    class main_window : public QMainWindow
    {
    Q_OBJECT
    public:
    main_window(QWidget *parent = 0);
    QLabel *lbl_cmd, *lbl_tab0, *lbl_tab1;
    QString *str, msgbox;
    QStandardItem *stdIt;
    QStandardItemModel *modele;
    QString str_lbl;
    QTableView *table;
    QTabWidget *tabu;

    signals:

    public slots:
    void tableView();
    void lblTab();
    void str_lbl_chg();

    QStandardItemModel *stdItmModel();
    

    };

    #endif // MAIN_WINDOW_H
    @



  • The QTableView does not take ownership of the model set with QTableView::setModel(); you will have to parent it explicitly (<code>modele = new QStandardItemModel(5,2,table)</code>) so it becomes a child of the QTableView.

    The model can be retrieved later on using QTableView::model().
    @
    QStandardItemModel main_window::stdItmModel()
    {
    return static_cast<QStandardItemModel
    >(static_cast<QTableView*>(tabu->currentWidget())->model());
    }
    @



  • Hi Lukas,
    thanks for your help, now is operate ! :-)

    Can you explane what is static_cast?

    because is operate with this code
    @QStandardItemModel *main_window::stdItmModel() // to return QStandardItemModel in active tabWidget
    {
    return tabu->currentWidget()->findChild <QStandardItemModel *>();
    }
    @
    with add my tableView like parents to my modele.

    thanks !



  • A "static_cast":http://en.wikipedia.org/wiki/Static_cast is used to "downcast":http://en.wikipedia.org/wiki/Downcasting from the base class pointer of QWidget* returned by QTabWidget::currentWidget() to the derived class pointer of type QTableView*, so we can access the QTableView::model() method.

    When you've added your widget using QTabWidget::addTab() you were supplying a pointer of type QTableView* (which was implicitly upcasted to a pointer of QWidget*, because QTabWidget::addTab() takes a pointer of QWidget* as parameter), but using QTabWidget::currentWidget() will get you only a pointer of type QWidget* out (the common base class of all widgets). To access the QTableView::model() method you will have to 'convert' your pointer of type QWidget* back to a pointer of type QTableView*. This is done using a static_cast.



  • Nice !

    thank you and have nice day !



  • You're weclome!


Log in to reply
 

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