发送自定义 PyQt 信号? [英] Sending custom PyQt signals?

查看:52
本文介绍了发送自定义 PyQt 信号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过制作一个简单的 Twitter 客户端来练习 PyQt 和 (Q) 线程.我有两个 Qthread.

I'm practicing PyQt and (Q)threads by making a simple Twitter client. I have two Qthreads.

  1. 主/GUI 线程.

  1. Main/GUI thread.

Twitter 获取线程 - 每 X 分钟从 Twitter 获取数据.

Twitter fetch thread - fetches data from Twitter every X minutes.

因此,我的 Twitter 线程每 X 分钟下载一组新的状态更新(Python 列表).我想将此列表交给 Main/GUI 线程,以便它可以使用这些状态更新窗口.

So, every X minutes my Twitter thread downloads a new set of status updates (a Python list). I want to hand this list over to the Main/GUI thread, so that it can update the window with these statuses.

我假设我应该使用信号/槽系统将状态"Python 列表从 Twitter 线程传输到主/GUI 线程.所以,我的问题是双重的:

I'm assuming that I should be using the signal / slot system to transfer the "statuses" Python list from the Twitter thread, to the Main/GUI thread. So, my question is twofold:

  1. 如何从 Twitter 线程发送状态?

  1. How do I send the statuses from the Twitter thread?

我如何在主/GUI 线程中接收它们?

How do I receive them in the Main/GUI thread?

据我所知,PyQt 默认只能通过信号/槽发送 PyQt 对象.我想我应该以某种方式注册一个我可以发送的自定义信号,但是我发现的关于此的文档对于像我这样的新手来说非常不清楚.我订购了一本 PyQt 书,但它不会再过一周到货,我不想等到那时.:-)

As far as I can tell, PyQt can by default only send PyQt-objects via signals / slots. I think I'm supposed to somehow register a custom signal which I can then send, but the documentation on this that I've found is very unclear to a newbie like me. I have a PyQt book on order, but it won't arrive in another week, and I don't want to wait until then. :-)

我在 Ubuntu 上使用 PyQt 4.6-1

I'm using PyQt 4.6-1 on Ubuntu

更新:

这是无效代码的证明.首先,我尝试将信号(newStatuses",一个我刚编的名字)连接"到主/GUI 线程中的函数 self.update_tweet_list:

This is an excert from the code that doesn't work. First, I try to "connect" the signal ("newStatuses", a name I just made up) to the function self.update_tweet_list in the Main/GUI thread:

QtCore.QObject.connect(self.twit_in,
                       QtCore.SIGNAL("newStatuses (statuses)"),
                       self.update_tweet_list)

然后,在 Twitter 线程中,我这样做:

Then, in the Twitter thread, I do this:

self.emit(SIGNAL("newStatuses (statuses)"), statuses)

调用此行时,我收到以下消息:

When this line is called, I get the following message:

QObject::connect: Cannot queue arguments of type 'statuses'
(Make sure 'statuses' is registered using qRegisterMetaType().)

我搜索了 qRegisterMetaType(),但没有找到任何我能理解的与 Python 相关的内容.

I did a search for qRegisterMetaType() but I didn't find anything relating to Python that I could understand.

推荐答案

来自这个例子:

http://doc.qt.digia.com/4.5/qmetatype.html

 int id = QMetaType.type("MyClass");

你可以用 Python 写下来

You can write down in Python

from PyQt4 import QtCore    
id = QtCore.QMetaType.type('MyClass')

编辑

从评论中提取的答案:

self.emit(SIGNAL("newStatuses(PyQt_PyObject)"), statuses)

这篇关于发送自定义 PyQt 信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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