Python 如何在QTreeView中获取当前选择的值我使用的是StandardItemModel()

Python 如何在QTreeView中获取当前选择的值我使用的是StandardItemModel(),python,pyqt,pyqt5,qtreeview,Python,Pyqt,Pyqt5,Qtreeview,我正在使用QTreeView显示具有父子关系的数据。子树视图也可以有另一组子树 在这里,我使用“load_os_隔间_bucket()”中的strip()方法来展开父对象并显示其子内容,我使用的是update_model()方法。我应该获取父索引的实际选定值,以便在update_model()方法中使用该值 我没有显示完整的代码,因为它太大了,所以我只想关注QTreeView和combobox,基于我在QTreeView上所做的选择,我必须用下拉列表加载combobox identity.lis

我正在使用QTreeView显示具有父子关系的数据。子树视图也可以有另一组子树

在这里,我使用“load_os_隔间_bucket()”中的strip()方法来展开父对象并显示其子内容,我使用的是update_model()方法。我应该获取父索引的实际选定值,以便在update_model()方法中使用该值

我没有显示完整的代码,因为它太大了,所以我只想关注QTreeView和combobox,基于我在QTreeView上所做的选择,我必须用下拉列表加载combobox

identity.list_compartments(self.compt_name_id["PL_IN"]).data 
在上面的示例代码中,“PL_in”是硬编码的,但我想得到在树视图中选择的实际值

我试着使用self.treeView.selectedIndex(),但似乎不起作用

    def load_os_compartment_bucket(self):
        identity = oci.identity.IdentityClient(self.config)
        compt_id = identity.list_compartments(self.config["tenancy"]).data
        object_storage = oci.object_storage.ObjectStorageClient(self.config)
        self.namespace = object_storage.get_namespace().data
        # self.MyTreeViewModel.clear()
        for compartments in compt_id:
            if "." not in compartments.name:
                self.compt_name_id[compartments.name] = compartments.id
                parent_item = QtGui.QStandardItem(compartments.name.strip())
                parent_item.setData(True, StandardItemModel.ExpandableRole)
                self.MyTreeViewModel.appendRow(parent_item)
        print(self.compt_name_id)

    def update_model(self, index):
        parent_node = QtGui.QStandardItem(self.MyTreeViewModel.itemFromIndex(index))
        parent_item_index = index.row()
        print(parent_node.data())
        print(parent_item_index)
        parent = self.MyTreeViewModel.itemFromIndex(index)
        newmodel = self.MyTreeViewModel.data(index, QtCore.Qt.UserRole+1)
        print(self.treeView.selectedIndexes())
        print(newmodel)
        print(parent.rowCount())
        identity = oci.identity.IdentityClient(self.config)
        child_compt_id = identity.list_compartments(self.compt_name_id["PL_IN"]).data 
        for child_comp in child_compt_id:
            if "." not in child_comp.name:
                children = QtGui.QStandardItem("{}".format(child_comp.name))
                parent.appendRow(children)
下面是我的Ui代码,其中包含treeview和combobox

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui_testmain.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.treeView = QtWidgets.QTreeView(self.centralwidget)
        self.treeView.setGeometry(QtCore.QRect(220, 40, 291, 151))
        self.treeView.setObjectName("treeView")
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(270, 230, 191, 41))
        self.comboBox.setObjectName("comboBox")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())


我想获得正确的父数据,以便使用它并获取dict self.compt_name_id[]的子详细信息。

我找到了解决我在这里发布的问题的方法。附加我的代码。此外,我还遇到了一个问题,就是一次又一次地追加相同的子数据,并使用rowCount()来修复它。不确定他们这样做是否正确,但现在这对我来说很有效

    def update_model(self, index):
        parent_node = QtGui.QStandardItem(self.MyTreeViewModel.itemFromIndex(index))
        parent_item_index = index.row()
        parent = self.MyTreeViewModel.itemFromIndex(index)
        if parent.rowCount() > 0:
            return
        current_idx_data = ""
        for i in self.treeView.selectedIndexes():
            current_idx_data = i.data()
        identity = oci.identity.IdentityClient(self.config)
        identity.list_compartments(self.compt_name_id[str(current_idx_data)]).data

        for child_comp in child_compt_id:
            if "." not in child_comp.name:
                children = QtGui.QStandardItem("{}".format(child_comp.name))
                parent.appendRow(children)

我有一个关于QTreeView的查询,每当我选择并展开父项时,我将更新子项,并说如果我选择任何一个子项,我想更新组合框,您知道如何实现这一点。我还分享了一张图片,以便更清楚地说明。是的,我有一个QTreeView,其中包含类别和类别中的组,如果您选择了一个类别或其子组,QTableView会根据您选择的内容显示项目的缩短列表。这听起来与您尝试做的基本相同
    def update_model(self, index):
        parent_node = QtGui.QStandardItem(self.MyTreeViewModel.itemFromIndex(index))
        parent_item_index = index.row()
        parent = self.MyTreeViewModel.itemFromIndex(index)
        if parent.rowCount() > 0:
            return
        current_idx_data = ""
        for i in self.treeView.selectedIndexes():
            current_idx_data = i.data()
        identity = oci.identity.IdentityClient(self.config)
        identity.list_compartments(self.compt_name_id[str(current_idx_data)]).data

        for child_comp in child_compt_id:
            if "." not in child_comp.name:
                children = QtGui.QStandardItem("{}".format(child_comp.name))
                parent.appendRow(children)