[PyKDE] Experiences of a new Qt/PyQt user.
Phil Thompson
phil at river-bank.demon.co.uk
Fri Jul 27 11:52:06 BST 2001
"Rob W. W. Hooft" wrote:
>
> 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.
Correct, and this is intentional. In the very early days connect did
increase the reference count, but this caused subsequent problems with
circular reference counts.
What version of Python are you using? With Python 2.1 and later you
should not get segfaults if the Python slot no longer exists because
they are stored as weak references.
> 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.
The simple rule is that NULL maps to None and vice versa. There may be
cases where this seems unpythonic - but PyQt is designed to mimic C++ Qt
as closely as possible. However, in this case QObjectList (the type
returned in C++ by children()) is mapped to a Python list automatically
so I think your point is valid. I'll change this for the next release -
it shouldn't break anything.
Phil
More information about the PyQt
mailing list