Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Summerfield's "Rapid GUI Programming with Python and Qt". I love the book in general. The first few chapters are the best Python primer I found online or otherwise, and the examples are varied and helpful (not perfect though, there's some padding and retracing for sure- it could be far more direct).

My problem is that I feel I might be picking up bad habits from it. Instead of just dynamically assigning new attributes to existing object, the author uses "setData" functions, special MIMEData slots and whatnot. Here's a good example from one of the first questions I asked on Stackoverflow:

Setting and getting "data" from PyQt widget items?

or, for instance, in a page-designer app:

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)

Am I right to think the author's C++/Qt background might make his examples sub-optimal? Or should I keep trying to make sense of a lot of his choices?

share|improve this question

closed as not constructive by Wooble, ekhumoro, Jim Garrison, KillianDS, Graviton Sep 10 '12 at 4:50

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 3 down vote accepted

You have to remember that PyQt4 is a binding to Qt. A lot of what is done in one, is the same in the other, for API compatability.

Obviously C++ is a static typed language and python allows you to simply assign attributes to object, but dont confuse that with something like a setData call. Normally those calls set internal members that are not exposed to you. This is where the binding aspect makes sense. In order for the C++ objects to get the interaction they expect, you would usually need to make use of the same api in pyqt

One example of something very C++ related is the QVariant. It is an object that can represent many many different types. Python doesnt normally need this kind of object, but C++ would to hold dynamic types in one object. You will have to deal with this object when you work with say QSettings, or the model/view components.

That book is an excellent learning tool and I would recommend not thinking about picking up bad habits. Mark knows what needs to be taught for an understanding of PyQt. As you learn python in general, you will easily be able to make these distinctions down the line.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["                  <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2015 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2015.2.26.2347\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </nos"], "copyright__copyright__copyright": ["ne object. You will have to deal with this object when you work with say QSettings, or the model/view components.\n\nThat book is an excellent learning tool and I would recommend not thinking about picking up bad habits. Mark knows what needs to be taught for an understanding of PyQt. As you learn python in general, you will easily be able to make these distinctions down the line.\n\nshare|improve this answer\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}