Pyqt event processing

Source: Internet
Author: User

Qt event processing 01

The second method for Qt to process events: "re-implementing the qobject: event () function", by re-implementing the event () function, events can be intercepted and processed before they reach a specific event processor. This method can be used to overwrite the default Processing Method of defined events, or to process events with a specific event processor not defined in QT. When the event () function is re-implemented, if no event processing is performed, the base class event () function must be called.

QT is event-driven, and every action of a program is triggered by an event. There are many types of QT events,
We can view Event System and qevent in manual of QT to obtain detailed information about each event.
Event Source
Spontaneous events (Spontaneous event)
Messages obtained from the system, such as mouse buttons and keyboard buttons. Read these events during QT event loop, convert them to qevents, and process them in sequence.
Posted events
Generated by QT or applications and placed in Message Queue
Qcoreapplication: postevent ()
Sent events
Generated by QT or applications and directly distributed and processed without being put into a queue
Qcoreapplication: sendevent ()
For example, the paintevent () function can be called for three types of events:

When the window is overwritten by another window and re-displayed, the spontaneous event is generated to request re-painting.
When Update () is called, The posted event is generated.
When repaint () is called, an sent event is generated.
Event dispatch event Loop

From pyqt4.qtgui import *

From pyqt4.qt import *

From pyqt4.qtcore import *

Import sys

# First, use a custom control to re-implement a specific event processor, derive a component, and re-implement its event processing. The three events are mainly processed by mousepressevent, mousereleaseevent, and mousemoveevent.

Class mybuuton (qpushbutton ):

Def _ init _ (self, parent = none ):

Super (mybuuton, self). _ init _ (parent)

Def mousepressevent (self, event ):

Self. settext (qstring ('x: % 1, Y: % 2 '). arg (qstring. number (event. X ())). arg (qstring. number (event. Y ())))

Def mousereleaseevent (self, event ):

Self. settext (qstring ('x: % 1, Y: % 2 '). arg (qstring. number (event. X ())). arg (qstring. number (event. Y ())))

Def mousemoveevent (self, event ):

Self. settext (qstring ('x: % 1, Y: % 2 '). arg (qstring. number (event. X ())). arg (qstring. number (event. Y ())))

 

Qtextcodec. setcodecfortr (qtextcodec. codecforname ('utf-8 '))

APP = qapplication (SYS. argv)

X = mybuuton ()

X. setwindowtitle (u'processor ')

X. Resize (400,200)

X. Show ()

App.exe C _()

When running in sequence, the text on the button is displayed with the mouse clicking, releasing, and left clicking and dragging at different locations.

:

________________________________________________________________________

Qt event processing 02

The second method for Qt to process events: "re-implementing the qobject: event () function", by re-implementing the event () function, events can be intercepted and processed before they reach a specific event processor. This method can be used to overwrite the default Processing Method of defined events, or to process events with a specific event processor not defined in QT. When the event () function is re-implemented, if no event processing is performed, the base class event () function needs to be called.

From pyqt4.qtgui import *

From pyqt4.qt import *

From pyqt4.qtcore import *

Import sys

# First, use a custom control to re-implement a specific event processor, derive a component, and re-implement its event processing. The three events are mainly processed by mousepressevent, mousereleaseevent, and mousemoveevent.

Class mybuuton (qpushbutton ):

Def _ init _ (self, parent = none ):

Super (mybuuton, self). _ init _ (parent)

Def mousepressevent (self, event ):

Self. settext (qstring ('x: % 1, Y: % 2 '). arg (qstring. number (event. X ())). arg (qstring. number (event. Y ())))

Def mousereleaseevent (self, event ):

Self. settext (qstring ('x: % 1, Y: % 2 '). arg (qstring. number (event. X ())). arg (qstring. number (event. Y ())))

Def mousemoveevent (self, event ):

Self. settext (qstring ('x: % 1, Y: % 2 '). arg (qstring. number (event. X ())). arg (qstring. number (event. Y ())))

Def event (self, E): # Check &&

If E. Type () = qevent. mousebuttonpress:

 

Event = mybuuton (E)

Self. settext (qstring ('x: % 1, Y: % 2 '). arg (qstring. number (event. X ())). arg (qstring. number (event. Y ())))

Return true

Elif E. Type () = qevent. mousebuttonpress or E. Type () = qevent. mousemove: # block mousebuttonrelease and mousemove events

Return true

Return qpushbutton. Event (e) # other events call the event () function of the base class for processing.

Qtextcodec. setcodecfortr (qtextcodec. codecforname ('utf-8 '))

APP = qapplication (SYS. argv)

X = mybuuton ()

X. setwindowtitle (u'processor ')

X. Resize (400,200)

X. Show ()

App.exe C _()

 

Return qpushbutton. Event (e) # other events call the event () function of the base class for processing.
Typeerror: qpushbutton. Event (qevent): first argument of unbound method must have type 'qpushbutton'

If you see this blog, please give me some advice. Thank you.

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.