#include <QApplication>#include <QStandardItemModel>#include <QAbstractProxyModel>#include <QTableView>#include <QSplitter>class TransposeProxyModel : public QAbstractProxyModel{public: TransposeProxyModel(QObject *p = 0) : QAbstractProxyModel(p){} QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const{ return index(sourceIndex.column(), sourceIndex.row()); } QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const{ return sourceModel()->index(proxyIndex.column(), proxyIndex.row()); } QModelIndex index(int r, int c, const QModelIndex &ind=QModelIndex()) const{ return createIndex(r,c); } QModelIndex parent(const QModelIndex&) const { return QModelIndex(); } int rowCount(const QModelIndex &) const{ return sourceModel()->columnCount(); } int columnCount(const QModelIndex &) const{ return sourceModel()->rowCount(); } QVariant data(const QModelIndex &ind, int role) const { return sourceModel()->data(mapToSource(ind), role); }};int main(int argc, char **argv){ QApplication app(argc, argv); QStandardItemModel model(3,3); model.setData(model.index(0,0), "1"); model.setData(model.index(0,1), "2"); model.setData(model.index(0,2), "3"); model.setData(model.index(1,0), "4"); model.setData(model.index(1,1), "5"); model.setData(model.index(1,2), "6"); model.setData(model.index(2,0), "7"); model.setData(model.index(2,1), "8"); model.setData(model.index(2,2), "9"); TransposeProxyModel trans; trans.setSourceModel(&model); QSplitter split; QTableView *t1 = new QTableView(&split); t1->setModel(&model); QTableView *t2 = new QTableView(&split); t2->setModel(&trans); split.show(); return app.exec();}
TransposeProxyModel trans1,trans2; trans1.setSourceModel(&model); trans2.setSourceModel(&model); QSplitter split; QTableView *t1 = new QTableView(&split); t1->setModel(&trans1); QTableView *t2 = new QTableView(&split); t2->setModel(&trans2);
#include <QApplication>#include <QStandardItemModel>#include <QAbstractProxyModel>#include <QTableView>#include <QSplitter>class TransposeProxyModel : public QAbstractProxyModel{ Q_OBJECTpublic: TransposeProxyModel(QObject *p = 0) : QAbstractProxyModel(p) {} QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const { return index(sourceIndex.column(), sourceIndex.row()); } QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const { return sourceModel()->index(proxyIndex.column(), proxyIndex.row()); } QModelIndex index( int r, int c, const QModelIndex& idx = QModelIndex() ) const { Q_UNUSED(idx) return createIndex( r, c ); } QModelIndex parent( const QModelIndex& ) const { return QModelIndex(); } int rowCount( const QModelIndex & ) const{ return sourceModel()->columnCount(); } int columnCount( const QModelIndex & ) const{ return sourceModel()->rowCount(); } QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const { Qt::Orientation srcOrient = ( orientation == Qt::Vertical ) ? Qt::Horizontal : Qt::Vertical; return sourceModel()->headerData( section, srcOrient, role ); } void setSourceModel( QAbstractItemModel * srcModel ) { QAbstractProxyModel::setSourceModel( srcModel ); connect( srcModel, SIGNAL(dataChanged(QModelIndex, QModelIndex)), this, SLOT(onSourceDataChanged(QModelIndex, QModelIndex)) ); }protected slots: void onSourceDataChanged( const QModelIndex& topLeft, const QModelIndex& botRight ) { emit dataChanged( mapFromSource(topLeft), mapFromSource(botRight) ); }};int main( int argc, char **argv ){ QApplication app(argc, argv); const int nRow = 5; const int nCol = 4; QStandardItemModel model( nRow, nCol ); for ( int row = 0; row < nRow; ++row ) { model.setHeaderData( row, Qt::Vertical, QString('A' + row) ); for ( int col = 0; col < nCol; ++col ) model.setData( model.index( row, col ), nCol * row + col + 1 ); } TransposeProxyModel trans; trans.setSourceModel(&model); QSplitter split; QTableView *srcView = new QTableView(&split); srcView->setModel(&model); QTableView *transView = new QTableView(&split); transView->setModel(&trans); split.show(); return app.exec();}#include "main.moc"