PyQt:通过Qt Designer将插槽与自定义对象连接 [英] PyQt: connecting slots with custom objects via Qt Designer

查看:357
本文介绍了PyQt:通过Qt Designer将插槽与自定义对象连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PyQt小部件,可以发送带有numpy.ndarray数据的信号.我还有另一个PyQt小部件,该小部件的插槽中装有numpy.ndarray数据.

I have a PyQt widget that sends signals with numpy.ndarray data. And I have another PyQt widget that has a slot with numpy.ndarray data.

两个小部件都位于我的主窗口中,该窗口是从* .ui文件编译而成的.这些小部件被设置为升级的小部件.

Both widget are located on my main window, that is compiled from *.ui file. The widgets are set as promoted widgets.

我不能以某种方式连接Qt Creator中的信号和插槽吗?

Cannot I somehow connect the signal and slot in Qt Creator?

现在它给了我下一个错误:

Just now it gives me the next error:

TypeError: C++ type 'ndarray' is not supported as a slot argument type

推荐答案

原因是Qt仅支持QMetaType中定义的数据类型作为信号的参数传递,请参见

Reason for this is Qt only support the datatype defined in QMetaType passed as argument of signal, looks here http://pyqt.sourceforge.net/Docs/PyQt4/qmetatype.html#Q_DECLARE_METATYPE According to ekhumoro's POST, I update the following code, it should work for PyQt4, not tested on PyQt5.

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from numpy import *


class MyThread(QThread):
    mysignal = pyqtSignal(ndarray)

    def __init__(self, parent=None):
        super(MyThread, self).__init__(parent)

    def run(self):
        while True:
            QThread.msleep(100)
            self.mysignal.emit(array((1, 2, 3, 4)))


class MyWidget(QWidget):
    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)
        self.thread = MyThread()
        self.thread.mysignal.connect(self.handleSignal)
        self.thread.start()

    def handleSignal(self, data):
        print data


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    app.exec_()

这篇关于PyQt:通过Qt Designer将插槽与自定义对象连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆