How to use the new signal/slot definition method in pyqt4

Source: Internet
Author: User
Tags python decorator

**************************************** **************************************** ****

Www.xiabanl.com. Thanks for your support and suggestions.

**************************************** **************************************** *****

 

If you are using a version later than pyqt 4.5, in addition to the signal/slot connector method of the runtime system, you also have a method similar to the runtime method in Python.
This method is achieved through the following two new pyqt objects.

  • Pyqtsignal

  • Pyqtslot

As its name suggests, pyqtsingal is used to define signal, whilePyqtslot
It is used in slot.

First, we will learn how to use pyqtsignal to create a signal. See the example below:

From pyqt4 import qtcore

Class myqobject (qtcore. qobject ):
# Define a digital signal
Signal1 = qtcore. pyqtsignal ()

# Define a signal with an integer and its name is qtsignal2.
Signal2 = qtcore. pyqtsignal (INT, name = 'qtsignal2 ')

Def connectsigslot (Self ):
# Using the connect provided by the pysignal object itself, we can easily merge the pysignal object with the corresponding slot.

# Connect signal1 and myreceiver1.
Signal1.connect (self. myreceiver1)
# Connect signal2 and myreceiver2.
Signal2.connect (self. myreceiver2)

Def myemitter (self, ARG ):
# Using the emit function provided by the pyqtsignal object, we can easily produce signal.
Signal1.emit ()
Signal2.emit (10)

Def myreceiver1 (Self ):
Print 'myreceiver1 called'

Def myreceiver2 (self, ARG ):
Print 'myreceiver2 called with argument Value % d' % ARG

Simply put, signal is regarded as an object through pyqtsignal, so the functions required by signal can be determined by the method of signal.
Therefore, the definition and use of the entire signal are completely in line with the spirit of the object direction, and the program looks more straightforward.

Pyslot is a python decorator. we can define a method as a slot through it.

@QtCore.pyqtSlot()
def mySlot(self):
print 'mySlot received a signal')

@QtCore.pyqtSlot(int)
def mySlot2(self, arg):
print 'mySlot2 received a signal with argument %d' % arg)

The definition of the entire slot is similar to the definition of the entire slot method, and it becomes much simpler at runtime. If your UI is made through pyuic4
The slot name is used to specify the component and signal to be connected. For example, if your UI contains a clicked listener named mybtn
Signal. You only need to define the following slot in the class differences you have defined:

@QtCore.pyqtSlot(bool) 
def on_myBtn_clicked(self, checked):
print 'myBtn clicked.'

Pyqt automatically connects this slot to the clicked singal scheme of mybtn In the UI. It is really easy.

The definition and usage of the new singal/slot is a major reform in pyqt 4.5. It makes pyqt programs clearer and easier to understand. If you use pyqt 4.5 as a later version.
I suggest you start using this new method.

Reference:

  • Http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#new-style-signal-and-slot-support

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.