链接到setItem的Python QtGui按钮
我对PyQT4很陌生。我正在尝试将按钮链接到表中的设置项。以下是我到目前为止的情况链接到setItem的Python QtGui按钮,python,pyqt4,Python,Pyqt4,我对PyQT4很陌生。我正在尝试将按钮链接到表中的设置项。以下是我到目前为止的情况 from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _transl
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.tableWidget = QtGui.QTableWidget(Form)
self.tableWidget.setGeometry(QtCore.QRect(0, 0, 256, 192))
self.tableWidget.setObjectName(_fromUtf8("tableWidget"))
self.tableWidget.setColumnCount(3)
self.tableWidget.setRowCount(3)
self.pushButton = QtGui.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(90, 230, 75, 23))
self.pushButton.setObjectName(_fromUtf8("pushButton"))
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("test")))
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "form", None))
self.pushButton.setText(_translate("Form", "CLEAR", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
我得到一个相当长的错误(如下)。是否有更好的方法将按钮链接到我想要的结果
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\PyQt4\test_scripts\ag_test.py", line 57, in <module>
ui.setupUi(Form)
File "C:\Python27\Lib\site-packages\PyQt4\test_scripts\ag_test.py", line 40, in setupUi
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("Adam")))
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'
回溯(最近一次呼叫最后一次):
文件“C:\Python27\Lib\site packages\PyQt4\test\u scripts\ag\u test.py”,第57行,在
ui.setupUi(表单)
文件“C:\Python27\Lib\site packages\PyQt4\test\u scripts\ag\u test.py”,位于setupUi的第40行
QtCore.QObject.connect(self.butdown,QtCore.SIGNAL(_fromUtf8(“clicked()”),self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem(“Adam”))
TypeError:参数与任何重载调用不匹配:
QObject.connect(QObject,SIGNAL(),QObject,SLOT(),Qt.ConnectionType=Qt.AutoConnection):参数3具有意外的类型“NoneType”
QObject.connect(QObject,SIGNAL(),可调用,Qt.ConnectionType=Qt.AutoConnection):参数3具有意外的类型“NoneType”
QObject.connect(QObject,SIGNAL(),SLOT(),Qt.ConnectionType=Qt.AutoConnection):参数2具有意外的类型“str”
您必须为connect
提供一个可调用的(函数指针,如果愿意的话)。但是,您不是在评估setItem
并给出答案
请尝试以下操作:
QtCore.QObject.connect(..., lambda x: self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("test")))
另一个解决方案:
def setupUi(self, Form):
...
self.pushButton.clicked.connect(self.setTableWidgetItem)
def setTableWidgetItem(self):
self.tableWidget.setItem(0,0,QtGui.QTableWidgetItem("test"))