Say I have a one-dimensional QStandardItemModel and a QTableView instance:
QStandardItemModel model;
for (int i = 1; i < 10; ++i) {
QStandardItem *item = new QStandardItem(QString::number(i));
model.appendRow(item);
}
QTableView tableView;
tableView.setModel(&model);
tableView.show();
This shows the data in the first column, but I want to show it in a two-dimensional way like this:
1 2 3
4 5 6
7 8 9
Additionally the user should be able to select the data, which means that a custom QStyledItemDelegate probably isn't the way to go to implement this.
So one needs to create a custom QAbstractItemView, where the documentation is unfortunately a bit lacking in my opinion. Help?
First of all, if you want show data as 2d array, you shoul write another loop. For example this:
On my computer this works exactly as you want.
Secondly. User can do different actions with cells. QTableView have a few very good signals. With this signal, we can communicate with cells. I write one more code snippet.
In this example, when user clicked on some cell, text in the cell tell him number of this cell. I hope,it helps.