[SOLVED] TableView inside MdiArea dosen't show the data



  • I am developing a GUI consist of a MdiArea as central widget and two dock windows. I have implemented a tabeView inside the MdiArea. It pops up when it's called from the menu, but, it doesn't show any of the table rows, columns, and data.

    I appreciate any help to resolve this issue.

    Thanks

    @
    // mainwindow.cpp

    #include <QtGui>

    #include "mainwindow.h"
    #include "mdichild.h"
    #include <QTableView>
    #include "mymodel.h"

    MdiTableViewChild *MainWindow::createMdiTableViewChild()
    {
    MdiTableViewChild *child = new MdiTableViewChild;
    mdiArea->addSubWindow(child);

      return child;
    

    }

    void MainWindow::openMdiTable()
    {
    MdiTableViewChild *child = createMdiTableViewChild();
    child->setWindowTitle("Table View");
    MyModel myModel(0);
    child->setModel( &myModel );
    child->show();
    }

    // mymodel.cpp
    #include "mymodel.h"

    MyModel::MyModel(QObject *parent)
    :QAbstractTableModel(parent)
    {
    }

    int MyModel::rowCount(const QModelIndex & /parent/) const
    {
    return 2;
    }

    int MyModel::columnCount(const QModelIndex & /parent/) const
    {
    return 3;
    }

    QVariant MyModel::data(const QModelIndex &index, int role) const
    {
    if (role == Qt::DisplayRole)
    {
    return QString("Row%1, Column%2")
    .arg(index.row() + 1)
    .arg(index.column() +1);
    }
    return QVariant();
    }

    // mymodel.h

    #ifndef MYMODEL_H
    #define MYMODEL_H

    #include <QAbstractTableModel>
    #include <QTableView>

    class MyModel : public QAbstractTableModel
    {
    Q_OBJECT
    public:
    MyModel(QObject *parent);
    int rowCount(const QModelIndex &parent = QModelIndex()) const ;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    };

    #endif
    @



  • Line 24, MyModel goes out of scope at line 27 and is destroyed... There is no model for the view to show. Nothing to do with MDI



  • Thanks ChrisW67. Would you please suggest a solution?



  • Allocate myModel on the heap with the child (or another suitable object) as its parent to avoid a memory leak.



  • Thanks for your hint. I have changed lines 22 and 26 as follows and problem was solved.

    @
    QTableView *tableView = new QTableView(mdiArea);
    mdiArea->addSubWindow(tableView);
    MyModel *myModel = new MyModel(tableView);
    tableView->setModel( myModel );
    tableView->show();
    @


Log in to reply
 

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