Results 1 to 5 of 5

Thread: With QTableView, `previous.indexes()` is sometime missing cell coordinates.

  1. #1
    Join Date
    Jul 2020
    Posts
    3
    Thanks
    1

    Default With QTableView, `previous.indexes()` is sometime missing cell coordinates.

    The code below, which maintains a set containing the coordinates of currently selected cells, works most of the time. But, after editing a cell by double-clicking, `previous.indexes` is missing the coordinates of the cell that was just edited. As a result, errors in `self.final.selection` build up over time. Is there any way to prevent this from happening?

    ```
    def selectionChanged(self, current, previous):
    """
    The names `current` and `previous` are misleading. `previous` provides information about
    cells that were deselected since the last invocation. `current` provides information
    about cells that were just selected.
    """

    self.final.previous= []
    self.final.current = []

    for index in previous.indexes():
    r= index.row()
    c= self.model.column_map[index.column()]

    self.final.previous .append ((r, c))
    self.final.selection.discard((r, c))

    for index in current.indexes():
    r= index.row()
    c= self.model.column_map[index.column()]

    self.final.current .append ((r, c))
    self.final.selection.add ((r, c))

    self.update_sig.emit(self)

    # If the application provided a callback function, invoke it:
    if self.selection_changed:
    self.selection_changed(self.final.previous, self.final.current, self.final.selection)
    ```

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,689
    Thanks
    257
    Thanked 750 Times in 740 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: With QTableView, `previous.indexes()` is sometime missing cell coordinates.

    The names `current` and `previous` are misleading. `previous` provides information about
    cells that were deselected since the last invocation. `current` provides information
    about cells that were just selected.
    Actually, if you look at the official Qt docs for QAbstractItemView::selectionChanged() you will see that the two arguments are named "selected" and "deselected", respectively. Whatever python Qt wrapping you are using has named them in a misleading way.

    In general, if your model changes as a result of editing or some other action, you cannot be guaranteed that any QModelIndex will be valid after the changes are processed. QModelIndex will only be valid during the scope of whatever method has provided it to you (signal, index(), etc.). See the discussion in the Qt docs, especially concerning QPersistentModelIndex.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jul 2020
    Posts
    3
    Thanks
    1

    Default Re: With QTableView, `previous.indexes()` is sometime missing cell coordinates.

    I've been attempting to determine what's currently selected by accumulating information from `selected` and `deselected`, but this eventually diverges from the truth. Is there a direct way to get information re. what cells are currently selected?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,689
    Thanks
    257
    Thanked 750 Times in 740 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: With QTableView, `previous.indexes()` is sometime missing cell coordinates.

    You should probably be using a QItemSelectionModel on your view.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    502
    Thanked 75 Times in 71 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: With QTableView, `previous.indexes()` is sometime missing cell coordinates.

    Quote Originally Posted by pfeldman View Post
    Is there a direct way to get information re. what cells are currently selected?
    That's exactly what the parameter selected in selectionChanged() contains and the same as QItemSelectionModel::selectedIndexes()

Similar Threads

  1. Replies: 5
    Last Post: 18th August 2017, 16:48
  2. Replies: 21
    Last Post: 13th August 2013, 12:38
  3. Replies: 2
    Last Post: 31st July 2013, 10:46
  4. QTableView Hidden Sections and Indexes
    By mechsin in forum Newbie
    Replies: 1
    Last Post: 15th July 2012, 17:49
  5. Replies: 4
    Last Post: 18th March 2010, 10:11

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.