[PyQt] Questions about the PyQt examples
David Boddie
david at boddie.org.uk
Sun Mar 18 19:30:00 GMT 2007
On Sun Mar 18 16:58:12 GMT 2007, Shriramana Sharma wrote:
> I have some questions as to the PyQt examples. Since I was practically
> offline for months on end I was not able to ask these questions when I
> was doing the examples. So please read patiently and answer. Thank you.
No problem. Out of interest, can you tell us if you are learning PyQt4
at the same time as you are learning Python, or do you have previous
experience with Python?
> 1)
>
> In #04, "self." is used repeatedly for setFixedSize, quit=QPushButton
> etc, whereas it is not similarly used in #05 - for lcd, slider etc. Why?
> Similarly, in #07, "self." is used for slider but not for lcd. Is there
> a reason?
The examples may have been ported to Python by different people.
> 2)
>
> In #08, two lines are not translated to Python from the C++ Qt example:
>
> lcd . setSegmentStyle ( QLCDNumber . Filled ) # missing in provided example
> self . setAutoFillBackground ( True ) # missing in provided example
>
> Is there a particular reason?
The second may be a result of when the example was ported. If it was ported
before Qt 4.1, that line may not have been present in the original C++
example. Since Qt 4.1, the contents of parent widgets are propagated to their
children unless explicitly disabled - that's why the line was added to later
versions of the C++ example.
> 3)
>
> In the same #08, if I remove the word self from the line:
>
> self.emit(QtCore.SIGNAL("angleChanged(int)"), self.currentAngle)
>
> I get an error:
>
> Traceback (most recent call last):
> File "./008.py.showserror-buthowdoesitwork", line 62, in setAngle
> self . emit ( SIGNAL ( "angleChanged ( int )" ), currentAngle )
> NameError: global name 'currentAngle' is not defined
>
> But the program still works normally. How?
Exceptions are caught by the signal-slot dispatch mechanism in Python.
This code looks different to the code in a recent snapshot, where
> 4)
>
> In #02, the last but one line has a semicolon at the end. This is C
> syntax, not Python syntax. But still Python executes the program without
> any warning or error. How?
Semicolons separate statements in a statement list:
http://docs.python.org/ref/compound.html
So, they're perfectly legal to use, if not actually required in this case.
> 5)
>
> Why do the tutorials not use tr() for user visible strings?
I've no idea - someone should probably fix that. :-)
> 6)
>
> I observed that a large part of the functions in the PyQt API take self
> as the first object (to emphasize that it should be called only on an
> instance of the class) but the calls in the examples do not include self
> as the first item. How is it that Python doesn't throw any errors?
Because you don't need to pass self when calling a method of an instance.
> 7)
>
> If there is no need to include "self", then why is it necessary to say
> "self" in the QWidget __init__ constructor whenever I subclass QWidget
> within PyQt?
This is related to question #6. When you implement the __init__() method,
you want to set up all of the things that QWidget does, so you need to
call its __init__() method.
class Widget(QWidget):
def __init__(self, parent):
QWidget.__init__(self, parent)
In the above __init__() implementation, you can't just call
self.__init__(parent)
because that will lead to infinite recursion - you need to explicitly
call the base class's __init__() method. In this case (unlike in #6)
you need to pass self as the first argument because you are calling
the method on the class itself.
> 8)
>
> Finally, is there any performance difference between
>
> from PyQt4 import QtCore, QtGui
>
> and
>
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
The first case will just import the QtCore and QtGui modules into the
program's namespace. The second case imports all the classes from those
modules into the namespace.
The first case is often considered to be tidier and less error-prone than the
second one.
David
More information about the PyQt
mailing list