我正在尝试将QlistWidgetItem设置为将对象存储在中。我正在使用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()
答案 0 :(得分:1)
我在您的代码中看到的是,您将QListWidgetItem
文本从一个QListWidget复制到另一个,而您没有移动QListWidgetItem
。 takeItem()
方法返回没有父级的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)