![](/img/trans.png)
[英]PyQt QListView Issue with Drag and Drop for both importing and rearranging in one list
[英]PyQt: QListView drag and drop reordering signal issue
我在使用QListView和QStandardItemModel實現拖放重新排序時遇到了一些麻煩。 使用itemChanged信號來處理選中的項目很好,但是當使用拖放對項目進行重新排序時,似乎會在觸發信號時創建一個臨時項目。 列表中有5個項目,但是每次重新排序項目時,即使最后只有5個可見,模型行計數也變為6。 當我專門設置動作要移動時,為什么會暫時變為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_())
更新,我發現了一個解決方法,它涉及使用單發計時器。 將項目放到列表中后,計時器會觸發該廣告位,並以新的順序找到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_())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.