struct myStruct{ int a; string b; stricg c;}
int myModel::columnCount(const QModelIndex &parent) const{ return 3;}QVariant myModel::data(const QModelIndex &index, int role) const{ if (!index.isValid()) return QVariant(); if (index.row() >= addrSeq.size()) return QVariant(); if (role == Qt::DisplayRole) { struct myStruct desc = addrSeq[index.row()]; switch (index.column()) { case 0: return QString("%1").arg(desc.a); case 1: return QString::fromUtf8(desc.b.c_str()); case 2: return QString::fromUtf8(desc.c.c_str()); } } else return QVariant();}QVariant myTableModel::headerData(int section, Qt::Orientation orientation, int role) const{ if (role != Qt::DisplayRole) return QVariant(); if (orientation == Qt::Horizontal) return QString("Column %1").arg(section); else return QString("Row %1").arg(section);}
struct myStruct desc = addrSeq[index.row()];
const struct myStruct& desc = addrSeq[index.row()];
switch (index.column()) { case 0: return QString("%1").arg(desc.a); case 1: return QString::fromUtf8(desc.b.c_str()); case 2: return QString::fromUtf8(desc.c.c_str()); }
struct myStruct1{ int a; string b; stricg c;}struct myStruct2{ int d; int e; stricg f;}
struct Point3D{ Point3D( float _x = 0, float _y = 0, float _z = 0 ) : x(_x), y(_y), z(_z) {} float x; float y; float z; QVariant data(int i) const { return (i == 0) ? x : (i == 1) ? y : (i == 2) ? z : QVariant(); } static int count() { return 3; } static QStringList names() { return QStringList() << "X" << "Y" << "Z"; }};
class AbstractTableData {public: virtual QVariant data( int row, int col ) const = 0; virtual int rowCount() const = 0; virtual int columnCount() const = 0; virtual QVariant headerData(int) const { return QVariant(); }};
template <class T>class TableData : public AbstractTableData, public QVector<T>{public: QVariant data( int row, int col ) const { return value(row).data(col); } int rowCount() const { return size(); } int columnCount() const { return T::count(); } QVariant headerData(int i) const { return T::names().value(i); }};
class TableModel : public QAbstractTableModel{ Q_OBJECTpublic: TableModel( AbstractTableData* data, QObject* parent ) : QAbstractTableModel(parent), m_data(data) {} int rowCount ( const QModelIndex& ) const { return m_data->rowCount(); } int columnCount ( const QModelIndex& ) const { return m_data->columnCount(); } QVariant data ( const QModelIndex& index, int role ) const { return role == Qt::DisplayRole ? m_data->data( index.row(), index.column() ) : QVariant(); } QVariant headerData ( int section, Qt::Orientation orientation, int role ) const { if (role == Qt::DisplayRole) return (orientation == Qt::Horizontal) ? m_data->headerData( section ) : section + 1; return QVariant(); }private: AbstractTableData* m_data;};
int main( int argc, char** argv ){ QApplication app( argc, argv ); TableData<Point3D> data; for ( int i = 0; i < 100; i++ ) data << Point3D( i % 10, i / 10, (i % 10) * (i / 10) ); TableModel model( &data, 0 ); QTableView view; view.setModel( &model ); view.resize( 400, 600 ); view.show(); return app.exec();}