table model with 3d input
-
Hi,
Looks like a combination of QListView and QTableView. You can use QStandardItemModel for both. Or depending on what you want to do with the data create your own QAbstractTableModel and QAbstractListModel.
-
Hi,
Looks like a combination of QListView and QTableView. You can use QStandardItemModel for both. Or depending on what you want to do with the data create your own QAbstractTableModel and QAbstractListModel.
@SGaist said in table model with 3d input:
QAbstractListModel.
Thanks, so I can have a different number of table rows/cols for different values of z?
I tried to subclass QAbstractTableModel but seems I have a segmentation fault
Matrix model:
MatrixModel::MatrixModel(int numRows, int numColumns, double* data) : m_numRows(numRows), m_numColumns(numColumns), m_data(data) { } int MatrixModel::rowCount(const QModelIndex& parent ) const { return m_numRows; } int MatrixModel::columnCount(const QModelIndex& parent ) const { return m_numColumns; } QVariant MatrixModel::data(const QModelIndex& index, int role ) const { if (!index.isValid() || role != Qt::DisplayRole) return QVariant(); // Return the data to which index points. return m_data[index.row() * m_numColumns + index.column()]; }
and Mainwindow
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // Create a matrix. const int numRows = 3; const int numColumns = 1; double matrix[numRows][numColumns]; for (int i = 0; i < numRows; ++i) for (int j = 0; j < numColumns; ++j) matrix[i][j] = i + j; MatrixModel model(numRows, numColumns, (double*)matrix); ui->tableView->setModel(&model); }
it runs if ui->tableView->setModel(&model); is commented!
-
@SGaist said in table model with 3d input:
QAbstractListModel.
Thanks, so I can have a different number of table rows/cols for different values of z?
I tried to subclass QAbstractTableModel but seems I have a segmentation fault
Matrix model:
MatrixModel::MatrixModel(int numRows, int numColumns, double* data) : m_numRows(numRows), m_numColumns(numColumns), m_data(data) { } int MatrixModel::rowCount(const QModelIndex& parent ) const { return m_numRows; } int MatrixModel::columnCount(const QModelIndex& parent ) const { return m_numColumns; } QVariant MatrixModel::data(const QModelIndex& index, int role ) const { if (!index.isValid() || role != Qt::DisplayRole) return QVariant(); // Return the data to which index points. return m_data[index.row() * m_numColumns + index.column()]; }
and Mainwindow
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // Create a matrix. const int numRows = 3; const int numColumns = 1; double matrix[numRows][numColumns]; for (int i = 0; i < numRows; ++i) for (int j = 0; j < numColumns; ++j) matrix[i][j] = i + j; MatrixModel model(numRows, numColumns, (double*)matrix); ui->tableView->setModel(&model); }
it runs if ui->tableView->setModel(&model); is commented!
@mhsgoud
MatrixModel model
is a local variable. You cannot set a view's model to a local variable, since the view persists but the model it points to is destroyed. I would not expect it to crash on theui->tableView->setModel(&model);
statement itself, but on the}
following it which ends the function. During the exit from the function all local variables get destroyed. At some point here, or hereafter, the view tries to access a model which no longer exists. Give the model the same scope/lifetime as the view.Further in a similar vein, unless the
Model
constructor copies thematrix
parameter to it (does it?) it will be using/pointing to thedouble matrix[][]
array and that is a local variable too. That will leave the model still pointing onto the stack for itsmatrix
data after that stack area has been released/invalidated. Don't makematrix
a local variable, unlessMatrixModel
constructor takes a permanent copy of it.Finally, as a stylistic note passing/writing
(double*)matrix
is considered bad style in C++. Do not use C-style casts any longer. If you must cast, usestatic_cast<double *>(matrix)
here. -
Beside the very good point provide by @JonB, you should design your data structure in such a way that it also supports 3 dimensions.
One of the first thing to make clear is whether all your matrices have the same size. In any cases, it should be your data structure that provides the information for the model to use. As for managing the third dimension, you should add an API to the model that allows to set the z value and then trigger a refresh of the model when it has changed so that the view can update themselves.