Results 1 to 9 of 9

Thread: QModelIndex values unexpectedly alternates

  1. #1
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QModelIndex values unexpectedly alternates

    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/1...dly-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.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QModelIndex values unexpectedly alternates

    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.

  3. #3
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QModelIndex values unexpectedly alternates

    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.

    Qt Code:
    1. def __init__(self):
    2. self.selectionModel = QItemSelectionModel()
    3. self.selectionModel.currentChanged.connect(
    4. self._currentChanged_slot)
    5.  
    6. def _currentChanged_slot(self, qindex_curr, qindex_prev):
    7. self.selectionModel.select(qindex_curr,
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QModelIndex values unexpectedly alternates

    Quote Originally Posted by q130s View Post
    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 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.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QModelIndex values unexpectedly alternates

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QModelIndex values unexpectedly alternates

    Quote Originally Posted by d_stranz View Post
    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QModelIndex values unexpectedly alternates

    Quote Originally Posted by wysota View Post
    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,
    _

  8. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QModelIndex values unexpectedly alternates

    @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?:
    Qt Code:
    1. if len(indexes) > 0:
    2. print(tabular_format.format(
    3. del_or_sel, index_current, index_current.row(),
    4. index_current.data(Qt.DisplayRole).toString(),
    5. index_parent_sel, index_internalid))
    6. self.selectionModel.select(index_current,QItemSelectionModel.Deselect) # added statement
    7. elif len(deselected.indexes()) > 0:
    8. print(tabular_format.format(
    9. del_or_sel, index_deselected, index_deselected.row(),
    10. index_deselected.data(Qt.DisplayRole).toString(),
    11. index_parent_desel, index_internalid))
    12. #self.selectionModel.select(index_deselected,QItemSelectionModel.Deselect) # not necessary
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to norobro for this useful post:

    q130s (8th March 2013)

  10. #9
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QModelIndex values unexpectedly alternates

    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.

Similar Threads

  1. the program has unexpectedly finished
    By narlapavan in forum Qt Programming
    Replies: 9
    Last Post: 9th July 2012, 09:04
  2. How to convert string hex values to its real binary values?
    By AttilaPethe in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 22:47
  3. Program quits unexpectedly
    By MCFormax in forum Newbie
    Replies: 2
    Last Post: 9th November 2011, 02:31
  4. Program has unexpectedly finished
    By Maluko_Da_Tola in forum Newbie
    Replies: 5
    Last Post: 1st December 2010, 09:54
  5. Replies: 1
    Last Post: 18th November 2009, 18:06

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.