如何改变 Qt TableView 的方向 [英] How to change orientation of Qt TableView

查看:98
本文介绍了如何改变 Qt TableView 的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I am using a QTableView to display data from a sql table using the qsqltablemodel asfollows:

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 实现它,该模型在本示例中不使用数据方法,而是一次填充整个模型的模型->select() 语句.任何指导将不胜感激.

I just want to display the table in such a view that columns are presented as rows and rows as columns. I have searched google etc.. but cannot find any simple solution. There is mention of a proxy model but I am unsure how i can implement that with a QSqltablemodel which does not use a data method in this example, rather a model->select() statement which populate the whole model at once. Any guidance will be appreciated.

推荐答案

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);

这篇关于如何改变 Qt TableView 的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆