473,387 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

PyQT: pausing an application for the next event

Hello,

[Python 2.3 + QT 2.3.0 + PyQT 3.8.0-nc]

I am wondering if QT has something like QWaitForNextEvent() function.
This function would block execution of the application till another key
was pressed and then return the event which occured.

Would like to utlize this in a small application I am building:
I am trying to create vi key-bindings for a simple QMultiLineEdit
control. I created a class which overloads the QMultiLineEdit class
and then re-defined its keyPressEvent() function. If something like
QWaitForNextEvent() were available, I would have done something like the
following:

def MySimpleVi(QMultiLineEdit):
def keyPressEvent(self, event):
if event.text() == 'd':
self.deleteLine(event)

def deleteLine(self, event):
count = 0
movement = None

nextevent = QWaitForNextEvent()
while iscount(nextevent):
count = 10*count + int(nextevent).text
nextevent = QWaitForNextEvent()

if ismovement(nextevent):
movement = nextevent.text()

range = calculateRange(self.getCursorPosition(), movement, count)
self.deleteRange(range)

Without a function like QWaitForNextEvent(), I will have to make
deleteLine() somehow remember its state between successive calls. I will
also have to make the keyPressEvent() function remember state between
calls so that it can keep passing events to deleteLine() till
deleteLine() is "done". Such things will get even more complex if I plan
to implement vi commands like "3d3j", which take an optional count
before the command description and then execute the command so many
times.

Any advice/suggestions would be greatly appreciated.

If PyQT does not have something like this, is there some completeley
different way to accomplish something similar? Basically have a function
pause between successive calls and resume from last time but with one of
the local variables corresponding to the function arguments changed?
This sounds a little like generator functions in Python, but I cannot
think of a way to use them here.

I searched the QT-interest mailing list and found this to be of
interest:

http://lists.trolltech.com/qt-intere...ad00270-0.html

However, as Mr. Peter Becker says after offering a solution:

But my last experiences using such techniques taught me: don't use
such stuff if you can avoid it. You can run in different kinds of
deadlocks and livelocks -- I even got a reproducable Zombie this way
(NT showed a running process without task). Plus: to much calling of
QApplication::processEvents() seems to result in big CPU load.
Thanks,

Srinath

Jul 18 '05 #1
2 4186
On Monday 13 October 2003 10:35 am, Srinath Avadhanula wrote:
Hello,

[Python 2.3 + QT 2.3.0 + PyQT 3.8.0-nc]

I am wondering if QT has something like QWaitForNextEvent() function.
This function would block execution of the application till another key
was pressed and then return the event which occured.

Would like to utlize this in a small application I am building:
I am trying to create vi key-bindings for a simple QMultiLineEdit
control. I created a class which overloads the QMultiLineEdit class
and then re-defined its keyPressEvent() function. If something like
QWaitForNextEvent() were available, I would have done something like the
following:

def MySimpleVi(QMultiLineEdit):
def keyPressEvent(self, event):
if event.text() == 'd':
self.deleteLine(event)

def deleteLine(self, event):
count = 0
movement = None

nextevent = QWaitForNextEvent()
while iscount(nextevent):
count = 10*count + int(nextevent).text
nextevent = QWaitForNextEvent()

if ismovement(nextevent):
movement = nextevent.text()

range = calculateRange(self.getCursorPosition(), movement, count)
self.deleteRange(range)

Without a function like QWaitForNextEvent(), I will have to make
deleteLine() somehow remember its state between successive calls. I will
also have to make the keyPressEvent() function remember state between
calls so that it can keep passing events to deleteLine() till
deleteLine() is "done". Such things will get even more complex if I plan
to implement vi commands like "3d3j", which take an optional count
before the command description and then execute the command so many
times.

Any advice/suggestions would be greatly appreciated.

If PyQT does not have something like this, is there some completeley
different way to accomplish something similar? Basically have a function
pause between successive calls and resume from last time but with one of
the local variables corresponding to the function arguments changed?
This sounds a little like generator functions in Python, but I cannot
think of a way to use them here.

I searched the QT-interest mailing list and found this to be of
interest:

http://lists.trolltech.com/qt-intere...ad00270-0.html

However, as Mr. Peter Becker says after offering a solution:

But my last experiences using such techniques taught me: don't use
such stuff if you can avoid it. You can run in different kinds of
deadlocks and livelocks -- I even got a reproducable Zombie this way
(NT showed a running process without task). Plus: to much calling of
QApplication::processEvents() seems to result in big CPU load.


The (now obsolete) QApplication.processOneEvent() is probably the nearest -
but it processes the event and does not return it for you to process the way
you want to. Also, as the documentation says "Using this function in new
applications may be an indication of design problems."

Your code seems to make assumptions about what event is expected next. Events
are asynchronous and used for all sorts or things.

What's wrong with keeping state information? Handling an optional prefix
hardly complicates things much.

Phil
Jul 18 '05 #2

[replying to my own email]

It turns out that using generators in python, we can build an equivalent
of the fictional QWaitForNextEvent (which blocks execution till an event
occurs and then returns the event), using generator functions in python.
It is quite cool.

For example, the code
def MySimpleVi(QMultiLineEdit):
def keyPressEvent(self, event):
if event.text() == 'd':
self.deleteLine(event)

def deleteLine(self, event):
count = 0
movement = None

nextevent = QWaitForNextEvent()
while iscount(nextevent):
count = 10*count + int(nextevent).text
nextevent = QWaitForNextEvent()

if ismovement(nextevent):
movement = nextevent.text()

range = calculateRange(self.getCursorPosition(), movement, count)
self.deleteRange(range)
which uses a fictional QWaitForNextEvent() could have been written
without it using generator functions as:
def MySimpleVi(QMultiLineEdit):
def __init__(self):
self.handler = None

def keyPressEvent(self, event):
# If we have a handler defined, it means that the handler
# requires more events to finish.
if self.handler != None:
# first store the event in a class-local variable and
# then call next() method of the handler.
self.nextevent = event
if self.handler.next() != None:
self.handler = None

if event.text() == 'd':
self.handler = self.deleteLine()
self.handler.next()

def deleteLine(self):
count = 0
movement = None

while iscount(self.nextevent):
count = 10*count + int(self.nextevent.text())
# By yielding None, we convey to keyPressEvent that we
# are not finished yet. We will thus essentially "pause"
# here till another event occurs and keyPressEvent()
# calls our next().
yield None

if ismovement(self.nextevent):
movement = self.nextevent.text()

range = calculateRange(self.getCursorPosition(), movement, count)
self.deleteRange(range)
# return 1 to signify that we are do not want any more
# events.
yield 1

Pretty elegant if I say so myself. The deleteLine() function did not
have to change at all! Ofcourse, without the generator methodology, I
would have had to make things much more complex looking and generally
obfuscated. Hail Python's generators!!! :)

This is ofcourse completely untested yet. It also hinges on the fact
that if the parent function changes self.nextevent, the generator
function sees the new value in the next iteration and not just the
cached value or something of the sort (actually just verified that the
correct thing indeed happens)...

--
Srinath

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Vamsi Mudrageda | last post by:
I am kind of new to Python, and after trying and using wxPython, I found it kind of lacking in easy-to-read documentation, speed at loading, and GUI response-time. So I am looking for an another...
4
by: Adrian Casey | last post by:
I'm using a QTimer object to expire certain password protected GUI options in my application after 2 minutes. Currently, the timer is reset each time the user presses the 'OK' button. This is not...
3
by: Fabio | last post by:
Hi all, I'm about to write an application, and I'd like to use PyQt, but before choosing this toolkit I would like to clarify some particular licensing issues; if some user has already touched...
7
by: Dr. Know | last post by:
I am working on an ASP page that writes to several databases, ranging from MDBs to x-base. One of the tasks involves using an existing highest value from the DB and incrementing it before...
2
by: Dave Harris | last post by:
I am having a bit of trouble with form manipulation. I am working on a program where the user opens one form and when all the processes are done, he/she clicks on "Continue" which opens the next...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
3
by: Phil Thompson | last post by:
Riverbank Computing is pleased to announce the release of PyQt v4.0beta1 available from http://www.riverbankcomputing.co.uk/pyqt/. PyQt is a comprehensive set of Qt bindings for the Python...
25
by: Kwebway Konongo | last post by:
Hi everyone, I'm developing an application in C; basically a linked list, with a series of "events" to be popped off, separated by a command to pause reading off the next event in the list. It has...
12
by: greg | last post by:
Hi, Can anyone help me with the following issue: How can I pause the execution of a program until a given file is created (by another process) in a specified directory? Any ideas would be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.