如何使用currentIndex()获取itemFromIndex中项目的另一个值?

时间:2019-11-13 12:09:44

标签: python pyqt pyqt5

我使用itemFromIndex来获取项目,但是即使我想获取该项目的另一个值,它也仅返回项目的选定名称。

这是物品

name

当我单击 AAA 时,我希望获得 123 321 ,但只有我获得 AAA 。我知道这是因为currentIndex()的列值已经通过单击一项来确定。我认为currentIndex()具有列值,但是我不知道如何获取其列值来获取另一项。

from collections import deque
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1050, 750)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(800, 600))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setEnabled(True)
        self.centralwidget.setObjectName("centralwidget")
        self.page = QtWidgets.QWidget()
        self.page.setObjectName("page")
        self.listView_2 = QtWidgets.QListView(self.page)
        self.listView_2.setGeometry(QtCore.QRect(0, 0, 731, 641))
        self.listView_2.setObjectName("listView_2")
        self.page_2 = QtWidgets.QWidget()
        self.page_2.setObjectName("page_2")
        self.leftView = QtWidgets.QTreeView(self.centralwidget)
        self.leftView.setGeometry(QtCore.QRect(20, 50, 671, 641))
        self.leftView.setObjectName("leftView")
        self.leftView.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.leftView.setHorizontalScrollBarPolicy(2)
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

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

    def fun_setup(self):
        print("data taken")
        data = [
            {'level': 0, 'dbID': 'f1', 'parent_ID': 0, 'name': 'root'} ,
            {'level': 1, 'dbID': 1, 'parent_ID': 'f1', 'name': 'aaa'} ,
            {'level': 2, 'dbID': 2, 'parent_ID': 1, 'name': 'bbb'} ,
            {'level': 1, 'dbID': 3, 'parent_ID': 'f1', 'name': 'c c'}
            ]
        print("data taken")
        self.model = QtGui.QStandardItemModel()
        self.model.setHorizontalHeaderLabels(['Name'])
        self.leftView.setModel(self.model)
        self.importData(data)
        self.leftView.expandAll()
        self.leftView.clicked.connect(self.func_lvSelected)

    def func_lvSelected(self):
        print(self.model.itemFromIndex(self.leftView.currentIndex()).text()) #<-This one

    def importData(self, data, root=None):
        self.model.setRowCount(0)
        if root is None:
            root = self.model.invisibleRootItem()
        seen = {}
        values = deque(data) 
        while values:
            value = values.popleft()
            #print(value)
            #print('val icon: '+value)
            if value['level'] == 0:
                rootname = value['dbID']
                parent = root 
            else:
                pid = value['parent_ID']
                if pid not in seen:
                    values.append(value)
                    continue
                parent = seen[pid]
            dbid = value['dbID']
            parent.appendRow([
                QtGui.QStandardItem(value['name']),
                QtGui.QStandardItem(str(dbid)), #showing dbID
                ])
            seen[dbid] = parent.child(parent.rowCount() - 1)

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

1 个答案:

答案 0 :(得分:1)

您必须使用QModelIndex的sibling方法:

def func_lvSelected(self):
    ix = self.leftView.currentIndex()
    s_ix = ix.sibling(ix.row(), 1)
    it = self.model.itemFromIndex(s_ix)
    if it is not None:
        print(it.text())