gpt4 book ai didi

python - QListWidgetItem 保存列表之间的数据

转载 作者:太空宇宙 更新时间:2023-11-04 02:35:46 25 4
gpt4 key购买 nike

我正在尝试将 QlistWidgetItem 设置为使用 in 存储对象。我正在使用 setData 函数来实现此目的。我可以将数据存储在列表 A 中的项目中。但是我试图将 QlistWidgetItem 移动到列表 B,这样做时它会丢失列表 A 中的数据。我试图阅读listA.item 中的数据然后在创建 listB.item 时使用相同的方法将其存储在那里但没有成功。

http://doc.qt.io/archives/qt-4.8/qlistwidgetitem.html#setData

请查看下面的代码,因为它是我正在尝试执行的操作的示例。如果您运行下面的程序,您将看到它将一个示例项目加载到底部列表中。然后我尝试将列表移动到顶部并返回底部,同时将数据元素保留在内部,但它在这个过程中丢失了。任何帮助都会很棒!

import sys
from PyQt4 import QtGui, QtCore

class Struct():
def __init__(self,row):
self.hotkey=row['hotkey']
self.command=row["COMMAND"]

class Example(QtGui.QWidget):

def __init__(self):
super(Example, self).__init__()
self.setMinimumSize(600, 400)
self.initUI()

def initUI(self):

self.pushButton = QtGui.QPushButton(self)
self.pushButton.setGeometry(QtCore.QRect(90, 630, 100, 41))
self.pushButton.setObjectName(("pushButton"))
self.listWidget = QtGui.QListWidget(self)
self.listWidget.setGeometry(QtCore.QRect(20, 90, 261, 220))
self.listWidget.setObjectName(("listWidget"))
self.listWidget_2 = QtGui.QListWidget(self)
self.listWidget_2.setGeometry(QtCore.QRect(20, 379, 261, 211))
self.listWidget_2.setObjectName(("listWidget_2"))
self.pushButton_2 = QtGui.QPushButton(self)
self.pushButton_2.setGeometry(QtCore.QRect(160, 320, 31, 31))
self.pushButton_2.setObjectName(("pushButton_2"))
self.pushButton_3 = QtGui.QPushButton(self)
self.pushButton_3.setGeometry(QtCore.QRect(100, 320, 31, 31))
self.pushButton_3.setObjectName(("pushButton_3"))
self.pushButton.clicked.connect(self.testData)
self.pushButton_2.clicked.connect(self.moveItemsTopToBottom)
self.pushButton_3.clicked.connect(self.moveItemsBottomToTop)
self.loadRightList()
self.loadLeftList()
self.listWidget_2.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.listWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.show()


def testData(self, state):
print self.listWidget_2.item(0).text()
print self.listWidget_2.item(0).data(32).toPyObject().hotkey
def moveItemsTopToBottom(self,state):
for i in self.listWidget.selectedItems():
x= i.data(32).toPyObject()
self.listWidget_2.addItem(i.text())
self.listWidget.takeItem(self.listWidget.row(i))
item_num=self.listWidget.count()-1
return
def moveItemsBottomToTop(self,state):
for i in self.listWidget_2.selectedItems():
self.listWidget.addItem(i.text())
self.listWidget_2.takeItem(self.listWidget_2.row(i))
item_num=self.listWidget_2.count()-1
self.listWidget_2.item(item_num)
return
def loadLeftList(self):

results = []
if (results!=[]):
for i in results:
self.listWidget.addItem(i['NAME'])
item_num=self.listWidget.count()-1
self.listWidget.item(item_num).setToolTip(i['COMMAND'])
x=Struct(i)
self.listWidget.item(item_num).setData(32,x)
return
def loadRightList(self):

results= [{'NAME': 'Example Query 1', 'TEAM_CD': 1, 'TYPE_CD': 0, 'COMMAND': 'SELECT * FROM CHARGE C WHERE C.ACTIVE_IND = 1', 'hotkey': 'cinfo', 'COMMAND_ID': 2}]
if (results!=None):
for i in results:
self.listWidget_2.addItem(i['NAME'])
item_num=self.listWidget_2.count()-1
self.listWidget_2.item(item_num).setToolTip(i['COMMAND'])
x=Struct(i)
self.listWidget_2.item(item_num).setData(32,x)
print self.listWidget_2.item(item_num).data(32).toPyObject().hotkey
else:
return None


def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())


if __name__ == '__main__':
main()

最佳答案

我在您的代码中看到的是,您将 QListWidgetItem 文本从一个 QListWidget 复制到另一个,您没有移动 QListWidgetItemtakeItem() 方法返回没有父级的 QListWidgetItem,因此您只需将它添加到另一个。

class Example(QtGui.QWidget):

def __init__(self):
super(Example, self).__init__()
self.setMinimumSize(600, 400)
self.initUI()

def initUI(self):
lay = QtGui.QVBoxLayout(self)
self.topListWidget = QtGui.QListWidget(self)
self.bottomListWidget = QtGui.QListWidget(self)
toTopButton = QtGui.QPushButton("to top", self)
toBottomButton = QtGui.QPushButton("to bottom", self)
printButton = QtGui.QPushButton("print", self)
hlay = QtGui.QHBoxLayout()
hlay.addWidget(toBottomButton)
hlay.addWidget(toTopButton)
lay.addWidget(self.topListWidget)
lay.addLayout(hlay)
lay.addWidget(self.bottomListWidget)
lay.addWidget(printButton)

self.topListWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.bottomListWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)

self.show()
printButton.clicked.connect(self.testData)
self.loadRightList()
toBottomButton.clicked.connect(self.moveItemsTopToBottom)
toTopButton.clicked.connect(self.moveItemsBottomToTop)


def testData(self, state):
for i in range(self.topListWidget.count()):
it = self.topListWidget.item(i)
print("top: ", it.data(QtCore.Qt.UserRole).toPyObject().hotkey)

for i in range(self.bottomListWidget.count()):
it = self.bottomListWidget.item(i)
print("bottom: ", it.data(QtCore.Qt.UserRole).toPyObject().hotkey)


def moveItemsTopToBottom(self,state):
for item in self.topListWidget.selectedItems():
it = self.topListWidget.takeItem(self.topListWidget.row(item))
self.bottomListWidget.addItem(it)

def moveItemsBottomToTop(self,state):
for item in self.bottomListWidget.selectedItems():
it = self.bottomListWidget.takeItem(self.bottomListWidget.row(item))
self.topListWidget.addItem(it)

def loadRightList(self):
results= [{'NAME': 'Example Query 1', 'TEAM_CD': 1, 'TYPE_CD': 0, 'COMMAND': 'SELECT * FROM CHARGE C WHERE C.ACTIVE_IND = 1', 'hotkey': 'cinfo', 'COMMAND_ID': 2}]
for result in results:
item = QtGui.QListWidgetItem(result["NAME"])
item.setToolTip(result["COMMAND"])
item.setData(QtCore.Qt.UserRole, Struct(result))
self.topListWidget.addItem(item)

关于python - QListWidgetItem 保存列表之间的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47944013/

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