QStandardItemModel findchild !

hi everybody !

i've an problem to understand how is operate QStandardItemModel.

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.

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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 
There is a windows forum. Your question might get more answers down there.
:)
Hi !

sorry for my mistake !

i have resolved my probelm, i have forget to add parents in my QStandardItemModels !
modele = new QStandardItemModel(5,2,table);

have a nice day !
Topic archived. No new replies allowed.