[PyKDE] Problems with a port of C++/Qt book to Python

Jim Bublitz jbublitz at nwinternet.com
Mon Jul 18 20:33:34 BST 2005


On Monday 18 July 2005 08:48, Douglas Soares de Andrade wrote:
> Hi !
>
> Another problem:
>
> What about this construction ?
>
> class FindDialog :  public QDialog
> {
> 	Q_OBJECT
>
> 	public:
> 		FindDialog(QWidget *parent, const char *name = 0);
>
> All is clear for me, but what about the Q_OBJECT macro ? How to use it in
> PyQt ?

It's not required in Python/PyQt

> Another thing...
>
> How to use the tr() function ?

Any QObject descendant inherits it from QObject:

    self.tr ( ... )

in your example below. It's also a static member of QObject, so you could also 
use:

   QObject.tr ( ... )

any place. connect () works the same way.

> How to use this setCaption() in the code below, do i have to use self, or
> QDialog ?

   self.setCaption ( ... )

> And, in the connect line, what to do with the this keyword ?

If the object calling connect is a QObject descendant (all QWidgets are), 
then:

	self.connect ( ... )

will work. You can also do this:

     lineEdit = new QLineEdit(this);
     lineEdit.connect (lineEdit, SIGNAL ("textChanged (const QString &)"),   
         self.slotTextChanged)

Also note the PyQt syntax - the signal name and arguments are enclosed in 
quotes, and the complete argument list (including &, *, etc) is used; the 
slot is any callable Python method or function.


> FindDialog::FindDialog(QWidget *parent, const char *name)

# don't forget the imports
from qt import QDialog,  QLabel, QLineEdit, QCheckBox, QPushButton,  SIGNAL,\   
     QObject

class FindDialog (QWidget):
>
>     : QDialog(parent, name)

    def  __init__ (self, parent, name):
        QDialog.__init__  (self, parent, name)
>
> {
>     setCaption(tr("Find"));

	self.setCaption (self.tr ("Find"))
>
>     label = new QLabel(tr("Find &what:"), this);

        label = QLabel (self.tr  ("Find &what:"), self)

>     lineEdit = new QLineEdit(this);

        lineEdit = QLineEdit (self)

>     label->setBuddy(lineEdit);

        label.setBuddy (lineEdit)

>     caseCheckBox = new QCheckBox(tr("Match &case"), this);

        caseCheckBox = QCheckBox (self.tr ("Match &case"), self)

>     backwardCheckBox = new QCheckBox(tr("Search &backward"), this);

        backwardCheckBox = QCheckBox (self.tr ("Search &backward"), self)
>
>     findButton = new QPushButton(tr("&Find"), this);

        findButton = QPushButton (self.tr ("&Find"), self)

>     findButton->setDefault(true);

        findButton.setDefault (True)  # note capitalization here and next line

>     findButton->setEnabled(false);

        findButton.setEnabled (False)
>
>     closeButton = new QPushButton(tr("Close"), this);

        closeButton = QPushButton (self.tr ("Close"), self)
>
>     connect(lineEdit, SIGNAL(textChanged(const QString &)),
>             this, SLOT(enableFindButton(const QString &)));


# three different ways to call connect - any should work (and it's better to
# be consistent); all assume slot is a member of this class

        self.connect (lineEdit, SIGNAL ("textChanged (const QString&)"),
            self.enableFindButton)  

>     connect(findButton, SIGNAL(clicked()),
>             this, SLOT(findClicked()));

        findButton.connect (findButton, SIGNAL ("clicked ()"),
             self.findClicked)

>     connect(closeButton, SIGNAL(clicked()),
>             this, SLOT(close()));

        QObject.connect (closeButton, SIGNAL ("clicked ()"), self.close)

Jim




More information about the PyQt mailing list