在PyQt中使用用户定义的槽

时间:2014-01-21 03:59:17

标签: pyqt

我使用信号和插槽来使用按钮来触发调用“gc.speed_rpm”(使用的已定义插槽或我自己的方法/功能)的事件并在文本浏览器上显示它的输出时出现问题窗口小部件。

QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.textBrowser_2, gc.speed_rpm)

我收到以下错误:

arguments did not match any overloaded call:
  QObject.connect(QObject, SIGNAL(), QObject, SLOT(),     Qt.ConnectionType=Qt.AutoConnection): argument 4 has unexpected type 'instancemethod'
  QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 3 has unexpected type 'QTextBrowser'

可能出现什么问题?任何想法。

提前致谢。

1 个答案:

答案 0 :(得分:2)

试试这个:

def __init__(self):
  # Connect the clicked action of the push button to a custom slot
  self.pushButton.clicked.connect(self.display_speed_rpm)

def display_speed_rpm(self):
  # Use of setPlainText or setHtml depends on the output of gc.speed_rpm()
  self.textBrowser_2.setPlainText(gc.speed_rpm())

编辑:您还可以使用装饰器方式将小部件信号连接到插槽。您必须根据小部件名称和信号名称命名您的插槽:

@QtCore.pyqtSlot()
def on_pushButton_clicked(self):
  # Use of setPlainText or setHtml depends on the output of gc.speed_rpm()
  self.textBrowser_2.setPlainText("{}".format(gc.speed_rpm()))