Where is the PyQt5 documentation for classes, methods and modules?

I'm trying to learn PyQt5 and I am finding it very difficult since I can't just guess what methods are available. I've just spent an entire week trying to find a method to simulate a button push. I eventually found the solution ( QPushButton.animateClick() ) only after stumbling across an example someone left out there (how did this person know this?). It's very difficult to develop without some reference to what's available for tools! Riverbank has a version of what I'm looking for but it is not complete making it virtually useless.

1

2 Answers

being a binding has almost all the functionalities (there are minimal known incompatibilities) so the documentation: is valid for except for small exceptions.

Although the target of the documentation is the description of the classes and methods are generic, so they also validly apply for , on the other hand many of the examples are written in but the translation to python in many cases is trivial .

So to avoid doing a double task it seems that Riverbank Computing Limited only documents the exceptions indicated in the docs:


The next part of my answer will propose tips to handle the Qt documentation.

The Qt documentation also has an easy to understand structure, for example let's analyze the QPushButton class ():

At the top there is a table:

enter image description here

This table indicates how to include the class in a C++ project, how to add it to qmake, from which class it inherits, and which classes inherit from it. From the above, relevant information for PyQt5 can be extracted, such as to which sub-module the class belongs to: In this case we use QT += widgets that inform us that it belongs to the QtWidgets sub-module, in general if Qt += submodulefoo belongs to QtSubModuleFoo (camelcase)

If you want to know all the methods of the QPushButton class, you must use the "List of all members, including inherited members" link below the table, in this case the link will be where is the complete list of all class methods, enumerations, etc.

Other tips to understand the conversion between Qt/C++ and PyQt5/Python are:

  1. Some methods use pointers to receive information such as:

    those transformed to PyQt5 as:

    lay = QtWidgets.QXLayout()
    left, top, right, bottom = lay.getContentsMargins()
    process = QProcess()
    # ...
    ok, pid = process.startDetached()
  2. Some methods collide with reserved words such as exec , raise, print, etc so to avoid incompatibilities, the underscore is added at the end: exec_, raise_, print_, etc

  3. In Qt, the Q_SLOT and Q_SIGNAL that are translated into python are used through the @pyqtSlot and @pyqtSignal decorators.


In conclusion, my recommendation is that you use the Qt and PyQt5 documentation at the same time to know all the functionalities, in addition there are many Q&A in SO about translations from one language to another so you could learn from there.

The Qt documentation can also be consulted using the Qt Assistant tool.

The main PyQt5 documentation is on the official website:

But it's still incomplete, and most parts refer to the official Qt documentation:

While that's C++ oriented, consider that almost every module, class and function behave exactly in the same way as it does in python, so it's usually better to use that.

Consider that:

  • in the function lists you'll always see the returned type on the left of each function;
  • "void" means that the function returns None;
  • when overriding some existing method (expecially private and virtual), you always have to return the expected types listed for that function;
  • function arguments are usually written in the form [const] Type argName=default: you can usually ignore the "const" part (it's a C++ term), while the argName for keyword arguments might be different in PyQt;
  • some functions could have different names, since they're reserved on python (print, raise, etc); in those cases, an underscore is always appended;
  • some positional or keyword arguments might be different, or the return type signature might; that's because in C++ you can use a pointer to a variable as an argument, and the function will change that variable using the pointer (this is an oversimplification);
  • all "properties" are not python properties, and they are only accessible through their parenthesis functions, such as self.width() an self.setWidth();
  • some methods have different overrides, in some cases python has special cases with different arguments that are not available in C++, and viceversa; also, some methods don't exist at all in one case or the other;

My suggestion is to always use the official documentation, it's only a matter of habit to get used to the C++ references (and you'll see that it is educational too); whenever some doubt raises, check the PyQt documentation to see what's different and use the help command in the python shell.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like