coderbob
11th December 2008, 09:26
From the docs on QModelIndex : "Note: Model indexes should be used immediately and then discarded. You should not rely on indexes to remain valid after calling model functions that change the structure of the model or delete items. If you need to keep a model index over time use a QPersistentModelIndex ".
My problem is the interpretation of the word "immediately". Are we talking a single atomic operation? A few calls? Multiple function calls? What about threading issues?
I understand checking if it isValid() before any work to ensure it is safe to use but it leaves me guessing how long it will be valid and if I need to re acquire an index mid function since it is no longer valid.
For me personally the code for Qt is hard to understand so when I look through the source I am not sure I am interpreting it properly. My guess would be that the index would remain valid as long as no data has changed in the model to force it to re optimize itself.
So this leaves me with QPersistentModelIndex. I do not have the source of the information at had to quote right now but I have "read" that using a QPersistentModelIndex comes as a significant overhead cost. That it must be updated any time the model changes to ensure that it is properly positioned.
So I can live with a short life cycle of a QModelIndex and don't really see any problems there other then unsure how long many cycles I can trust it validity.
Now I am going to need a QPersistentModelIndex in my model items since they will need to know there position independently of any model queries since they are passed around to other widgets and will update independently and I cannot guarantee QItemSelectionModel::Current will be the currently editing (widget edits data, it does not edit from the view) item.
After researching QTreeWidgetItem I cannot determine how it knows it's position in a view other then it must be internally keeping a QPersistentModelIndex.
My questions are:
1. Will keeping a QPersistentModelIndex with each item create significant amount of overhead? (Model size can possibly be very large)?
2. Is this how QTreeWidgetItem does this? Or is there a better way I am missing? (I will never be able to guarantee that the current selection is the item that is changing)
Thanks,
Bob
My problem is the interpretation of the word "immediately". Are we talking a single atomic operation? A few calls? Multiple function calls? What about threading issues?
I understand checking if it isValid() before any work to ensure it is safe to use but it leaves me guessing how long it will be valid and if I need to re acquire an index mid function since it is no longer valid.
For me personally the code for Qt is hard to understand so when I look through the source I am not sure I am interpreting it properly. My guess would be that the index would remain valid as long as no data has changed in the model to force it to re optimize itself.
So this leaves me with QPersistentModelIndex. I do not have the source of the information at had to quote right now but I have "read" that using a QPersistentModelIndex comes as a significant overhead cost. That it must be updated any time the model changes to ensure that it is properly positioned.
So I can live with a short life cycle of a QModelIndex and don't really see any problems there other then unsure how long many cycles I can trust it validity.
Now I am going to need a QPersistentModelIndex in my model items since they will need to know there position independently of any model queries since they are passed around to other widgets and will update independently and I cannot guarantee QItemSelectionModel::Current will be the currently editing (widget edits data, it does not edit from the view) item.
After researching QTreeWidgetItem I cannot determine how it knows it's position in a view other then it must be internally keeping a QPersistentModelIndex.
My questions are:
1. Will keeping a QPersistentModelIndex with each item create significant amount of overhead? (Model size can possibly be very large)?
2. Is this how QTreeWidgetItem does this? Or is there a better way I am missing? (I will never be able to guarantee that the current selection is the item that is changing)
Thanks,
Bob