首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >跟踪QTableView中行被移动的时间的PyQt信号

跟踪QTableView中行被移动的时间的PyQt信号
EN

Stack Overflow用户
提问于 2020-06-22 23:14:07
回答 1查看 113关注 0票数 1

我正在使用带有QTableViewQAbstractItemModel的子类化版本,并使用子类化的model.dropMimeData()model.insertRows()model.removeRows()激活了拖放。现在,我想要显示拖放操作完成后的更改,并让用户再次撤消该操作。因此,我为tableView实现了自己的dropEvent()-method。我还将move方法设置为InternalMove

我检查方法内部的移动确认,然后调用super(widget.__class__, widget).dropEvent(event)。我期望在此执行之后,行被插入到新位置,并在旧位置被删除。发生的情况是,它在指定位置插入行,但只有在dropEvent()完成后才删除旧位置的行。无论我是在函数内部调用event.accept()还是event.acceptProposedAction(),它都会一直等到dropEvent()完成。

我正在寻找一个信号,它告诉我拖放操作何时被执行。我希望QAbstractItemModelrowsMoved-signal是我想要的,但它不是在dnd操作期间发出的。然而,信号rowsInsertedrowsRemoved是发出的。但rowsRemoved信号是在dropEvent()完成时立即发出的。谁知道QTableView在哪里执行插入目标行、设置数据和删除源行的操作?

我在Windows10上使用python3和PyQt5。

代码语言:javascript
运行
复制
    def dropEvent_withConfirm(widget, event):
        dropInd = widget.indexAt(event.pos())
        if not dropInd.isValid():
            event.ignore()
            return

        confirmGUI = ConfirmGUI("Drag Element to new position?",
                                "Are you sure you want to drag the element to this position?",
                                True)
        if confirmGUI.getConfirmation():
            super(widget.__class__, widget).dropEvent(event)
            event.accept()
            # Here I want to do something after the finished move, but here the target row was inserted, but the source row is not yet deleted
        else:
            event.ignore()

    self.client.tblView.dropEvent = lambda e: dropEvent_withConfirm(self.client.tblView, e)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-25 17:56:59

我现在不依赖于super().dropEvent(),而是通过自己实现它来解决它。我找不到一个合适的信号,它是在完成drop时发出的。下面是我的更新代码:

代码语言:javascript
运行
复制
def dropEvent_withConfirm(widget, event):
    dropInd = widget.indexAt(event.pos())
    if not dropInd.isValid():
        event.ignore()
        return

    confirmGUI = ConfirmGUI("Drag Element to new position?",
                            "Are you sure you want to drag the element to this position?",
                            True)
    if confirmGUI.getConfirmation():
        old_row, new_row = getSrcAndDstRow()
        entry            = getDraggedEntry()
        self.myModel.insertRows(new_row)
        self.myModel.setRow(new_row, entry)
        if old_row > new_row:
            self.myModel.removeRows(old_row + 1)
        else:
            self.myModel.removeRows(old_row)
        event.accept()
        # Here I can now do something after the finished move
    else:
        event.ignore()

self.client.tblView.dropEvent = lambda e: dropEvent_withConfirm(self.client.tblView, e)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62517678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档