QTableView into 2D array



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



  • 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>();
        }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.