有一个Qtreeview填充了字典中的数据,当在此树视图上更改选择时,QLabel上会显示一些数据。
当字典更新时(通过双击另一个Qtreeview),我调用最初将数据加载到Qtreeview的函数。显示了新数据,但selectionChanged不再起作用。
我做错了什么?
#!/usr/bin/env python -tt
# -*- coding: utf-8 -*-
#from PySide.QtGui import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
reload(sys)
sys.setdefaultencoding('utf8')
data_for_tree = {"tomato":{"color":"red","ammount":"10", "note":"a note for tomato","price":"0.8"},"banana":{"color":"yellow","ammount":"1", "note":"b note for banana", "price":".6"}, "some fruit":{"color":"unknown","ammount":"100", "note":"some text","price":"2.1"}}
data_for_receiver = {"1":{"name":"milk","price":"3.2","note":"I love milk"}, "2":{"name":"coca-cola","price":".8","note":"coke forever"}}
class ProxyModel(QSortFilterProxyModel):
def __init__(self, parent=None):
super(ProxyModel, self).__init__(parent)
def lessThan(self, left, right):
leftData = self.sourceModel().data(left)
rightData = self.sourceModel().data(right)
try:
return float(leftData) < float(rightData)
except ValueError:
return leftData < rightData
class MainFrame(QWidget):
def __init__(self):
QWidget.__init__(self)
self.infoLable = QLabel()
self.MyTreeView = QTreeView()
self.MyTreeViewModel = QStandardItemModel()
self.MyTreeView.setModel(self.MyTreeViewModel)
self.most_used_cat_header = ['Name', "ammount", "color"]
self.MyTreeViewModel.setHorizontalHeaderLabels(self.most_used_cat_header)
self.MyTreeView.setSortingEnabled(True)
self.MyTreeView_Fill()
self.receiver_tree = QTreeView()
self.receiver_model = QStandardItemModel()
self.receiver_tree.setModel(self.receiver_model)
self.receiver_tree_header = ['#','Name', "price"]
self.receiver_model.setHorizontalHeaderLabels(self.receiver_tree_header)
self.MyTreeView.doubleClicked.connect(self.addToReceiver)
self.receiver_tree.selectionModel().selectionChanged.connect(self.showInfo)
self.receiver_fill()
MainWindow = QVBoxLayout(self)
MainWindow.addWidget(self.infoLable)
MainWindow.addWidget(self.MyTreeView)
MainWindow.addWidget(self.receiver_tree)
self.setLayout(MainWindow)
def showInfo(self):
indexes = self.receiver_tree.selectedIndexes()
index_list =[i.data() for i in self.receiver_tree.selectedIndexes()]
name = index_list[1]
price = index_list[2]
txt = str(name) + " " + str(price)
self.infoLable.setText(txt)
def addToReceiver(self):
indexes = self.MyTreeView.selectedIndexes()
index_list =[i.data() for i in self.MyTreeView.selectedIndexes()]
last_id = max(int(i) for i in data_for_receiver)
for k in data_for_tree:
v = data_for_tree[k]
if [k,v["ammount"],v["color"]] == index_list:
i =QStandardItem(str(last_id+1))
name = QStandardItem(k)
price = QStandardItem(format(float(v["price"]), ".2f"))
tooltip = v["note"]
name.setToolTip(tooltip)
item = ( i, name, price)
#self.receiver_model.appendRow(item)
upd = {"name":k,"price":v["price"],"note":v["note"]}
data_for_receiver[str(last_id+1)] = upd
self.receiver_model = QStandardItemModel()
self.receiver_tree.setModel(self.receiver_model)
self.receiver_fill()
def MyTreeView_Fill(self):
for k in data_for_tree:
name = QStandardItem(k)
ammount = QStandardItem(data_for_tree[k]["ammount"])
note = QStandardItem(data_for_tree[k]["color"])
name.setEditable(False)
tooltip = "price "+format(float(data_for_tree[k]["price"]), ".2f")+"<br>"
tooltip += data_for_tree[k]["note"]
item = (name, ammount, note)
name.setToolTip(tooltip)
self.MyTreeViewModel.appendRow(item)
self.MyTreeView.sortByColumn(1, Qt.DescendingOrder)
proxyModel = ProxyModel(self)
proxyModel.setSourceModel(self.MyTreeViewModel)
self.MyTreeView.setModel(proxyModel)
c = 0
while c < len(self.most_used_cat_header):
self.MyTreeView.resizeColumnToContents(c)
c=c+1
def receiver_fill(self):
for k in data_for_receiver:
v = data_for_receiver[k]
i = QStandardItem(k)
name = QStandardItem(v["name"])
price = QStandardItem(format(float(v["price"]), ".2f"))
tooltip = v["note"]
name.setToolTip(tooltip)
item = (i,name, price)
self.receiver_model.appendRow(item)
c = 0
while c < len(self.receiver_tree_header):
self.receiver_tree.resizeColumnToContents(c)
c=c+1
if __name__ == "__main__":
app = QApplication(sys.argv)
main = MainFrame()
main.show()
main.move(app.desktop().screen().rect().center() - main.rect().center())
sys.exit(app.exec_())
答案 0 :(得分:0)
原因是您每次都在QStandardItemModel
创建并设置新的addToReceiver
。 QAbstractItemView::setModel
方法文档说它会导致视图重新创建选择模型,并且信号连接会丢失。您应该使用self.receiver_model.clear()
代替并删除setModel
来电。