Tidy up / correct use of proxy model #273

Closed
opened 2024-12-25 09:35:57 +00:00 by kae · 0 comments
Owner

From stackoverflow:

In my app, I am using a QSortFilterProxyModel so the fix is slightly more complex:

to_index = self.indexAt(event.position().toPoint())
if (
    self.dropIndicatorPosition()
    == QAbstractItemView.DropIndicatorPosition.BelowItem
):
    proxy_index = self.proxy_model.createIndex(
        to_index.row() + 1,
        to_index.column(),
        to_index.internalId(),
    )
else:
    proxy_index = to_index
target_row = self.proxy_model.mapToSource(proxy_index).row()

The proxy part seems unnecessary and/or inconsistent: 1. if you want to get the index in the next row, you should just use QModelIndex.siblingAtRow(); 2. you should prefer index() and avoid createIndex() along with the internalId() unless you're completely sure about its behaviour; 3. in your original question you ask about QTableWidget which doesn't allow proxies, are you actually using a QTableView? 4. even if you are, the view should always act on its own model, not the source of its proxy. –
musicamante
CommentedJun 24 at 15:15

From [stackoverflow](https://stackoverflow.com/questions/78655695/odd-behaviour-dragging-and-dropping-in-pyq6-qtablewidget/78662615?noredirect=1#comment138688455_78662615): In my app, I am using a QSortFilterProxyModel so the fix is slightly more complex: ``` to_index = self.indexAt(event.position().toPoint()) if ( self.dropIndicatorPosition() == QAbstractItemView.DropIndicatorPosition.BelowItem ): proxy_index = self.proxy_model.createIndex( to_index.row() + 1, to_index.column(), to_index.internalId(), ) else: proxy_index = to_index target_row = self.proxy_model.mapToSource(proxy_index).row() ``` The proxy part seems unnecessary and/or inconsistent: 1. if you want to get the index in the next row, you should just use `QModelIndex.siblingAtRow()`; 2. you should prefer `index()` and avoid `createIndex()` along with the `internalId()` unless you're completely sure about its behaviour; 3. in your original question you ask about `QTableWidget` which doesn't allow proxies, are you actually using a `QTableView`? 4. even if you are, the view should always act on its own model, not the source of its proxy. – musicamante CommentedJun 24 at 15:15
kae closed this issue 2024-12-26 14:11:06 +00:00
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: kae/musicmuster#273
No description provided.