first of all you can get your string directly from QModelIndex:
index.data(Qt::DisplayRole).toString();
index.data(Qt::DisplayRole).toString();
To copy to clipboard, switch view to plain text mode
if it is in the column 0. If not then:
index = index.model()->index(index.row(), 0, index.parent());
index = index.model()->index(index.row(), 0, index.parent());
To copy to clipboard, switch view to plain text mode
should work I think.
Second of all: is your model a tree structure? If yes then you forgot about parents. item(i, 0) gives you top level item in row i and column 0. So when you have some child nodes it will work incorrectly when clicking child node, because it will try to get text from top level item in the same row as your clicked child item.
Third of all: item(i, 0)->text() already return QString so no need to construct QString copy which would be copied and destructed in the next line, so it should be:
return item(i, 0)->text();
return item(i, 0)->text();
To copy to clipboard, switch view to plain text mode
Fourth of all: it is good to check first if pointer returned by item(i. 0) is valid. For example in your "parents case" item at row i and column 0 may not exist so item() will return 0. Using ->text() on null pointer gives you what? That's right: Segmentation fault! :]
Bookmarks