Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] TableView inside MdiArea dosen't show the data
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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
    @

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Thanks ChrisW67. Would you please suggest a solution?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            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();
            @

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved