如何改变 Qt TableView 的方向

Posted

技术标签:

【中文标题】如何改变 Qt TableView 的方向【英文标题】:How to change orientation of Qt TableView 【发布时间】:2014-03-06 08:53:18 【问题描述】:

您好,我正在使用 QTableView 来显示使用 qsqltablemodel 的 sql 表中的数据,如下所示:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

    ui->setupUi(this);
    model = new QSqlTableModel(this);
    model->setTable("staging");
    model->select();
    model->setHeaderData(0, Qt::Vertical, tr("ID"));
    model->setHeaderData(1, Qt::Vertical, tr("Region"));
    model->setHeaderData(2, Qt::Vertical, tr("T1"));
    model->setHeaderData(3, Qt::Vertical, tr("N1"));
    model->setHeaderData(4, Qt::Vertical, tr("M1"));

    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->setModel(model);
    ui->tableView->showRow(1);
    ui->tableView->resizeColumnsToContents();


我只想在这样的视图中显示表格,即列显示为行,行显示为列。我搜索了谷歌等。但找不到任何简单的解决方案。提到了代理模型,但我不确定如何使用在此示例中不使用数据方法的 QSqltablemodel 来实现它,而是一次填充整个模型的 model->select() 语句。任何指导将不胜感激。

【问题讨论】:

【参考方案1】:
class Horizontal_proxy_model : public QAbstractProxyModel 
public:
  Horizontal_proxy_model(QObject * parent = 0);
  QModelIndex mapToSource(const QModelIndex &proxyIndex) const;
  QModelIndex mapFromSource(const QModelIndex &sourceIndex) const;
  QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
  QModelIndex parent(const QModelIndex &child) const;
  int rowCount(const QModelIndex &parent) const;
  int columnCount(const QModelIndex &parent) const;
  QVariant headerData(int section, Qt::Orientation orientation, int role) const;
;

Horizontal_proxy_model::Horizontal_proxy_model(QObject *parent) : 
  QAbstractProxyModel(parent) 


QModelIndex Horizontal_proxy_model::mapToSource(const QModelIndex &proxyIndex) const 
  if (sourceModel()) 
    return sourceModel()->index(proxyIndex.column(), proxyIndex.row());
   else 
    return QModelIndex();
  


QModelIndex Horizontal_proxy_model::mapFromSource(const QModelIndex &sourceIndex) const 
  return index(sourceIndex.column(), sourceIndex.row());


QModelIndex Horizontal_proxy_model::index(int row, int column, const QModelIndex &) const 
  return createIndex(row, column, (void*) 0);


QModelIndex Horizontal_proxy_model::parent(const QModelIndex &) const 
  return QModelIndex();


int Horizontal_proxy_model::rowCount(const QModelIndex &) const 
  return sourceModel() ? sourceModel()->columnCount() : 0;


int Horizontal_proxy_model::columnCount(const QModelIndex &) const 
  return sourceModel() ? sourceModel()->rowCount() : 0;


QVariant Horizontal_proxy_model::headerData(
           int section, Qt::Orientation orientation, int role) const 
  if (!sourceModel())  return QVariant(); 
  Qt::Orientation new_orientation = orientation == Qt::Horizontal ? 
      Qt::Vertical : Qt::Horizontal;
  return sourceModel()->headerData(section, new_orientation, role);

用法:

Horizontal_proxy_model* proxy_model = new Horizontal_proxy_model();
proxy_model->setSourceModel(model);
ui->tableView->setModel(proxy_model);

【讨论】:

【参考方案2】:

为 PyQt5 更新

从 PyQt5.QtCore 导入 Qt、QAbstractProxyModel、QModelIndex、QSortFilterProxyModel 类水平代理模型(QAbstractProxyModel): """将模型旋转 90 度""" def __init__(self, src, parent=None): 超级(水平代理模型,自我).__init__(父) self.setSourceModel(src) def mapToSource(self, proxyIndex: QModelIndex) -> QModelIndex: 如果 self.sourceModel(): return self.sourceModel().index(proxyIndex.column(), proxyIndex.row()) 别的: 返回 QModelIndex() def mapFromSource(self, sourceIndex: QModelIndex) -> QModelIndex: 返回 self.index(sourceIndex.column(), sourceIndex.row()) def index(self, row: int, column: int, parent: QModelIndex = ...) -> QModelIndex: 返回self.createIndex(行,列) def parent(self, child: QModelIndex) -> QModelIndex: 返回 QModelIndex() def rowCount(self, parent: QModelIndex = ...) -> int: 返回 self.sourceModel().columnCount() if self.sourceModel() else 0 def columnCount(self, parent: QModelIndex = ...) -> int: 返回 self.sourceModel().rowCount() if self.sourceModel() else 0 def headerData(self, section: int,orientation: Qt.Orientation, role: int = ...) -> typing.Any: 如果不是 self.sourceModel(): 返回无 new_orientation = Qt.Vertical iforientation == Qt.Horizo​​ntal else Qt.Horizo​​ntal return self.sourceModel().headerData(section, new_orientation, 角色)

【讨论】:

以上是关于如何改变 Qt TableView 的方向的主要内容,如果未能解决你的问题,请参考以下文章

QTableView 获取选中行内容并赋值给另一个tableview的问题,求QT高手解救。。。

如何判断tableview下拉和下拉

有没有办法改变TableView的焦点可遍历行为?

求问QT creator的table view一些问题……

QT如何实现QSqltablemodel实时更新数据库,并在tableview中实时显示,

QT中table view怎么显示sqlite数据库的内容?