PDA

View Full Version : [SOLVED] can't get QTableView working



marvin
26th February 2011, 12:08
I'm trying to get QTableView working, from this example: http://doc.qt.nokia.com/latest/model-view-programming.html#a-read-only-example-model

so i made a new project in creator, added my custom class (a copy paste from example):
mymodel.h

#ifndef MYMODEL_H
#define MYMODEL_H

#include <QAbstractTableModel>

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 // MYMODEL_H


mymodel.cpp

#include "mymodel.h"
#include "QDebug"

MyModel::MyModel(QObject *parent)
:QAbstractTableModel(parent)
{
qDebug() << "constructor";
}

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


and in dialog.cpp i have push button on click, where i add the model to the table:
dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"
#include "mymodel.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{

ui->setupUi(this);
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::on_pushButton_clicked()
{
MyModel myModel(0);
ui->tableView->setModel( &myModel );
ui->tableView->show();


}


and when i run the example and click the button, i get this, nothing in table:

5986

i can just guess it's something basic i have missed, but i have no idea where?

Added after 1 40 minutes:

Found the solution, i created myModel locally... :o