-1

I am using QTreeView to display data with parent child relationship. child tree-view can in turn have another set of child as well.

Here i am using strip() method inside "load_os_compartment_bucket()" to expand parent and show its child contents for which i am using update_model() method. I should fetch actual selected value of the of that parent index so that i can use that in update_model() method

I haven't shown the complete code because its huge so wanted to focus only on QTreeView and combobox based on the selection i make on qtreeview i have to load combobox with list of drop downs.

identity.list_compartments(self.compt_name_id["PL_IN"]).data 

in the above code for sample "PL_IN" is have hardcoded but i want to get the actual value which gets selected in that treeview.

I tried using self.treeView.selectedIndexes() but that doesn't seem to work.

    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)

My Ui code is below which has treeview and 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_())

enter image description here

i want to get the correct parent data so that i can use that and fetch child details for the dict self.compt_name_id[].

vinodh
  • 167
  • 2
  • 8

1 Answers1

-1

I found a way around for the question i posted here. Attaching my code. Also i had problem of appending same child data again and again used rowCount() to fix it. Not sure if thats they right way to do but for now this works for me.

    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)
vinodh
  • 167
  • 2
  • 8
  • i have one query with regards to QTreeView whenever i select and expand a parent item i will update child items and say if i select any one of the child item i want to update combo box you have idea how to achieve this. i have shared an image as well to give more clarity – vinodh Oct 10 '19 at 10:14
  • Yes I have a QTreeView containing Categories and Groups within Categories and if you select a Category or one of its sub-Groups the QTableView shows shorten list of items based on what you have selected -- this sounds like it is basically the same thing you are trying to do – Dennis Jensen Oct 10 '19 at 13:55