PyQT:更改列表项时调用我自己的方法
PyQT: Calling my own method when list item is changed
我正在尝试让 list2 清除其中包含的任何内容并为其分配一个新值,但是在尝试使用我自己的方法而不是内置方法时出现错误 fillListView
功能类似于 self.list2.clear
。我怎样才能避免它并使其按预期工作?
class Ui(object):
def setupUi(self, Ui):
self.List1= QtGui.QListWidget(self.ProjectNavigator)
self.List1.setGeometry(QtCore.QRect(10, 50, 141, 241))
self.List1.setObjectName(_fromUtf8("List1"))
self.List2 = QtGui.QListWidget(self.ProjectNavigator)
self.List2.setGeometry(QtCore.QRect(170, 50, 141, 241))
self.List2.setObjectName(_fromUtf8("List2"))
### QtCore.QObject.connect(self.ProjectType, QtCore.SIGNAL(_fromUtf8("itemSelectionChanged()")), self.List2.clear) This one works but it's only clear the list
QtCore.QObject.connect(self.ProjectType, QtCore.SIGNAL(_fromUtf8("itemSelectionChanged()")), self.fillListView())
这是我的方法:
def fillListView(self):
self.List2.clear,
self.List2.addItem("TEST1")
self.List2.addItem("TEST2")
回溯:
TypeError: arguments did not match any overloaded call:
QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 2 has unexpected type 'str'
您的问题是,当您将信号连接到:
QtCore.QObject.connect(self.ProjectType, QtCore.SIGNAL(_fromUtf8("itemSelectionChanged()")), self.fillListView())
您正在传递 self.fillListView()
的 return 值,即 None
,而不是函数本身。
尝试:
QtCore.QObject.connect(self.ProjectType, QtCore.SIGNAL(_fromUtf8("itemSelectionChanged()")), self.fillListView)
此外,在您的 fillListView()
方法中,您可能想要 运行 clear 方法:
self.list2.clear() # note the parethesis
此外,正如另一位评论者所说,您应该决定是否要将其称为 self.list2
或 self.List2
。我建议小写。
我正在尝试让 list2 清除其中包含的任何内容并为其分配一个新值,但是在尝试使用我自己的方法而不是内置方法时出现错误 fillListView
功能类似于 self.list2.clear
。我怎样才能避免它并使其按预期工作?
class Ui(object):
def setupUi(self, Ui):
self.List1= QtGui.QListWidget(self.ProjectNavigator)
self.List1.setGeometry(QtCore.QRect(10, 50, 141, 241))
self.List1.setObjectName(_fromUtf8("List1"))
self.List2 = QtGui.QListWidget(self.ProjectNavigator)
self.List2.setGeometry(QtCore.QRect(170, 50, 141, 241))
self.List2.setObjectName(_fromUtf8("List2"))
### QtCore.QObject.connect(self.ProjectType, QtCore.SIGNAL(_fromUtf8("itemSelectionChanged()")), self.List2.clear) This one works but it's only clear the list
QtCore.QObject.connect(self.ProjectType, QtCore.SIGNAL(_fromUtf8("itemSelectionChanged()")), self.fillListView())
这是我的方法:
def fillListView(self):
self.List2.clear,
self.List2.addItem("TEST1")
self.List2.addItem("TEST2")
回溯:
TypeError: arguments did not match any overloaded call:
QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'NoneType'
QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 2 has unexpected type 'str'
您的问题是,当您将信号连接到:
QtCore.QObject.connect(self.ProjectType, QtCore.SIGNAL(_fromUtf8("itemSelectionChanged()")), self.fillListView())
您正在传递 self.fillListView()
的 return 值,即 None
,而不是函数本身。
尝试:
QtCore.QObject.connect(self.ProjectType, QtCore.SIGNAL(_fromUtf8("itemSelectionChanged()")), self.fillListView)
此外,在您的 fillListView()
方法中,您可能想要 运行 clear 方法:
self.list2.clear() # note the parethesis
此外,正如另一位评论者所说,您应该决定是否要将其称为 self.list2
或 self.List2
。我建议小写。