2015-11-27 76 views
0

我現在創建了一個QTreeWidget,當我單擊一個項目時,我想獲取小部件數據。 我填補我QTreeWidget這樣:在DoubleClick事件中從QTreeWidget獲取ItemWidget

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?

感謝

+0

您可以訪問您所添加的窗口小部件到樹,又名'fileWidgetItem'。你不能訪問任何'fileWidget',因爲它們不在樹中。在代碼中,'fileWidgetItem'和'fileWidget'之間沒有鏈接。 – Mel

+0

感謝您的回答。那麼這是什麼意思?我應該把隱藏的信息放在fileWidgetItem中嗎?或者改變我創建TreeView的方式? – PyNico

回答

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())) 
相關問題