在DoubleClick事件中从QTreeWidget获取ItemWidget

时间:2015-11-27 10:37:28

标签: python pyqt5 qtreewidget

我现在创建了一个QTreeWidget,当我点击一个项目时,我想获取小部件数据。 我这样填写我的QTreeWidget:

    def addNodeToTreeview(self, data):
    self.panelInfoPatientUi.treeWidgetDevices.clear()
    for item in data: 
        mainWidgetItem = QtGui.QTreeWidgetItem(self.panelInfoPatientUi.treeWidgetDevices)
        widgetContainer = QtWidgets.QWidget()
        widgetContainer.setObjectName("widgetContainer") 
        deviceWidget = Ui_DeviceListviewWidget()
        deviceWidget.setupUi(widgetContainer)

        deviceWidget.labelSerialNumber.setText(item.serialNumber)
        deviceWidget.labelModel.setText(item.model)
        deviceWidget.labelInstallationDate.setText(item.installDate)

        mainWidgetItem.setSizeHint(0, widgetContainer.sizeHint())   

        self.panelInfoPatientUi.treeWidgetDevices.addTopLevelItem(mainWidgetItem)
        self.panelInfoPatientUi.treeWidgetDevices.setItemWidget(mainWidgetItem, 0, widgetContainer)

        for files in item.listFile:
            #Files
            fileWidgetItem = QtGui.QTreeWidgetItem(mainWidgetItem)
            widgetContainerFiles = QtWidgets.QWidget()
            widgetContainerFiles.setObjectName("widgetContainerFiles")
            fileWidget = Ui_FileListWidgetItem()    
            fileWidget.setupUi(widgetContainerFiles)

            fileWidgetItem.setText(0, "BLABLBALA")

            fileWidget.labelFileName.setText(files.fileName)
            fileWidget.labelDateFile.setText(files.dateFile)

            fileWidgetItem.setSizeHint(0, widgetContainerFiles.sizeHint())
            mainWidgetItem.addChild(fileWidgetItem)
            self.panelInfoPatientUi.treeWidgetDevices.setItemWidget(fileWidgetItem, 0, widgetContainerFiles)

我以这种方式连接小部件:

    def connectSignalTreeWidget(self):
        self.view.panelInfoPatientUi.treeWidgetDevices.itemDoubleClicked.connect(self.testest)

当我收到Click事件时,我无法访问我的小部件,我尝试了几种方式:

    def testest(self, item, col):

    print(self.view.panelInfoPatientUi.treeWidgetDevices.itemWidget(item, 0))

    #print([method for method in dir(item) if callable(getattr(item, method))])
    #print(str(item.ItemType()))
    #print(str(item.text(col)))
    #print(str(item.child(0)))
    #print(str(item.childCount()))
    #print(str(item.child(1).text(0)))

    #print(str(self.view.panelInfoPatientUi.treeWidgetDevices.currentItem()))
#   titi = .itemWidget(item, columnIndex)
#   print(str(titi))
#   titi.text(0)
#   titi.data()
    #print(str(titi.labelFileName.text()))

    '''selectedItems = self.view.panelInfoPatientUi.treeWidgetDevices.selectedItems()
    for selectedItem in selectedItems:
        print(str(selectedItem.text(0)))
        print(str(selectedItem.text(1)))
        print(str(selectedItem.text(2)))    ''' 

    '''
    print(item.data(1, 0))
    print("column count " + str(data.columnCount()))
    print("AHYAAAAAAAAAA")'''

当我使用“setItemWidget”方法时,我希望得到一个getItemWidget方法并从中检索数据但是没有。我如何访问fileWidget.labelFileName?

由于

1 个答案:

答案 0 :(得分:0)

我找到了解决方案: 我改变了我的创作方法:

    def addNodeToTreeview(self, data):
    self.panelInfoPatientUi.treeWidgetDevices.clear()
    for item in data: 
        mainWidgetItem = QtGui.QTreeWidgetItem(self.panelInfoPatientUi.treeWidgetDevices)
        widgetContainer = QtWidgets.QWidget()
        widgetContainer.setObjectName("widgetContainer") 
        deviceWidget = Ui_DeviceListviewWidget()
        deviceWidget.setupUi(widgetContainer)

        deviceWidget.labelSerialNumber.setText(item.serialNumber)
        deviceWidget.labelModel.setText(item.model)
        deviceWidget.labelInstallationDate.setText(item.installDate)

        mainWidgetItem.setSizeHint(0, widgetContainer.sizeHint())   

        self.panelInfoPatientUi.treeWidgetDevices.addTopLevelItem(mainWidgetItem)
        self.panelInfoPatientUi.treeWidgetDevices.setItemWidget(mainWidgetItem, 0, widgetContainer)

        for files in item.listFile:
            #Files

            fileWidgetItem = QtGui.QTreeWidgetItem(mainWidgetItem)
            widgetContainerFiles = QtWidgets.QWidget()
            widgetContainerFiles.setObjectName("widgetContainerFiles")

            widgetContainerFiles.ui = Ui_FileListWidgetItem()
            widgetContainerFiles.ui.setupUi(widgetContainerFiles)

            widgetContainerFiles.ui.labelFileName.setText(files.fileName)
            widgetContainerFiles.ui.labelDateFile.setText(files.dateFile)

            fileWidgetItem.setSizeHint(0, widgetContainerFiles.sizeHint())
            mainWidgetItem.addChild(fileWidgetItem)
            self.panelInfoPatientUi.treeWidgetDevices.setItemWidget(fileWidgetItem, 0, widgetContainerFiles)

我可以得到我的数据:

    def testest(self, item, col):

    print(str(item.treeWidget().itemWidget(item, col).ui.labelFileName.text()))