Unsolved "Rotate" TableView
-
Hi,
So my question is simple. Is there a way to "rotate" a tableView in QML so I could have columns as rows ans rows as columns?
For instance, in a normal tableview or listview, it would look like that:
name type price
apple fruit 1.50
orange fruit 2.5
banana fruit 3.5and I'd like my table like that:
name apple orange banana
type fruit fruit fruit
price 1.50 2.5 3.5Is there an easy way to do it with listView or tableVIew? or should I create my own component?
Thanks
-
Hi,
Are you using a C++ model ? If so you can use a proxy that does that.
-
Hi,
I am using a C++ model like that
MyCppModel { id: myModel container: myCppContainer }
with myCppContainer being something like that
class CppContainer : public QObject { public: CppContainer(QObject *parent=nullptr); // getter // setter private: QVecto<MyElement*> m_elements; }
and MyCppModel being like that
class MyCppModel : public QAbstractListModel { Q_OBJECT Q_PROPERTY(CppContainer *container READ getContainer WRITE setContainer NOTIFY sigContainerChanged) Q_PROPERTY(int count READ count NOTIFY sigCountChanged) public: explicit MyCppModel(QObject *parent = nullptr); enum { Role1 = Qt::UserRole Role2, Role3 }; // Basic functionality: int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; virtual QHash<int, QByteArray> roleNames() const override; CppContainer *getContainer() const; void setContainer(CppContainer *container); Q_INVOKABLE QVariantMap get(int row); int count() const; signals: void sigContainerChanged(); void sigCountChanged(); public slots: Q_INVOKABLE void refreshIndex(UInt32); private: CppContainer *m_container; };
-
@romain-donze
I assume @SGaist was asking about proxy model because of https://doc-snapshots.qt.io/qt5-dev/qtransposeproxymodel.htmlThis model will make the rows of the source model become columns of the proxy model and vice-versa.
Take a look at that, either to use directly or possibly to adapt to your case. The point being, the rows/columns are swapped/rotated at the model stage, for direct use in
QTableView
, rather than making the view have to deal with it. -
Hi,
after some research, I was thinking of subclassing QAbstractTableModel and reimplement the data() function to suit my needs.
But I can't find any example on how to subclass QAbstractTableModel properly.
Then I could simply use MyTableModel in a tableview
-
@romain-donze
Then in your research you must have come acrossQTransposeProxyModel
, for which I have given you the link, which is going to be close to what you need. So why don't you look at that? Source code can be seen in https://code.woboq.org/qt5/qtbase/src/corelib/itemmodels/qtransposeproxymodel.cpp.html. Even if you refuse to use a proxy model, you can see from that what sort of thing you will need to do in your own model.