Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. "Rotate" TableView
Qt 6.11 is out! See what's new in the release blog

"Rotate" TableView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
6 Posts 3 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    romain.donze
    wrote on last edited by
    #1

    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.5

    and I'd like my table like that:

    name apple orange banana
    type fruit fruit fruit
    price 1.50 2.5 3.5

    Is there an easy way to do it with listView or tableVIew? or should I create my own component?

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you using a C++ model ? If so you can use a proxy that does that.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • R Offline
        R Offline
        romain.donze
        wrote on last edited by
        #3

        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;
        };
        
        JonBJ 1 Reply Last reply
        0
        • R romain.donze

          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;
          };
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @romain-donze
          I assume @SGaist was asking about proxy model because of https://doc-snapshots.qt.io/qt5-dev/qtransposeproxymodel.html

          This 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.

          1 Reply Last reply
          1
          • R Offline
            R Offline
            romain.donze
            wrote on last edited by romain.donze
            #5

            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

            JonBJ 1 Reply Last reply
            0
            • R romain.donze

              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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @romain-donze
              Then in your research you must have come across QTransposeProxyModel, 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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved