Addded HorizontalProxyModel which is used to effictively rotate a table 90 degrees.

This commit is contained in:
eelke 2018-08-25 18:14:08 +02:00
parent 50cb21b6f9
commit 68a1a8e7c9
3 changed files with 78 additions and 2 deletions

View file

@ -0,0 +1,56 @@
#include "HorizontalProxyModel.h"
/*
* Code borrowed from: https://stackoverflow.com/questions/21653253/how-to-change-orientation-of-qt-tableview
*/
HorizontalProxyModel::HorizontalProxyModel(QObject *parent)
: QAbstractProxyModel(parent)
{
}
QModelIndex HorizontalProxyModel::mapToSource(const QModelIndex &proxyIndex) const
{
if (sourceModel()) {
return sourceModel()->index(proxyIndex.column(), proxyIndex.row());
}
else {
return QModelIndex();
}
}
QModelIndex HorizontalProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
{
return index(sourceIndex.column(), sourceIndex.row());
}
QModelIndex HorizontalProxyModel::index(int row, int column, const QModelIndex &) const
{
return createIndex(row, column, nullptr);
}
QModelIndex HorizontalProxyModel::parent(const QModelIndex &) const
{
return QModelIndex();
}
int HorizontalProxyModel::rowCount(const QModelIndex &) const
{
return sourceModel() ? sourceModel()->columnCount() : 0;
}
int HorizontalProxyModel::columnCount(const QModelIndex &) const
{
return sourceModel() ? sourceModel()->rowCount() : 0;
}
QVariant HorizontalProxyModel::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);
}

View file

@ -0,0 +1,18 @@
#ifndef HORIZONTALPROXYMODEL_H
#define HORIZONTALPROXYMODEL_H
#include <QAbstractProxyModel>
class HorizontalProxyModel : public QAbstractProxyModel {
public:
HorizontalProxyModel(QObject * parent = nullptr);
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;
};
#endif // HORIZONTALPROXYMODEL_H

View file

@ -73,7 +73,8 @@ SOURCES += main.cpp\
Module.cpp \
EditorGutter.cpp \
CodeEditor.cpp \
PlgPage.cpp
PlgPage.cpp \
HorizontalProxyModel.cpp
HEADERS += \
QueryResultModel.h \
@ -119,7 +120,8 @@ HEADERS += \
EditorGutter.h \
CodeEditor.h \
PlgPage.h \
AbstractCommand.h
AbstractCommand.h \
HorizontalProxyModel.h
FORMS += mainwindow.ui \
ConnectionManagerWindow.ui \