使用QListView和QStandardItemModel实现拖放重新排序时遇到了一些麻烦。使用itemChanged信号处理已检查的项目很好,但是当使用拖放重新排序项目时,似乎在触发信号时会创建临时项目。列表中有5个项目,但每次重新排序项目时,模型行计数变为6,即使最终只有5个可见。当我专门设置要移动的动作时,为什么它会暂时变为6?我想处理新订单中的5个项目,因为列表直接用于更新另一个小部件。我尝试了dataChange信号,但没有区别。
我的设置是在Win 7上使用Python 2.7.10的PyQt 4.11.4。
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
if __name__ == '__main__':
app = QApplication(sys.argv)
# Our main window will be a QListView
list = QListView()
list.setDragDropMode(QAbstractItemView.InternalMove)
list.setDefaultDropAction(Qt.MoveAction)
list.setDragDropOverwriteMode(False)
list.setAcceptDrops(True)
list.setDropIndicatorShown(True)
list.setDragEnabled(True)
list.setWindowTitle('Example List')
list.setMinimumSize(600, 400)
# Create an empty model for the list's data
model = QStandardItemModel(list)
# Add some textual items
foods = [
'pizza',
'burger',
'steak',
'chips',
'soda'
]
for food in foods:
# create an item with a caption
item = QStandardItem(food)
# add a checkbox to it
item.setCheckable(True)
item.setData({'one': 1, 'two': 2, 'three': 3})
item.setDragEnabled(True)
item.setDropEnabled(False)
# Add the item to the model
model.appendRow(item)
def on_item_changed(item):
# If the changed item is not checked, don't bother checking others
print 'DEBUG model rowcount'
print model.rowCount()
print 'itemChanged'
if not item.checkState():
return
# Loop through the items until you get None, which
# means you've passed the end of the list
i = 0
while model.item(i):
if not model.item(i).checkState():
return
i += 1
app.quit()
model.itemChanged.connect(on_item_changed)
# Apply the model to the list view
list.setModel(model)
w = QMainWindow()
w.setCentralWidget(list)
w.show()
sys.exit(app.exec_())
答案 0 :(得分:2)
更新,我发现了一个解决方法,它涉及使用单一计时器。删除列表中的项目后,计时器将触发插槽,并在新订单中找到5个项目而不是6个。
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
if __name__ == '__main__':
app = QApplication(sys.argv)
# Our main window will be a QListView
list = QListView()
list.setDragDropMode(QAbstractItemView.InternalMove)
list.setDefaultDropAction(Qt.MoveAction)
list.setDragDropOverwriteMode(False)
list.setAcceptDrops(True)
list.setDropIndicatorShown(True)
list.setDragEnabled(True)
list.setWindowTitle('Example List')
list.setMinimumSize(600, 400)
# Create an empty model for the list's data
model = QStandardItemModel(list)
# Add some textual items
foods = [
'pizza',
'burger',
'steak',
'chips',
'soda'
]
for food in foods:
# create an item with a caption
item = QStandardItem(food)
# add a checkbox to it
item.setCheckable(True)
item.setData({'one': 1, 'two': 2, 'three': 3})
item.setDragEnabled(True)
item.setDropEnabled(False)
# Add the item to the model
model.appendRow(item)
def showModelCount():
print 'DEBUG rowcount after singleshot'
print model.rowCount()
def on_item_changed(item):
# If the changed item is not checked, don't bother checking others
print 'DEBUG model rowcount'
print model.rowCount()
QTimer.singleShot(1, showModelCount)
print 'itemChanged'
if not item.checkState():
return
# Loop through the items until you get None, which
# means you've passed the end of the list
i = 0
while model.item(i):
if not model.item(i).checkState():
return
i += 1
app.quit()
model.itemChanged.connect(on_item_changed)
# Apply the model to the list view
list.setModel(model)
w = QMainWindow()
w.setCentralWidget(list)
w.show()
sys.exit(app.exec_())