python tableview添加内容_为上一个问题PySide2 QListView和QTableView添加功能

该博客介绍了如何在PySide2中处理包含引用其他字典的字典数据结构,并展示了如何在QTableView和QListView之间进行交互。通过设置自定义角色和使用setSpan方法,使得当在QTableView中双击指向另一个字典的条目时,能在QListView中选中对应的字典并在QTableView中显示其内容。
摘要由CSDN通过智能技术生成

Imagine to have another dict4 in the data structure:

'dict4':{'k8':'v8', 'EXISTING_DICT':'dict2'},

Meaning, that a dictionary could include another existing dictionary.

So the representation in the QTableView shouldn't be directly showed, but:

1) Show just the name of it in the QTableView:

k1 | v1

-------

k2 | v2

-------

k3 | v3

-------

dict2

2) If double click it on it: select the existing dictionary in the QListView, which will trigger to show its contents in the QTableView, which in this case:

k4 | v4

解决方案

In this case the idea is to use a role to indicate that the field points to another item, then use setSpan to join items and finally check if the item selected in the QTableView points to another element by selecting if it is so

from PySide2 import QtCore, QtGui, QtWidgets

dict_of_dicts={

'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3', 'EXISTING_DICT': 'dict3'},

'dict2':{'k4':'v4', 'EXISTING_DICT': 'dict1'},

'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7', 'EXISTING_DICT': 'dict4'},

'dict4':{'k8':'v8', 'EXISTING_DICT':'dict2'},

}

def create_model_from_dict(d, parent=None):

model = QtGui.QStandardItemModel(0, 2, parent)

for k, v in dict_of_dicts.items():

it = QtGui.QStandardItem(k)

model.appendRow(it)

for k_, v_ in v.items():

if k_ != "EXISTING_DICT":

it.appendRow([QtGui.QStandardItem(k_), QtGui.QStandardItem(v_)])

else:

child_it = QtGui.QStandardItem(v_)

child_it.setTextAlignment(QtCore.Qt.AlignCenter)

child_it.setData(True, QtCore.Qt.UserRole + 1000)

it.appendRow(child_it)

re

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值