2

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?

student
  • 1,636
  • 3
  • 29
  • 53

3 Answers3

3

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:

Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Sorry, I somehow omitted that part of my original answer. Basically, I believe you should learn some PyQt4 first, and then learn some basic C++ syntax. From there, you should be able to guess how to convert the C++ into Python. – unutbu Jul 26 '12 at 10:56
2

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

2

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.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • The link to the source packages for PyQt was exactly what I was looking for! I was halfway through transcribing one of he `C++` examples and couldn't shake the feeling that this had already been done. Indeed, it had been :) – svenevs Apr 27 '17 at 03:12