'QModelIndex' object has no attribute 'currentItem'和 argument 1 has unexpected type 'NoneType'

 

第一:

class UIDebugFrom(QWidget,debug_Widget):
    def __init__(self):
        super(UIDebugFrom,self).__init__()
        self.setupUi(self)  # 创建窗口
 
        Station1.stationname(self.tree_Widget)
 
        '''
    def stationname(self):
        root = QTreeWidgetItem(self.tree_Widget)
        root.setText(0, 'root')
        self.tree_Widget.addTopLevelItem(root)
        self.tree_Widget.clicked.connect(self.onClicked1)

    def onClicked1(self):
        item = self.tree_Widget.currentItem()
        print(item.text(0))
        '''
 当stationname和onClicked1这两个函数写在这个继承的类中时,不报错,
但是当单独提取这两个函数作为一个py文件,通过Station1.stationname(self.tree_Widget)导入到类中时,
就会进行'QModelIndex' object has no attribute 'currentItem'的报错

报错的py文件

station1.py

def onClicked1(treeWidget1):
    item = treeWidget1.currentItem().text(0)
    print(item)

def stationname(treeWidget):
    #treeWidget.setHeaderLAbels(['stationname'])
    root = QTreeWidgetItem(treeWidget)
    root.setText(0,'root')
    treeWidget.addTopLevelItem(root)
 )
    # TODO 优化3 给节点添加响应事件
    treeWidget1=0
    treeWidget.clicked.connect(onClicked1)

报错'QModelIndex' object has no attribute 'currentItem'

修改后变成

station1.py

def onClicked1(treeWidget1):
    item = treeWidget1.currentItem().text(0)
    print(item)

def stationname(treeWidget):
    #treeWidget.setHeaderLAbels(['stationname'])
    root = QTreeWidgetItem(treeWidget)
    root.setText(0,'root')
    child1 = QTreeWidgetItem(root)
    child1.setText(0,'child')
    child2 = QTreeWidgetItem(treeWidget)
    child2.setText(0, 'child1')
    treeWidget.addTopLevelItem(root)
    treeWidget.addTopLevelItem(child2)
    # TODO 优化3 给节点添加响应事件
    treeWidget1=0
    treeWidget.clicked.connect(onClicked1(treeWidget))

报错        argument 1 has unexpected type 'NoneType'

最终版:

station.py

def onClicked1(treeWidget1):
    item = treeWidget1.currentItem().text(0)
    print(item)

def stationname(treeWidget):
    #treeWidget.setHeaderLAbels(['stationname'])
    root = QTreeWidgetItem(treeWidget)
    root.setText(0,'root')
 
    treeWidget.addTopLevelItem(root)
    # TODO 优化3 给节点添加响应事件
    treeWidget.clicked.connect(lambda :onClicked1 (treeWidget))

大功告成

你可能感兴趣的:('QModelIndex' object has no attribute 'currentItem'和 argument 1 has unexpected type 'NoneType')