Search code examples
c++qtqt5qtableviewqtwidgets

Paradigmatic way to layout QTableView vertically


I have a QTableView that shows a model with many columns.

The model contains something like vector<my_item_with_lots_of_fields>; for most applications the size of vector is below 5.

For aesthetic reasons, the model would look better flipped so that each entry would run from top to bottom.

One dirty solution is to change the model so that the row and column indexing is switched. Unfortunately this would break other widgets that access the model.

Is there some easy, paradigmatic way to achieve this effect without changing the underlying model? Perhaps to change the widget?


Solution

  • I think one approach would be to create a proxy model for this. You would then need to do two changes:

    1) data method

    QVariant MyProxyModel::data(const QModelIndex & index,
                                int role = Qt::DisplayRole) const
    {
        return myTableModel::data(QModelIndex(index.column(), index.row()), role);
    }
    

    2) headerData method

    QVariant MyProxyModel::headerData(int section, Qt::Orientation orientation,
                                      int role = Qt::DisplayRole) const
    {
        return myTableModel::headerData(section,
               orientation == Qt::Horizontal ? Qt::Vertical : Qt::Horizontal, role);
    }