无法从rowsInserted信号中获取数据

时间:2015-07-06 21:42:04

标签: python qt pyqt

当我尝试从rowsInserted信号中获取数据时,我正在使用Python 3.4.3和PyQt 5.4.2并遇到一些问题。

我希望在删除操作后从丢弃项目附近的所有项目中获取数据。

这里有一些我的代码(整个模型类太大了,希望这部分足够了):

class PlModel(QtGui.QStandardItemModel):
    def __init__(self):
        self.rowsInserted.connect(self.printSomeData)

    def supportedDragActions(self):
        return QtCore.Qt.MoveAction

    def supportedDropActions(self):
        return QtCore.Qt.CopyAction | QtCore.Qt.MoveAction

    def canDropMimeData(self, QMimeData, Qt_DropAction, p_int, p_int_1, QModelIndex):
        if not self.itemFromIndex(QModelIndex) and p_int_1 == p_int == -1:
            return False
        elif QModelIndex.isValid():
            if self.item(self.itemFromIndex(QModelIndex).row(), 0) is None:
                return False
            else:
                return QtGui.QStandardItemModel.canDropMimeData(self, QMimeData, Qt_DropAction, p_int, p_int_1, QModelIndex)
        else:
            return QtGui.QStandardItemModel.canDropMimeData(self, QMimeData, Qt_DropAction, p_int, p_int_1, QModelIndex)

    def dropMimeData(self, mimedata, dropaction, row, col, modelindex):
        if not self.itemFromIndex(modelindex):
            if row == col == -1:
                return False    
            else:
                return QtGui.QStandardItemModel.dropMimeData(self, mimedata, dropaction, row, 0, self.index(row, 0))
        elif self.itemFromIndex(modelindex).isDragEnabled():
            if row == col == -1:
                crow = self.item(modelindex.row(), 0).rowCount()
                return QtGui.QStandardItemModel.dropMimeData(self, mimedata, dropaction, crow, 0, self.indexFromItem(self.item(modelindex.row(), 0)))
            else:
                return QtGui.QStandardItemModel.dropMimeData(self, mimedata, dropaction, row, 0, modelindex)
        elif self.itemFromIndex(modelindex).isDropEnabled():
            if col != -1:
                return False
            else:
                return QtGui.QStandardItemModel.dropMimeData(self, mimedata, dropaction, row, 0, modelindex)
        else:
            return QtGui.QStandardItemModel.dropMimeData(self, mimedata, dropaction, row, 0, modelindex)

    def printSomeData(self, ind: QtCore.QModelIndex, first, last):
        row = ind.row()
        item = self.itemFromIndex(ind)
        for i in range(0, item.rowCount()):
            print(self.index(i, 0, ind).data(QtCore.Qt.DisplayRole))

此代码打印所有项目的有效数据,不包括已删除的项目。对于掉落的项目,它打印“无”。在QTreeView中放入后,所有数据都可以。怎么了?

抱歉我的英语不好。

1 个答案:

答案 0 :(得分:2)

看似rowsInserted信号在插入行之后但在模型填充插入行中的数据之前发出。 所以我从dropMimeData函数调用printSomeData函数(在数据被删除之后)并且它可以工作。

此处更正的代码:

class PlModel(QtGui.QStandardItemModel):
    def __init__(self):
        QtGui.QStandardItemModel.__init__(self)
        # no need for connect
        #self.rowsInserted.connect(self.printSomeData)

    def supportedDragActions(self):
        return QtCore.Qt.MoveAction

    def supportedDropActions(self):
        return QtCore.Qt.CopyAction | QtCore.Qt.MoveAction

    def canDropMimeData(self, QMimeData, Qt_DropAction, p_int, p_int_1, QModelIndex):
        if not self.itemFromIndex(QModelIndex) and p_int_1 == p_int == -1:
            return False
        elif QModelIndex.isValid():
            if self.item(self.itemFromIndex(QModelIndex).row(), 0) is None:
                return False
            else:
                return QtGui.QStandardItemModel.canDropMimeData(self, QMimeData, Qt_DropAction, p_int, p_int_1, QModelIndex)
        else:
            return QtGui.QStandardItemModel.canDropMimeData(self, QMimeData, Qt_DropAction, p_int, p_int_1, QModelIndex)

    def dropMimeData(self, mimedata, dropaction, row, col, modelindex):
        if not self.itemFromIndex(modelindex):
            if row == col == -1:
                return False    
            else:
                result = QtGui.QStandardItemModel.dropMimeData(self, mimedata, dropaction, row, 0, self.index(row, 0))
                if result:
                    self.printSomeData(self.index(row, 0))
                return result
        elif self.itemFromIndex(modelindex).isDragEnabled():
            if row == col == -1:
                crow = self.item(modelindex.row(), 0).rowCount()
                result = QtGui.QStandardItemModel.dropMimeData(self, mimedata, dropaction, crow, 0, self.indexFromItem(self.item(modelindex.row(), 0)))
                if result:
                    self.printSomeData(self.indexFromItem(self.item(modelindex.row(), 0)))
                return result
            else:
                result = QtGui.QStandardItemModel.dropMimeData(self, mimedata, dropaction, row, 0, modelindex)
                if result:
                    self.printSomeData(modelindex)
                return result
        elif self.itemFromIndex(modelindex).isDropEnabled():
            if col != -1:
                return False
            else:
                result = QtGui.QStandardItemModel.dropMimeData(self, mimedata, dropaction, row, 0, modelindex)
                if result:
                    self.printSomeData(modelindex)
                return result
        else:
            result = QtGui.QStandardItemModel.dropMimeData(self, mimedata, dropaction, row, 0, modelindex)
                if result:
                    self.printSomeData(modelindex)
                return result

    def printSomeData(self, ind: QtCore.QModelIndex):
        row = ind.row()
        item = self.itemFromIndex(ind)
        for i in range(0, item.rowCount()):
            print(self.index(i, 0, ind).data(QtCore.Qt.DisplayRole))