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. QTableView doesn't show any data
Forum Updated to NodeBB v4.3 + New Features

QTableView doesn't show any data

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 6.3k 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.
  • T Offline
    T Offline
    thinkpad20
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tucnak
      wrote on last edited by
      #2

      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)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thinkpad20
          wrote on last edited by
          #4

          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!

          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