-
UID:80999
-
- 注册时间2009-06-19
- 最后登录2012-02-28
- 在线时间23小时
-
- 发帖16
- 搜Ta的帖子
- 精华0
- 金钱160
- 威望26
- 贡献值0
- 好评度16
-
访问TA的空间加好友用道具
|
楼主兄弟,还是我来告诉你吧,正如楼上的楼上所说, 定义一个RotatedProxyModel代理类: rotatedproxymodel.h文件 - #ifndef ROTATEDPROXYMODEL_H
- #define ROTATEDPROXYMODEL_H
- #include <QAbstractProxyModel>
- class QAbstractTableModel;
- // 交换行和列的代理model
- class RotatedProxyModel : public QAbstractProxyModel
- {
- Q_OBJECT
- public:
- RotatedProxyModel( QObject* parent );
- ~RotatedProxyModel();
- virtual QModelIndex index ( int row, int column, const QModelIndex & parent = QModelIndex() ) const;
- virtual QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const;
- virtual QModelIndex mapToSource ( const QModelIndex & proxyIndex ) const;
- virtual void setSourceModel( QAbstractTableModel* sourceModel );
- virtual int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
- virtual QModelIndex parent ( const QModelIndex & index ) const;
- virtual int rowCount ( const QModelIndex & parent = QModelIndex() ) const;
- virtual QVariant headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const;
- virtual bool setHeaderData ( int section, Qt::Orientation orientation, const QVariant & value, int role = Qt::EditRole );
- };
- #endif
rotatedproxymodel.cpp文件 - #include "rotatedproxymodel.h"
- RotatedProxyModel::RotatedProxyModel( QObject* parent ): QAbstractProxyModel( parent )
- {
- }
- RotatedProxyModel::~RotatedProxyModel()
- {
- }
- void RotatedProxyModel::setSourceModel( QAbstractTableModel* sourceModel )
- {
- QAbstractProxyModel::setSourceModel(sourceModel);
- }
- int RotatedProxyModel::columnCount( const QModelIndex & parent ) const
- {
- if( parent.isValid() )
- return 0;
- if( sourceModel() )
- return sourceModel()->rowCount( ); /* 代理model的columnCount等于sourceModel的rowCount */
- else
- return 0;
- }
- QModelIndex RotatedProxyModel::parent( const QModelIndex & index ) const
- {
- if( sourceModel() )
- return sourceModel()->parent(index);
- else
- return QModelIndex();
- }
- int RotatedProxyModel::rowCount( const QModelIndex & parent ) const
- {
- if( parent.isValid() )
- return 0;
- if( sourceModel() )
- return sourceModel()->columnCount( ); /* 代理model的rowCount等于sourceModel的columnCount */
- else
- return 0;
- }
- QModelIndex RotatedProxyModel::mapFromSource( const QModelIndex & sourceIndex ) const
- {
- if( sourceModel() )
- return sourceModel()->index( sourceIndex.column(), sourceIndex.row(), sourceIndex.parent() );
- else
- return QModelIndex();
- }
- QModelIndex RotatedProxyModel::mapToSource( const QModelIndex & proxyIndex ) const
- {
- if( sourceModel() )
- return sourceModel()->index( proxyIndex.column(), proxyIndex.row(), proxyIndex.parent() );
- else
- return QModelIndex();
- }
- QModelIndex RotatedProxyModel::index( int row, int column, const QModelIndex & parent ) const
- {
- if( sourceModel() )
- return sourceModel()->index( column, row, parent );
- else
- return QModelIndex();
- }
- QVariant RotatedProxyModel::headerData( int section, Qt::Orientation orientation, int role ) const
- {
- if( orientation == Qt::Horizontal )
- return sourceModel()->headerData( section, Qt::Vertical, role );
- else
- return sourceModel()->headerData( section, Qt::Horizontal, role );
- }
- bool RotatedProxyModel::setHeaderData( int section, Qt::Orientation orientation, const QVariant & value, int role )
- {
- if( orientation == Qt::Horizontal )
- return sourceModel()->setHeaderData( section, Qt::Vertical, value, role );
- else
- return sourceModel()->setHeaderData( section, Qt::Horizontal, value, role );
- }
使用这个类的方法: - standardView = new QTableView;
- QSqlTableModel *sourceModel = new QSqlTableModel;
- sourceModel->setTable("model_group");
- sourceModel->select();
- standardModel = new RotatedProxyModel(this);
- standardModel->setSourceModel(sourceModel);
- standardView->setModel(standardModel);
;
|