PyQT:更改列表项时调用我自己的方法

时间:2015-04-23 17:51:15

标签: python user-interface pyqt4

我正在尝试使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'

1 个答案:

答案 0 :(得分:1)

当您用以下信号连接信号时,问题是:

QtCore.QObject.connect(self.ProjectType, QtCore.SIGNAL(_fromUtf8("itemSelectionChanged()")), self.fillListView())

您传递self.fillListView()的返回值,即None,而不是函数本身。

尝试:

QtCore.QObject.connect(self.ProjectType, QtCore.SIGNAL(_fromUtf8("itemSelectionChanged()")), self.fillListView)

此外,在您的fillListView()方法中,您可能希望实际使用以下方法运行clear方法:

self.list2.clear()  # note the parethesis

此外,正如其他评论员所说,您应该决定是否要将其称为self.list2self.List2。我会建议小写。