PySide2 QListView QTableView同步问题 [英] PySide2 QListView QTableView sync problem

查看:990
本文介绍了PySide2 QListView QTableView同步问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是与python/PySide2接口相关的问题,正如我尝试过的,我无法使其同步(QListView和QTableView).

It is python/PySide2 interface related problem, as much as I tried, I couldn't make it sync (QListView and QTableView).

我将尝试简化它,而不是解释整个复杂的事情,因为QT中充满了表格内容,所以事情变得更大了.

I will try to simplify it, instead of explaining the whole complicated thing which is much bigger with full of forms stuff in QT...

试想像这样具有一个数据结构(我想是模型):

Just imagine to have a data structure (I guess, model) like the following:

dict_of_dicts={
'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
'dict2':{'k4':'v4'},
'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
}

我想要一个包含两个部分的表单(或对话框):

I would like to have a form (or dialog), with 2 parts:

1)在表单的左侧,具有一个QListView来可视化以下内容:

1) In the left of the form, to have a QListView to visualize the following:

*dict1
------
dict2
-----
dict3

注意:

  • dict1中的星号表示已被选中.

  • The asterisk in the dict1 means that is selected.

连字符只是在行之间分隔.

The hyphens are just to separate between rows.

2)在表单的右侧,使QTableView显示以下内容:

2) In the right of the form, to have a QTableView showing the following:

k1 | v1
-------
k2 | v2
-------
k3 | v3

注意:

  • 连字符只是在行之间分隔.

  • The hyphens are just to separate between rows.

管道只是用来表示列的分隔.

The pipes, are just to represent the columns separation.

每次在QListView中选择另一个元素时,QTableView必须更改为原始数据结构指示的元素.

Everytime you select the another element int the QListView, the QTableView must change to the one indicated by the original data structure.

我相信对于大多数人来说这确实很容易,但是我只是从UI内容和MVC开始.

I am sure it could be really easy for most of you guys, but I am just starting with UI stuff and MVC.

推荐答案

您必须使用树结构创建一个模型,在该模型中可以看到相关性,在QListView的情况下,它将显示根项,而在QListView的情况下,将显示根项. QTableView的叶子将显示叶子,并且将QListView的所选QModelIndex作为rootIndex.出于教育目的,我将在QTreeView中显示树的模型.

You have to create a model with the tree structure where the dependency is seen, and in the case of the QListView it will show the root items and in the case of the QTableView it will show the leaves and it will have as rootIndex the selected QModelIndex of the QListView. For educational purposes I will show the model of the tree in a QTreeView.

from PySide2 import QtCore, QtGui, QtWidgets

dict_of_dicts={
    'dict1':{'k1':'v1', 'k2':'v2', 'k3':'v3'},
    'dict2':{'k4':'v4'},
    'dict3':{'k5':'v5', 'k6':'v6', 'k7':'v7'},
}

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():
            it.appendRow([QtGui.QStandardItem(k_), QtGui.QStandardItem(v_)])
    return model

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent)
        model = create_model_from_dict(dict_of_dicts, self)

        self.tableview = QtWidgets.QTableView()
        self.tableview.setModel(model)

        self.listview = QtWidgets.QListView()
        self.listview.setModel(model)
        self.listview.selectionModel().selectionChanged.connect(self.handleSelectionChanged)
        self.listview.selectionModel().select(model.index(0, 0), QtCore.QItemSelectionModel.Select)

        self.treeview = QtWidgets.QTreeView()
        self.treeview.setModel(model)
        self.treeview.expandAll()

        hlay = QtWidgets.QHBoxLayout(self)
        hlay.addWidget(self.listview)
        hlay.addWidget(self.tableview)
        hlay.addWidget(self.treeview)

    @QtCore.Slot(QtCore.QItemSelection)
    def handleSelectionChanged(self, item):
        ixs = item.indexes()
        if ixs:
            self.tableview.setRootIndex(ixs[0])

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

这篇关于PySide2 QListView QTableView同步问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆