...//mainwindow.cpp#include <QTableView>...#include "mainwindow.h"#include "tablemodel.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ createCentralWdgt();...}...void MainWindow::createCentralWdgt(){ tableView = new QTableView(); tableModel = new TableModel(); tableView->setModel(tableModel); setCentralWidget(tableView);}...
...//mainwindow.h#include <QtGui/QMainWindow>class QTableView;class TableModel;class MainWindow : public QMainWindow{ Q_OBJECTpublic: MainWindow(QWidget *parent = 0); ...private: ... QTableView *tableView; TableModel *tableModel;... void createCentralWdgt();...};...
...//tablemodel.h#include <QAbstractTableModel>class TableModel : public QAbstractTableModel{ Q_OBJECTpublic: TableModel(QObject* = 0); int rowCount(const QModelIndex& = QModelIndex()) const; int columnCount(const QModelIndex& = QModelIndex()) const; QVariant data(const QModelIndex&, int = Qt::DisplayRole) const; QVariant headerData(int, Qt::Orientation, int = Qt::DisplayRole) const; Qt::ItemFlags flags(const QModelIndex&) const; bool insertRows(int, int, const QModelIndex& = QModelIndex()); bool removeRows(int, int, const QModelIndex& = QModelIndex()); bool insertColumns(int, int, const QModelIndex& = QModelIndex()); bool removeColumns(int, int, const QModelIndex& = QModelIndex()); ...};...
...//tablemodel.cpp#include "tablemodel.h"TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent) {...}int TableModel::rowCount(const QModelIndex &parent) const {...}int TableModel::columnCount(const QModelIndex &parent) const {...}QVariant TableModel::data(const QModelIndex &index, int role) const {...}QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {...}Qt::ItemFlags TableModel::flags(const QModelIndex &index) const {...}bool TableModel::insertRows(int position, int rows, const QModelIndex &index) {...}bool TableModel::removeRows(int position, int rows, const QModelIndex &index) {...}bool TableModel::insertColumns(int position, int columns, const QModelIndex &index) {...}bool TableModel::removeColumns(int position, int columns, const QModelIndex &index) {...}
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QAbstractTableModel>class TableModel : public QAbstractTableModel{ Q_OBJECTpublic: TableModel(QObject* = 0); int rowCount(const QModelIndex& = QModelIndex()) const; int columnCount(const QModelIndex& = QModelIndex()) const; QVariant data(const QModelIndex&, int = Qt::DisplayRole) const; QVariant headerData(int, Qt::Orientation, int = Qt::DisplayRole) const; Qt::ItemFlags flags(const QModelIndex&) const; bool insertRows(int, int, const QModelIndex& = QModelIndex()); bool removeRows(int, int, const QModelIndex& = QModelIndex()); bool insertColumns(int, int, const QModelIndex& = QModelIndex()); bool removeColumns(int, int, const QModelIndex& = QModelIndex());private: QList<QStringList> array;};#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H#define MAINWINDOW_H...#endif // MAINWINDOW_H