Important: Please read the Qt Code of Conduct -

[Solved] How to show one-dimensional data two-dimensionally?

  • It's pretty easy to show 1d data one-dimensionally using QListView and 2d data two-dimensionally using QTableView. However I'm trying to show 1d data both one-dimensionally (easily done with QListView and a custom delegate) and also two-dimensionally (but how?).
    QTableView shows 1d data only one-dimensionally for some reason, so the data gets shown in exactly one row or column.

    QStandardItemModel model;
    for (int i = 1; i < 10; ++i) {
    QStandardItem *item = new QStandardItem(QString::number(i));

    QListView listView;

    QTableView tableView;

    I want the QTableView to show the 1d data in the example above like this:
    1 2 3
    4 5 6
    7 8 9

    and the QListView should show the data as usual

    One could probably replace the QTableView with a QListView with a custom QStyledItemDelegate which displays the data two-dimensonally, but then the user won't be able to select the items.

    So one has to create a custom QAbstractItemView to show 1d data two-dimensonally? Why can't this simple task be achieved with QTableView?
    There aren't many examples on how to create a custom view as one rarely needs to do that, but this seems to be the best way to solve this problem. I also asked for help on this problem "here":

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    QTableView is doing the right thing, your model has only one column and nine rows. So it's up to you tell QTableView that it's looking at something different.

    If you really don't want to use two models, you can implement a QAbstractProxyModel that will translate your one dimensional array in two dimension.

    Hope it helps

  • Thanks, the QAbstractProxyModel method worked fine.

  • Lifetime Qt Champion

    You're welcome !

    Since your visualization is now working, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

Log in to reply