PDA

View Full Version : QModelIndex values unexpectedly alternates



q130s
18th February 2013, 20:21
I'm getting inconsistent index address (QModelIndex) from QTreeview. For the full working code and its result, please see this link where I posted the full description of the problem.

http://stackoverflow.com/questions/14646979/qmodelindex-values-unexpectedly-alternates

Yes it's stackoverflow...I asked this there nearly 3 weeks ago and haven't got any response, which made me think that not many Qt specialists might be checking that website. Hope I'm not violating the rule of this forum. I appreciate your response either to this forum or to the link I cite.

Thank you for your time.

Lykurg
18th February 2013, 20:51
Next time, please just copy and past the question, that the thread stays valid a long time even st*** closes.

And what exactly is the problem with getting different index address? Do you care where the objects are stored? Isn't the main point getting a valid value by row/column? Please elaborate.

q130s
18th February 2013, 21:57
Thanks for the response and guidance @Lykurg. The problem of getting different address from QModelIndex may not be necessarily a problem but I suspect it is the cause after many hours of observation.

The direct problem I'm facing is that my program fails to deselect a tree item when it's clicked by user. QItemSelectionModel.select method takes index as an argument to make the selection either selected or deselected, and as far as I've seen the method seems not to deselect when getting different address from previous selection action. For example, the following code doesn't deselect the item clicked.



def __init__(self):
self.selectionModel = QItemSelectionModel()
self.selectionModel.currentChanged.connect(
self._currentChanged_slot)

def _currentChanged_slot(self, qindex_curr, qindex_prev):
self.selectionModel.select(qindex_curr,
QItemSelectionModel.Deselect)

norobro
19th February 2013, 02:28
The direct problem I'm facing is that my program fails to deselect a tree item when it's clicked by user.

That should work "out of the box".

The ui file that you uploaded has the selection mode set to QAbstractItemView::MultiSelection. You haven't by any chance changed that to QAbstractItemView::SingleSelection (http://qt-project.org/doc/qt-4.8/qabstractitemview.html#SelectionMode-enum) have you?
When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item by clicking on it.

d_stranz
19th February 2013, 02:35
I'm not that familiar with Python, so I can't answer why your code might not work. But what you seem to be saying is that you want to deselect the item the user just clicked on. Isn't that the opposite behavior of what should happen (e.g. the current item is selected)?

However, QModelIndex is not guaranteed to return the same memory address each time the index is requested for a given row and column. That's why the docs make a big deal about persitent vs. non-persistent model indexes. If you implement your own model, then you are required to also implement the methods that return the index for a given location. Typically, this makes a new instance each time (and it is returned as a copy on the stack, so the address *will* be different each time). Therefore, if you store the address of a model index variable, then the address will cease to be valid as soon as that instance goes out of scope.

What you need to do is to store the location (row and column) of the item, or ask the model for a persistent index and store that if you want to use it later.

wysota
19th February 2013, 09:43
However, QModelIndex is not guaranteed to return the same memory address each time the index is requested for a given row and column.
I would even say it is bound to return a different object.

The point is that all of them will point to the same item in the model (which can be seen by comparing the internal pointer values).

anda_skoa
19th February 2013, 17:37
I would even say it is bound to return a different object.


Exactly!
Even if the model would return the same object every time (which it doesn't), the caller would always get a different one since the API returns a copy, not a reference.

Cheers,
_

norobro
19th February 2013, 18:40
@q130s - Sorry, I misunderstood your intent (reading comprehension problem:)).

Like d_stranz, I'm not that familar with Python but where in your original code do you deselect the clicked on item? Does the following change work?:
if len(indexes) > 0:
print(tabular_format.format(
del_or_sel, index_current, index_current.row(),
index_current.data(Qt.DisplayRole).toString(),
index_parent_sel, index_internalid))
self.selectionModel.select(index_current,QItemSele ctionModel.Deselect) # added statement
elif len(deselected.indexes()) > 0:
print(tabular_format.format(
del_or_sel, index_deselected, index_deselected.row(),
index_deselected.data(Qt.DisplayRole).toString(),
index_parent_desel, index_internalid))
#self.selectionModel.select(index_deselected,QItem SelectionModel.Deselect) # not necessary

q130s
28th February 2013, 07:27
I haven't noticed that there has been responses (no email update feature?) but thanks for that.

I think I worked it around by something similar to what norobro suggests...comparing qindex.data. Since this works fine for me I'll keep using it but next time when I implement the similar feature I'll think about storing row & column.