PyQt:从列表视图拖动项并放到表视图中,删除索引总是1

2024-10-02 04:30:20 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的代码中,我成功地实现了从一个QListView到另一个QListView的拖放操作,并且内部移动也运行良好。 现在,由于需要,我修改了接受drops的视图,也就是说,我试图从QListView拖动到QTableView。在

问题是,当我删除QTableView时,每次它打印无效的索引,即-1(在dropEvent()中打印该索引)。在

在我以前的列表视图之间的实现中,即使将项放在项目之间,索引也会得到适当的更新。这里不是这样。谢谢你的回答。在

class SerialTestStepListView(QtGui.QTableView):
    itemSelectionChanged = pyqtSignal()
    casualSignal3 = pyqtSignal()
    casualSignal4 = pyqtSignal()

    def __init__(self,parent = None):
        QListView.__init__(self, parent)
        self.setAcceptDrops(True)
        # Hide column here......
        self.setSelectionMode(self.SingleSelection) 
        self.setDragDropMode(self.InternalMove)
        self.setSelectionBehavior(self.SelectRows)

    def dragEnterEvent(self, event):
        if event.mimeData().hasFormat("application/xml-chirag"):
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasFormat("application/xml-chirag"):
            event.setDropAction(QtCore.Qt.MoveAction)
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        data = event.mimeData()
        bstream = data.retrieveData("application/xml-chirag", QtCore.QVariant.ByteArray)
        selected = pickle.loads(bstream)
        index = self.indexAt(event.pos()).row()
        print("into the drop event")
        print(index)  # This is printing -1
        print(self.indexAt(event.pos()))
        self.emit(SIGNAL("casualSignal3"),selected, index)
        event.accept()

    def startDrag(self, event):
        indx = self.indexAt(event.pos())
        index = indx.row()
        print("into the drag event")
        self.emit(SIGNAL("casualSignal4"),indx, index)
        if not indx.isValid():
            pass
        else:
            return True

    def mouseMoveEvent(self, event):
        self.startDrag(event)

型号为:

^{pr2}$

控制器部件:

SerialTestStepListViewHdlr = CTC.SerialTestStepListView()
    SerialTestStepListViewHdlr.show()
    SerialTestStepListViewHdlr.connect(SerialTestStepListViewHdlr, SIGNAL("casualSignal3"), acceptDrag)


def acceptDrag(selected, index):
    SerialTestStepListModel = mod.SerialTestListModel(testStep)
    #selected = str(selected)
    SerialTestStepListModel.insertRows(index, 1, selected)
    SerialTestStepListViewHdlr.setModel(SerialTestStepListModel)

Tags: selfeventindexifapplicationdefprintselected
1条回答
网友
1楼 · 发布于 2024-10-02 04:30:20

更换后解决了:

index = self.indexAt(event.pos()).row()

^{pr2}$

在dropEvent()方法中。在

相关问题 更多 >

    热门问题