background preloader

PyQt

Facebook Twitter

PyQt by Example. The backend The most re­cent ver­sion of this ses­sion (in RST for­mat) is al­ways avail­able at GitHub’s mas­ter tree as tut1.txt Since we are de­vel­op­ing a TO­DO ap­pli­ca­tion, we need a back­end that han­dles the stor­age, re­trieval and gen­er­al man­ag­ing of TO­DO tasks. To do that the sim­plest pos­si­ble way, I will do it us­ing Elixir, “A declar­a­tive lay­er over the SQLAlche­my Ob­jec­t-Re­la­tion­al Map­per”. If that sound­ed very scary, don’t wor­ry. What that means is “a way to cre­ate ob­jects that are au­to­mat­i­cal­ly stored in a database”.

Here is the code, with com­ments, for our back­end, called to­do.py. The Main Window Now, let’s start with the fun part: PyQt! I rec­om­mend us­ing de­sign­er to cre­ate your graph­i­cal in­ter­faces. And here is the Qt De­sign­er file for it: win­dow.ui. This is how it looks in de­sign­er: The main win­dow, in de­sign­er. What you see is a “Main Win­dow”. You can see how this win­dow looks by us­ing “For­m” -> “Pre­view” in de­sign­er. QGraphicsScene. Introduction This is not re­al­ly part of my PyQt by Ex­am­ple se­ries but since it’s a to­tal­ly un­re­lat­ed top­ic that would be im­pos­si­ble to con­nect to it, but is still a PyQt tu­to­ri­al and fol­lows the same con­cep­t… what­ev­er, here it is.

In this tu­to­ri­al I will ex­plain QGraph­ic­sS­cene (QGS for short) and its re­lat­ed class­es. What are they for, you may ask? Well, the an­swer is: they are for cool stuff. You don’t need QGS for CRUD. You don’t need QGS for of­fice ap­pli­ca­tion­s. You don’t need QGS for bor­ing stuff. How does it work You can think of a QGraph­ic­sS­cene as a stage, filled with ac­tors. An item can be of dif­fer­ent kind­s: A text A shape (like a cir­cle or a poly­gon) A pic­ture A wid­get (yes, any Qt wid­get work­s!) Then, you can have one or more “cam­eras” look­ing at the stage. Keep this in mind, be­cause it’s very im­por­tan­t: One QGraph­ic­sS­cene can con­tain any num­ber of QGraph­ic­sItem­s.

So, hav­ing said that, let’s start do­ing code. PyQt. PyQt is a set of Python v2 and v3 bindings for Digia's Qt application framework and runs on all platforms supported by Qt including Windows, MacOS/X and Linux. PyQt5 supports Qt v5. PyQt4 supports Qt v4 and will build against Qt v5. The bindings are implemented as a set of Python modules and contain over 620 classes. Digia have announced that support for Qt v4 will cease at the end of 2015. PyQt5 and Qt v5 are strongly recommended for all new development. PyQt is dual licensed on all supported platforms under the GNU GPL v3 and the Riverbank Commercial License. PyQt does not include a copy of Qt. PyQt Components A description of the components of PyQt5 can be found in the PyQt5 Reference Guide. A description of the components of PyQt4 can be found in the PyQt4 Reference Guide.

Why PyQt? PyQt brings together the Qt C++ cross-platform application framework and the cross-platform interpreted language Python. Qt is more than a GUI toolkit. PyQt combines all the advantages of Qt and Python. Events and Signals in PyQt4. In this part of the PyQt4 programming tutorial, we will explore events and signals occurring in applications. Events All GUI applications are event-driven. Events are generated mainly by the user of an application. But they can be generated by other means as well: e.g. an Internet connection, a window manager, or a timer.

In the event model, there are three participants: event source event object event target The event source is the object whose state changes. PyQt4 has a unique signal and slot mechanism to deal with events. New API PyQt4.5 introduced a new style API for working with signals and slots. QtCore.QObject.connect(button, QtCore.SIGNAL('clicked()'), self.onClicked) This is the old style API. button.clicked.connect(self.onClicked) The new style adheres more to the Python standards.

Signals & Slots This is a simple example demonstrating signals and slots in PyQt4. #! In our example, we display a QtGui.QLCDNumber and a QtGui.QSlider. Sld.valueChanged.connect(lcd.display) #! Event sender #! #! 6: Qt Reference Documentation - Iceweasel.