PyQt遇到的一些问题

  • QString

最近在使用PyQt GPL v4.10.1 for Python v3.3 (x32)编写界面的时候,总是遇见余下问题:

1 >>> from PyQt4 import QtCore
2 >>> QtCore.QString('nihao')
3 Traceback (most recent call last):
4   File "<pyshell#1>", line 1, in <module>
5     QtCore.QString('nihao')
6 AttributeError: 'module' object has no attribute 'QString'

查看PyQt4的reference guid,里面介绍到:

The QString class is implemented as a mapped type that is automatically converted to and from a Python string. In addition a None is converted to a null QString. However, a null QString is converted to an empty Python string (and not None). (This is because Qt often returns a null QString when it should probably return an empty QString.)

在一个国外的网站上看到:

From PyQt4 4.6+ in Python3 QString doesn't exist and you are supposed to use ordinary Python3 unicode objects (string literals). To do this so that your code will work in both Python 2.x AND Python 3.x you can do following:

从适用于Python3的PyQT4.6版本以后,QString不再存在,我们可以使用Python3中的str类型代替。加入如下的代码,可以是你的code可以再python2.x和Python3.x上都能工作。

1 try:
2     from PyQt4.QtCore import QString
3 except ImportError:
4     # we are using Python3 so QString is not defined
5     QString = type("")
  • New-style Signal and Slot Support

One of the key features of Qt is its use of signals and slots to communicate between objects. Their use encourages the development of reusable components.

A signal is emitted when something of potential interest happens. A slot is a Python callable. If a signal is connected to a slot then the slot is called when the signal is emitted. If a signal isn’t connected then nothing happens. The code (or component) that emits the signal does not know or care if the signal is being used.

The signal/slot mechanism has the following features.

  • A signal may be connected to many slots.
  • A signal may also be connected to another signal.
  • Signal arguments may be any Python type.
  • A slot may be connected to many signals.
  • Connections may be direct (ie. synchronous) or queued (ie. asynchronous).
  • Connections may be made across threads.
  • Signals may be disconnected.

定义Signal:

 1 from PyQt4 import QtCore
 2 
 3 class testSignal(QtCore.QObject):
 4     def __init__(self):
 5         super(testSignal,self).__init__()
 6         self.signalemitFlag = False
 7     signals1 = QtCore.pyqtSignal(str)##发送str类型的信号
 8 
 9 
10     def testemitSignal(self):
11         if self.signalemitFlag == True:
12             self.signals1.emit('emit signals1!')
13 
14     def changeFlag(self,Flag):
15         self.signalemitFlag = Flag
16         self.testemitSignal()
17     
18 
19 class testSlot(QtCore.QObject):
20     def __init__(self):
21         super(testSlot,self).__init__() 
22         self.Signals = testSignal()
23         self.Signals.signals1.connect(self.testslot)
24 
25     def testslot(self,string):
26         print(string)
27 
28     def change(self,Flag):
29         self.Signals.changeFlag(Flag)
30 
31 class testSlot1(QtCore.QObject):
32     def __init__(self):
33         super(testSlot1,self).__init__()
34         self.Signals = testSignal()
35         self.Signals.signals1.connect(self.testslot)
36 
37     @QtCore.pyqtSlot(str)
38     def testslot(self,string):
39         print('@QtCore.pyqtslot(str)')
40         print(string)
41 
42     def change(self,Flag):
43         self.Signals.changeFlag(Flag)
44 
45 def Main():
46     testsignalslot = testSlot()
47     testsignalslot.change(True)
48     testsignalslot1 = testSlot1()
49     testsignalslot1.change(True)
50 
51 Main()

运行结果:

1 >>> import signal_slot
2 emit signals1!
3 @QtCore.pyqtslot(str)
4 emit signals1!

转载于:https://www.cnblogs.com/linux-database/archive/2013/05/16/3081869.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值