gpt4 book ai didi

python - 无法从 rowsInserted 信号获取数据

转载 作者:太空宇宙 更新时间:2023-11-04 03:30:30 27 4
gpt4 key购买 nike

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

我想在放置操作后从放置项目附近的所有项目中获取数据。

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

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 后,所有数据都正常。有什么问题吗?

抱歉我的英语不好。

最佳答案

似乎 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))

关于python - 无法从 rowsInserted 信号获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31256157/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com