我有一个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_())
答案 0 :(得分:1)
当某项从位置i
移到位置j
时,要做的是:
j
itemChanged
信号,因此您会看到更多元素。i
中的项目。这就是该行为的原因。但是我不知道需要更新该列表。通常的事情是获取需求中的已检查项目,即具有一个被调用的函数并在必要时进行计算。
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_())