QTableView doesn't show any data



  • I'm trying to figure out how to use tables in Qt and I'm using the Model/View tutorial from Nokia: http://doc.qt.nokia.com/4.7-snapshot/modelview.html

    Using the code in this tutorial for the first example (2.1 A Read-Only Table), I tried to add a table to a Qt GUI application I had been building. Although I pretty much followed the code to the letter, the table doesn't show anything when the program runs. I'm using the WYSIWYG editor for the project, so a lot of the code is hidden. But I think I included the necessary code. The model code is almost a straight copy from that tutorial, but I'll post it here:

    pointsmodel.h
    @#ifndef POINTSMODEL_H
    #define POINTSMODEL_H

    #include <QAbstractTableModel>

    class PointsModel : public QAbstractTableModel
    {
    Q_OBJECT

    public:
    PointsModel(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 // POINTSMODEL_H
    @

    pointsmodel.cpp
    @#include "pointsmodel.h"
    #include <QDebug>

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

    int PointsModel::rowCount(const QModelIndex & /parent/) const
    {
    qDebug() << QString("call to rowCount");
    return 5;
    }

    int PointsModel::columnCount(const QModelIndex & /parent/) const
    {
    qDebug() << QString("call to columnCount");
    return 3;
    }

    QVariant PointsModel::data(const QModelIndex &index, int role) const
    {
    int row = index.row();
    int col = index.column();
    qDebug() << QString("call to data: row %1, col%2, role %3")
    .arg(row).arg(col).arg(role);
    if (role == Qt::DisplayRole)
    {
    return QString("Row%1, Column%2")
    .arg(index.row() + 1)
    .arg(index.column() +1);
    }
    return QVariant();
    }
    @

    And then in the constructor of my main window, after adding the qtableview widget to the layout in the WYSIWYG editor with the objectname pointsTableView:
    @ PointsModel pModel(0);
    ui->pointsTableView->setModel( &pModel );@

    Having done this, it loads up blank. Any help? Thanks a ton!



  • bq. I’m trying to figure out how to use tables in Qt

    There are two types of tables - views and item-based.

    QTableView based on model and represent view.
    QTableWidget based on items (QTableItem)



  • Your main problem is with this snippet:

    @
    PointsModel pModel(0);
    ui->pointsTableView->setModel( &pModel );
    @

    This is where your code deviates from that of the example, I bet. The problem is that you are creating the model on the stack. As soon as pModel goes out of scope, it is destroyed again. That is before it even could be displayed. Simply create the model on the heap (and choose a suitable parent object for it), and your code should work just fine.

    [quote author="tucnak" date="1341215352"]bq. I’m trying to figure out how to use tables in Qt

    There are two types of tables - views and item-based.

    QTableView based on model and represent view.
    QTableWidget based on items (QTableItem)

    [/quote]

    I fail to see how that is relevant to the question. The TS clearly was working with the model/view based version, as he defined his own model.



  • I really appreciate it Andre! I followed your advice, changed some things around and now it's displaying. I can't say I'm 100% solid on stack/heap stuff and why it needed to be that way, but it gives me some reading to do. Thanks a ton!


Log in to reply
 

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