我如何使用StandardItemModel()

时间:2019-10-09 09:34:31

标签: python pyqt pyqt5 qtreeview

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

在这里,我正在“ load_os_compartment_bucket()”内部使用strip()方法来扩展父级,并显示我正在使用update_model()方法的子级内容。我应该获取该父索引的实际选定值,以便可以在update_model()方法中使用它

我没有显示完整的代码,因为它庞大,因此只想根据我在qtreeview上所做的选择只专注于QTreeView和combobox,因此必须加载带有下拉列表的combobox。

identity.list_compartments(self.compt_name_id["PL_IN"]).data 
上面的示例“ PL_IN”代码中的

已进行了硬编码,但我想获取在该树视图中选择的实际值。

我尝试使用self.treeView.selectedIndexes(),但这似乎不起作用。

    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)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    timeout = QtCore.pyqtSignal(str)

    def __init__(self, *args, **kwargs):
        QtWidgets.QMainWindow.__init__(self, *args, **kwargs)

        self.setupUi(self)
        self.updateTime()
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.updateTime)
        self.timer.start(1000)

        # Object Storage related API Calls
        object_storage = oci.object_storage.ObjectStorageClient(self.config)
        namespace = object_storage.get_namespace().data
        self.MyTreeViewModel = StandardItemModel()
        self.treeView.setModel(self.MyTreeViewModel)
        self.most_used_cat_header = ['Compartment Name']
        self.MyTreeViewModel.setHorizontalHeaderLabels(self.most_used_cat_header)
        self.treeView.setSortingEnabled(True)
        self.treeView.expanded.connect(self.update_model)


class StandardItemModel(QtGui.QStandardItemModel):
    ExpandableRole = QtCore.Qt.UserRole + 500

    def hasChildren(self, index):
        if self.data(index, StandardItemModel.ExpandableRole):
            return True
        return super(StandardItemModel, self).hasChildren(index)

我的Ui代码在下面,具有树视图和组合框

# -*- 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_())

enter image description here

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

1 个答案:

答案 0 :(得分:0)

我找到了解决此问题的方法。附加我的代码。我也有问题再次追加相同的子数据,并再次使用rowCount()对其进行修复。不确定这是否是正确的做法,但目前对我有用。

img {
    min-width: 100%;
    height: auto;
}