Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableView into 2D array
Qt 6.11 is out! See what's new in the release blog

QTableView into 2D array

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.2k Views
  • 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.
  • T Offline
    T Offline
    t0msk
    wrote on last edited by
    #1

    Hello, is possible to create 2D array in Qt, which will contains content from QTableView?

    I have QTableView (model) filled with data from MySQL database, but I would like to load these data into 2D array too.

    Student who loves C/C++

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      Since "2D array" is quite generic, I made the algorithm quite generic as well

      template<class T>
      void modelToArray(const QAbstractItemModel* const model, T& array2d, const QModelIndex& parent = QModelIndex(), int role = Qt::DisplayRole)
      {
      if(!model) return;
          const int rowCount = model->rowCount(parent);
          const int colCount = model->columnCount(parent);
          for (int i = 0; i < rowCount; ++i) {
              for (int j = 0; j < colCount; ++j) 
                  array2d[i][j] = model->index(i, j, parent).data(role).value<std::remove_reference<decltype(array2d[i][j])>::type>();
          }
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1

      • Login

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