Learning PyQt from a book. Too much C ++ style code for Python?

Summerfield "Quick GUI Programming with Python and Qt." I like the book as a whole. The first few chapters are the best Python primer I have found online anyway, and the examples are varied and useful (but not perfect, although some additions and re-checks can be much more direct).

My problem is that I feel that maybe I'm going to get rid of her bad habits. Instead of simply dynamically assigning new attributes to an existing object, the author uses the setData functions, special MIMEData slots and much more. Here is a good example from one of the first questions I asked in Stackoverflow:

Installing and retrieving data from PyQt widget elements?

or, for example, in a web designer application:

def writeItemToStream(self, stream, item): if isinstance(item, QGraphicsTextItem): stream << QString("Text") << item.pos() \ << item.matrix() << item.toPlainText() << item.font() elif isinstance(item, QGraphicsPixmapItem): stream << QString("Pixmap") << item.pos() \ << item.matrix() << item.pixmap() elif isinstance(item, BoxItem): stream << QString("Box") << item.pos() \ << item.matrix() << item.rect stream.writeInt16(item.style) 

I correctly think that the author's background C ++ / Qt can make his examples suboptimal? Or should I continue to try to understand many of its options?

+4
source share
1 answer

You must remember that PyQt4 is a binding to Qt. Much of what is done in one, the same in the other, for API compatibility.

Obviously, C ++ is a static typed language, and python allows you to simply assign attributes to an object, but don't confuse this with something like calling setData. Typically, these calls establish internal elements that are not affected by you. This is where linking makes sense. In order for C ++ objects to get the expected interaction, you usually need to use the same api in pyqt

One example of something very C ++ related is QVariant. This is an object that can represent many different types. Python usually does not need this kind of object, but C ++ must support dynamic types in a single object. You will have to deal with this object when you work with QSettings sites or with model / view components.

This book is a great learning tool, and I would recommend not thinking about collecting bad habits. Mark knows what to learn to understand PyQt. Since you are learning python in general, you can easily make these differences in the ranks.

+3
source

All Articles