student
student

Reputation: 1646

Translate C++/Qt4 examples to PyQt4

I am trying to learn PyQt4 and there seem to be a lot usefull documentation for C++/Qt4 (for example: http://doc.qt.nokia.com/4.7-snapshot/model-view-programming.html). I do not know a lot about C++ and don't want to learn it at the same time, but the C++/Qt4 examples seem to be pretty usefull.

So is there a way to convert the C++ syntax (for instance in the examples from http://doc.qt.nokia.com/4.7-snapshot/model-view-programming.html) to python/PyQt4?

Upvotes: 2

Views: 3966

Answers (3)

ekhumoro
ekhumoro

Reputation: 120648

The definitive answer to this is: No, there is no way to automatically convert the Qt C++ examples to PyQt.

Nor is there is any general solution for translating C++ code into Python. It is precisely for that reason that the various language binding tools like SWIG, Boost, Sip, etc have been developed (the PyQt bindings are generated by Sip).

However, there is an ongoing effort to port all the Qt C++ examples to PyQt by hand. The current results of this effort can be found in the examples directory of the source packages for PyQt.

Upvotes: 2

Bob Newland
Bob Newland

Reputation: 21

http://www.zetcode.com/

It would benefit you greatly to have a small familiarity with C++ as a Python developer in principle. Anyway this the best Python reference for QT in my opinion.

Also, sorry this is so late

Upvotes: 2

unutbu
unutbu

Reputation: 880079

To convert the C++ examples to Python/PyQt4, it would help to first be familiar with the general structure of a PyQt4 app, and the usual syntax for constructing PyQt4 widgets and calling their methods.

Below I've listed resources to help you get started learning PyQt4 (without C++). Once you have a basic familiarity with PyQt4, you should be able to convert C++ code like this (taken from the tutorial you referenced):

 int main(int argc, char *argv[])
 {
     QApplication app(argc, argv);
     QSplitter *splitter = new QSplitter;

     QFileSystemModel *model = new QFileSystemModel;
     model->setRootPath(QDir::currentPath());

to the equivalent PyQt4 code:

import sys
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui

def main():
    app = QtGui.QApplication(sys.argv)
    splitter = QtGui.QSplitter()
    model = QtGui.QFileSystemModel()
    model.setRootPath(QtCore.QDir.currentPath())

Googling "QSplitter" and "QFileSystemModel" and "QDir" will lead you to the relevant PyQt4 documentation. It will tell you for instance, that the QApplication class is defined in the QtGui module. This will help you form associations between the C++ code and the equivalent PyQt4 code.

Of course, it also helps to know at least a little C++ syntax!


If you want to learn PyQt4 without knowing C++, I think it would be easiest to go through some of the tutorials listed here first.

Depending on your OS, your installation of pyqt4 may also come with a plethora of example code. For example, on Ubuntu, the python-qt4-doc package includes tutorials and a demo launcher (qtdemo) with numerous examples.

python /usr/share/doc/python-qt4-doc/examples/demos/qtdemo/qtdemo.py

enter image description here


Other useful links:

Upvotes: 3

Related Questions