我有一个 PyQt5 QListview,其中包含可以通过拖放和选中的复选框重新排列的项目列表。问题是当我重新排列列表中的项目时,项目在 GUI 中正确地重新排列,但是当我尝试保存输出时,在其原始位置和新位置都有移动项目的副本。
创建列表的函数
def create_methods_list(self):
self.methods_list = QListView()
self.methods_list.setDragDropMode(QListView.InternalMove)
self.methods_list.setDefaultDropAction(Qt.MoveAction)
self.methods_list.setDragDropMode(False)
self.methods_list.setAcceptDrops(True)
self.methods_list.setDropIndicatorShown(True)
self.methods_list.setDragEnabled(True)
self.methods_list.setWindowTitle('Method Order')
self.methods_model = QtGui.QStandardItemModel(self.methods_list)
# self.methods is a list of strings
for method in self.methods:
item = QtGui.QStandardItem(method)
item.setData(method)
item.setCheckable(True)
item.setDragEnabled(True)
item.setDropEnabled(False)
item.setCheckState(True)
self.methods_model.appendRow(item)
self.methods_model.itemChanged.connect(self.method_item_changed)
self.methods_list.setModel(self.methods_model)
重新排列列表时调用的函数:
def method_item_changed(self):
print(self.methods_model.rowCount())
i = 0
new_methods = []
while self.methods_model.item(i):
if self.methods_model.item(i).checkState():
new_methods.append(self.methods_model.item(i).data())
i += 1
print(new_methods)
self.methods = new_methods
第一个 print 语句返回比原始列表中的项目数多的一个。第二个打印语句返回列表中的项目,但移动的项目同时位于其原始位置和新位置。
我尝试了一种解决方案,其中包括添加一个具有较短间隔的 QTimer,但没有奏效。
如有任何帮助,我们将不胜感激。
完整版:
from PyQt5.QtWidgets import (
QAction, QWidget, QLabel, QDesktopWidget,
QApplication, QComboBox, QPushButton, QGridLayout,
QMainWindow, qApp, QVBoxLayout, QSlider,
QHBoxLayout, QLineEdit, QListView, QAbstractItemView
)
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.methods = ['option 1', 'option 2', 'option 3']
self.central_widget = QWidget(self)
self.layout = QVBoxLayout(self.central_widget)
self.layout.addStretch()
self.create_methods_list()
self.layout.addWidget(self.methods_list)
self.layout.addStretch()
self.setCentralWidget(self.central_widget)
self.show()
def create_methods_list(self):
self.methods_list = QListView()
self.methods_list.setDragDropMode(QListView.InternalMove)
self.methods_list.setDefaultDropAction(Qt.MoveAction)
self.methods_list.setDragDropMode(False)
self.methods_list.setAcceptDrops(True)
self.methods_list.setDropIndicatorShown(True)
self.methods_list.setDragEnabled(True)
self.methods_list.setWindowTitle('Method Order')
self.methods_model = QtGui.QStandardItemModel(self.methods_list)
for method in self.methods:
item = QtGui.QStandardItem(method)
item.setData(method)
item.setCheckable(True)
item.setDragEnabled(True)
item.setDropEnabled(False)
item.setCheckState(True)
self.methods_model.appendRow(item)
self.methods_model.itemChanged.connect(self.method_item_changed)
self.methods_list.setModel(self.methods_model)
self.methods_list.setMinimumHeight(
self.methods_list.sizeHintForRow(0)
* (self.methods_model.rowCount() + 2))
def method_item_changed(self):
print(self.methods_model.rowCount())
i = 0
new_methods = []
while self.methods_model.item(i):
if self.methods_model.item(i).checkState():
new_methods.append(self.methods_model.item(i).data())
i += 1
self.methods = new_methods
print(self.methods)
if __name__=='__main__':
app = QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
请您参考如下方法:
当一个项目从位置i移动到位置j时,完成的是:
- 在位置
j插入一个项目 - 将数据复制到新item中,此时发出
itemChanged信号,因此可以看到有more的元素 - 删除位置
i的项目。
这就是该行为的原因。但我不明白更新该列表的必要性。正常的是获取需求的checked items,也就是说有一个函数,需要的时候调用并计算。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.methods = ['option 1', 'option 2', 'option 3']
central_widget = QtWidgets.QWidget()
layout = QtWidgets.QVBoxLayout(central_widget)
self.create_methods_list()
layout.addWidget(self.methods_list)
layout.addStretch()
self.setCentralWidget(central_widget)
def create_methods_list(self):
self.methods_list = QtWidgets.QListView()
self.methods_list.setDragDropMode(QtWidgets.QListView.InternalMove)
self.methods_list.setDefaultDropAction(QtCore.Qt.MoveAction)
self.methods_list.setAcceptDrops(True)
self.methods_list.setDropIndicatorShown(True)
self.methods_list.setDragEnabled(True)
self.methods_model = QtGui.QStandardItemModel(self.methods_list)
for method in self.methods:
item = QtGui.QStandardItem(method)
item.setData(method)
item.setCheckable(True)
item.setDragEnabled(True)
item.setDropEnabled(False)
item.setCheckState(QtCore.Qt.Checked)
self.methods_model.appendRow(item)
self.methods_list.setModel(self.methods_model)
self.methods_list.setMinimumHeight(
self.methods_list.sizeHintForRow(0)
* (self.methods_model.rowCount() + 2))
def get_items_Checked(self):
checkeds = []
for i in range(self.methods_model.rowCount()):
it = self.methods_model.item(i)
if it is not None and it.checkState() == QtCore.Qt.Checked:
checkeds.append(it.text())
return checkeds
if __name__=='__main__':
app = QtWidgets.QApplication(sys.argv)
ex = MainWindow()
ex.show()
sys.exit(app.exec_())
