我正在通过制作一个简单的Twitter客户端来练习PyQt和(Q)线程。我有两个线程。
主/GUI线程。
Twitter fetch线程-每X分钟从Twitter获取数据。
所以,每隔X分钟,我的Twitter线程就会下载一组新的状态更新(Python列表)。我想把这个列表交给Main/GUI线程,这样它就可以用这些状态更新窗口。
我假设我应该使用signal/slot系统将“statuses”Python列表从Twitter线程传输到Main/GUI线程。所以,我的问题有两个:
如何从Twitter线程发送状态?
如何在主/GUI线程中接收它们?
据我所知,PyQt默认情况下只能通过信号/插槽发送PyQt对象。我想我应该以某种方式注册一个自定义信号,然后我可以发送,但我发现的文件是非常不清楚的一个新手喜欢我。我订了一本PyQt的书,但再过一周就到不了了,我不想等到那时候。:-)
我在Ubuntu上使用PyQt 4.6-1
更新:
这是不起作用的代码的一个例外。首先,我尝试将信号(“newStatuses”,我刚编好的一个名字)连接到Main/GUI线程中的函数self.update_tweet_list:
QtCore.QObject.connect(self.twit_in,
QtCore.SIGNAL("newStatuses (statuses)"),
self.update_tweet_list)
然后,在Twitter线程中,我这样做:
self.emit(SIGNAL("newStatuses (statuses)"), statuses)
调用此行时,我将收到以下消息:
QObject::connect: Cannot queue arguments of type 'statuses'
(Make sure 'statuses' is registered using qRegisterMetaType().)
我搜索了qRegisterMetaType(),但没有找到任何可以理解的与Python相关的内容。
回头看看这个question I asked。有一个代码示例可以帮助您找出需要执行的操作。
你所说的注册你的信号让我想到了这个代码(从上面的问题):
我在示例中传递字符串,但您应该能够用
list
替换str
。如果结果发现不能传递可变对象,可以像我在示例中那样处理结果(即在线程中设置一个
results
变量,告诉主线程它们已经准备好了,并让主线程“接收它们”)。更新:
您得到消息
QObject::connect: Cannot queue arguments of type 'statuses'
,因为您需要定义当您发出信号时将通过的类型参数。要传递的类型是list
,而不是statuses
。当你连接你的信号时,它应该是这样的:
当你发出信号时,它应该是这样的:
假设
statuses
是一个列表。请注意,根据您的情况,您可能需要发送列表的深层副本。更新2:
好的,使用
list
作为类型是不正确的。从PyQt4帮助参考:你也可以这样做,这是更Python(和可读!)。
从这个例子来看:
http://doc.qt.digia.com/4.5/qmetatype.html
你可以用Python写下来
编辑
从评论中得到的答案是:
相关问题 更多 >
编程相关推荐