[PyKDE] Experiences of a new Qt/PyQt user.

Rob W. W. Hooft rob at hooft.net
Thu Jul 26 14:04:49 BST 2001


I have recently started to use PyQt. This was also my first 
encounter with Qt. I am currently in the process of re-writing
my current Tkinter software using PyQt. 

There are a number of things I have found very difficult so far, and
maybe some experts can tell me how I should have done them instead. I
still have lots of things to learn, and any input I can get at this
point is very valuable to me. 

PyQt is the first ever python module I have tried that when improperly
used will generate Segmentation Faults in the program. This makes it
very difficult to debug....

Case 1: "emit"ting on a "connect"ion to a slot coded in Python will
        generate a segmentation fault if the destination object has
        been close()d. Apparently the "connect" call does not increase
        the refcount of the destination; and the connection is not
        removed upon destruction of the underlying C++ object.

I have a class called "stringvariable" that I can ask to make a
"Widget" that corresponds to it (a QLineEdit). There can be multiple
concurrent widgets belonging to the same variable. If any of the
widget values is changed, the variable value changes, and if the
variable value changes, all of the widgets change value. The variable
normally lives very long inside the program, but widgets might be
displayed in transient windows and be very short lived.

When I programmed this using "connect", the program would segfault whenever
the variable changed value after a corresponding widget() had been closed.
I now keep a widget list in the variable, and when the signal generates a 
"RunTimeError", I remove the widget from the list.

Case 2: I can't manage to do some close() handling in python, as it seems
        there is no call to the python code.

This is related to 1: as a solution for the crash I have tried to make
the widget notify the variable that it no longer exists. Except that I
could not get any of "close()" or "closeEvent()" to work. Only __del__
was called.

Case 3: I can't get disconnect to work.

Related to 1 and 2: as a solution to the crash in 1, I tried to "disconnect"
the signals from the variable to the widget in the python destructor for
the widget, but I didn't manage: disconnect didn't complain, but the 
program still crashed on the "emit".

Case 4: It is difficult to get a spinbox with non-linear numerical mapping.

Some of my floating-point variables are displayed using a spinbox, but
when the up arrow is pressed, the value is multiplied by sqrt(2), and when
the down arrow is pressed the value is divided by sqrt(2). This was very
difficult to do with a linear mapping. I finally managed by overwriting
the "stepUp", "stepDown" and "mapValueToText" methods of the QSpinBox.

A stripped down version of the code I have for these cases is attached
to this mail. It is still >500 lines, sorry.

Another remark: Sometimes the use of None in PyQt feels a bit unpythonic,
eg. for QWidget.children() it would be easier if an empty list could
be returned instead of None.

Thanks in advance for any input!

Regards,

Rob Hooft

-- 
=====   rob at hooft.net                 http://www.hooft.net/people/rob/  =====
=====   R&D, Bruker Nonius BV, Delft  http://www.nonius.nl/             =====
===== PGPid 0xFA19277D ================================= Use Linux! =========
-------------- next part --------------
A non-text attachment was scrubbed...
Name: variables.py
Type: application/octet-stream
Size: 17260 bytes
Desc: test module
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20010726/a1126a5d/variables.obj


More information about the PyQt mailing list