您好我正在使用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来实现它,该QSqltablemodel在本例中不使用数据方法,而是一次使用model-> select()语句来填充整个模型。任何指导将不胜感激。
答案 0 :(得分:3)
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);
答案 1 :(得分:0)
已针对PyQt5更新
from PyQt5.QtCore import Qt, QAbstractProxyModel, QModelIndex, QSortFilterProxyModel class HorizontalProxyModel(QAbstractProxyModel): """Rotates model 90 degrees""" def __init__(self, src, parent=None): super(HorizontalProxyModel, self).__init__(parent) self.setSourceModel(src) def mapToSource(self, proxyIndex: QModelIndex) -> QModelIndex: if self.sourceModel(): return self.sourceModel().index(proxyIndex.column(), proxyIndex.row()) else: return QModelIndex() def mapFromSource(self, sourceIndex: QModelIndex) -> QModelIndex: return self.index(sourceIndex.column(), sourceIndex.row()) def index(self, row: int, column: int, parent: QModelIndex = ...) -> QModelIndex: return self.createIndex(row, column) def parent(self, child: QModelIndex) -> QModelIndex: return QModelIndex() def rowCount(self, parent: QModelIndex = ...) -> int: return self.sourceModel().columnCount() if self.sourceModel() else 0 def columnCount(self, parent: QModelIndex = ...) -> int: return self.sourceModel().rowCount() if self.sourceModel() else 0 def headerData(self, section: int, orientation: Qt.Orientation, role: int = ...) -> typing.Any: if not self.sourceModel(): return None new_orientation = Qt.Vertical if orientation == Qt.Horizontal else Qt.Horizontal return self.sourceModel().headerData(section, new_orientation, role)