I'm using QAbstractTableModel subclassing and I can access to the date but when I try to associate theese data to a QtableView model I don't see anything.
This is mi code:
.cpp
#include "guiProductTable.h"
guiProductTable::guiProductTable(int numRows, int numColumns, QList<QMap<QString, QString> > productList)
: m_numRows(numRows),
m_numColumns(numColumns),
products(productList)
{
}
int guiProductTable::rowCount(const QModelIndex& parent) const
{
return m_numRows;
}
int guiProductTable::columnCount(const QModelIndex& parent) const
{
return m_numColumns;
}
QVariant guiProductTable
::data(const QModelIndex
& index,
int role
) const {
if (!index.isValid() || role != Qt::DisplayRole)
// Return the data to which index points.
switch (index.column()){
case 0:
return products.at(index.row())["name"];
break;
case 1:
return products.at(index.row())["id"];
break;
case 2:
return products.at(index.row())["price"];
break;
case 3:
return products.at(index.row())["barcode"];
break;
default:
}
}
#include "guiProductTable.h"
guiProductTable::guiProductTable(int numRows, int numColumns, QList<QMap<QString, QString> > productList)
: m_numRows(numRows),
m_numColumns(numColumns),
products(productList)
{
}
int guiProductTable::rowCount(const QModelIndex& parent) const
{
return m_numRows;
}
int guiProductTable::columnCount(const QModelIndex& parent) const
{
return m_numColumns;
}
QVariant guiProductTable::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || role != Qt::DisplayRole)
return QVariant();
// Return the data to which index points.
switch (index.column()){
case 0:
return products.at(index.row())["name"];
break;
case 1:
return products.at(index.row())["id"];
break;
case 2:
return products.at(index.row())["price"];
break;
case 3:
return products.at(index.row())["barcode"];
break;
default:
return QVariant();
}
}
To copy to clipboard, switch view to plain text mode
.h
#ifndef GUIPRODUCTTABLE_H
#define GUIPRODUCTTABLE_H
#include <QtGui>
#include <QtCore>
#include "guiProductTable.h"
{
//Q_OBJECT
public:
guiProductTable(int numRows, int numColumns, QList< QMap<QString,QString> > productList);
int rowCount
(const QModelIndex
& parent
= QModelIndex()) const;
int columnCount
(const QModelIndex
& parent
= QModelIndex()) const;
QVariant data
(const QModelIndex
& index,
int role
= Qt
::DisplayRole) const;
private:
int m_numRows;
int m_numColumns;
QList< QMap<QString,QString> > products;
};
#endif // GUIPRODUCTTABLE_H
#ifndef GUIPRODUCTTABLE_H
#define GUIPRODUCTTABLE_H
#include <QtGui>
#include <QtCore>
#include "guiProductTable.h"
class guiProductTable : public QAbstractTableModel
{
//Q_OBJECT
public:
guiProductTable(int numRows, int numColumns, QList< QMap<QString,QString> > productList);
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex()) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
private:
int m_numRows;
int m_numColumns;
QList< QMap<QString,QString> > products;
};
#endif // GUIPRODUCTTABLE_H
To copy to clipboard, switch view to plain text mode
guiProductTable productTable(productList.count(),4,productList);
ui->tableView->setModel(&productTable);
/* QVariant uno = productTable.data(productTable.index(0,0));
QVariant due = productTable.data(productTable.index(0,1));
QVariant tre = productTable.data(productTable.index(0,2));
QVariant quattro = productTable.data(productTable.index(0,3));
std::cout<<uno.toString().toStdString()<<due.toString().toStdString()<<tre.toString().toStdString()<<quattro.toString().toStdString();*/
guiProductTable productTable(productList.count(),4,productList);
ui->tableView->setModel(&productTable);
/* QVariant uno = productTable.data(productTable.index(0,0));
QVariant due = productTable.data(productTable.index(0,1));
QVariant tre = productTable.data(productTable.index(0,2));
QVariant quattro = productTable.data(productTable.index(0,3));
std::cout<<uno.toString().toStdString()<<due.toString().toStdString()<<tre.toString().toStdString()<<quattro.toString().toStdString();*/
To copy to clipboard, switch view to plain text mode
if i remove the comment at the bottom I can see the right output.
If i remove comment here: //Q_OBJECT i get a compiling error
Added after 1 8 minutes:
I'v founded the solution.
I had to move the model from stack area to the heap.
Bookmarks