3

I have a QTreeView in which each node represents a data object. I managed to pack a pointer to this data objects into a QVariant so that I know which is selected in the Tree. I can access the nodes by the currentIndex() function.

The root of the Tree is at QStandardItemModel::invisibleRootItem(). Other nodes are added by the parent nodes appendRow function. So the Items have no row and column that I can use in QStandardItemModel::item().

Is there a way to give the nodes valid coordinates or to retrieve the items without coordinates.

Thomas Klier
  • 449
  • 4
  • 16

1 Answers1

5

In QStandardItemModel, there is a item(int row, int column = 0) method. Qt nomenclature doesn't use 'get' as prefix of getters methods.

Antwane
  • 20,760
  • 7
  • 51
  • 84
  • Thanks. I was always looking in QAbstractItemModel. But I can't use row and column as in a tree they are relative to the parent node. – Thomas Klier May 21 '15 at 09:14
  • 1
    Nope, item() always returns 0. `for (int i = -100; i < 100; i++) for (int j = -100; j < 100; j++) if (static_cast(m_ui->treeView->model()).item(i, j) != 0) qDebug() << i << j << static_cast(m_ui->treeView->model()).item(i, j);` --> No output. – Thomas Klier May 21 '15 at 09:29
  • Thanks ! that was the thing I need it so much :) – SdSaati Dec 19 '18 at 06:06
  • One of the best examples of SO non-answers. How does this answer the question if OP stated that `row` and `column` are useless if `QStandardItemModel` models a tree? (Maybe this part was added to the question after the answer, but still...so frustrating) – z33k Oct 04 '19 at 07:59
  • You're right, but as you may have noticed, the original question was drastically different. Before taking time to post a disrespectful comment, maybe you could take a few more time to check the history of this SO post ? The original question was "Why is there no getItem() method in QStandardItemModel?". This answer seems perfectly valid. But OP reworded his question later, and the answer is now pointless. Continue to search a solution to your problem, but please avoid this kind of behavior on SO. Thanks – Antwane Oct 04 '19 at 11:01