- Design QtDesigner graphical interface.
- code generation in Python pyuic4 with the tool.
- Create a class to contain object with the window and manage their events, as well as entry point (main) for our program.
But what is this? Let the Wikipedia enlighten us, because all is said and life is short, and now, in the absence of the corresponding article, I can tell you QtDesigner is an application for designing graphical interfaces Qt, storing the description and layout of components in an XML file for later use from multiple programming languages.
Requirements.
The system used in the development and testing of this example is Debian Sid, for I will mention the name of the distribution's own package. However, the tools used are cross-platform.
- python (we use version 2.4 onwards).
- The Qt4 libraries, which will automatically install dependencies to python-qt4.
- python-qt4 (the interface between the Qt4 libraries, programmed in C + +, and the language we will be using).
- qt4-designer.
- PyQt4-dev-tools (this package provides the command pyuic4).
Introduction to Python.
Python is very easy to learn, so even if we have not used ever, with a little reading and practice can begin with our first programs, a basic, but functional.
I recommend you read this tutorial to a quick start. To easily understand the example on this post, you must know the structure of a program in Python, data types, functions, and to a lesser extent, inheritance, modules and packages.
Moving on.
Our example is based on this material , still highly recommended reading, and from the slide 41 begins to use QtDesigner. They should be something like:
The buttons for the four operations we have the names: sum, res, mul and div, on the other hand, the text fields will be named num1, num2, and result, and finally to the window we called calc. Well, we will keep this form with the name calc.ui and now comes the magic: with the command pyuic4 take that file. Ui and generate the code in Python. The command is:
The result is illustrated below:
calc_ui.py
Noten QtDesigner that we have done all the dirty work, have modified added more properties or more components, write, test and maintain the code manually would be an unwelcome activity.
Here's an important point to treat, and every time we change our interface (. Ui) we have to regenerate the code in Python, so it is not advisable to make changes to the latter, as they will be overwritten without mercy .
I recommend you read this tutorial to a quick start. To easily understand the example on this post, you must know the structure of a program in Python, data types, functions, and to a lesser extent, inheritance, modules and packages.
Moving on.
Our example is based on this material , still highly recommended reading, and from the slide 41 begins to use QtDesigner. They should be something like:
The buttons for the four operations we have the names: sum, res, mul and div, on the other hand, the text fields will be named num1, num2, and result, and finally to the window we called calc. Well, we will keep this form with the name calc.ui and now comes the magic: with the command pyuic4 take that file. Ui and generate the code in Python. The command is: calc.ui-o calc_ui.py pyuic4
The result is illustrated below:
calc_ui.py
Noten QtDesigner that we have done all the dirty work, have modified added more properties or more components, write, test and maintain the code manually would be an unwelcome activity.
Here's an important point to treat, and every time we change our interface (. Ui) we have to regenerate the code in Python, so it is not advisable to make changes to the latter, as they will be overwritten without mercy .
To supplement the form with the code for the events and some other actions at runtime, we can write a new class and then create a class based object originated from QtDesigner, or that this new class inherits from the first. On this occasion we will Finally.
calc.py
One difference we see between the code above and shown on slide 57 of presentation, is that in our class have not controlled event clicked () on the exit button . Instead, we defined a handler for this event from the same QtDesigner, redirecting the method close () of the form. In slide 44 we can see where is the editor of signals. Here is a screenshot of this tool:
This what we do because close () is part of the class QWidget, and is what is called a slot , which is nothing more than a method designed to receive signals (events) and a defined behavior. Do not forget that since we can not add code QtDesigner, so our methods to respond to clicks on the buttons have to be implemented and "plugged in" externally. Commenting
more about the code, lines 13 through 16 we see how we link to a specific signal buttons, with four different methods located in the same class (line 18 to 28). The block from line 30 onwards is outside the definition of the class, and will be executed when the module is invoked as an entry point for the application.
To run the program: python
Well, that we and our powerful calculator running. In a later entry we see the changes or problems that arise in doing this in windows, and more important things, we will use another method of communication between QtDesigner and Python, which is charged directly to the. Ui at runtime, ie without pre-generate the code with pyuic4.
links and attachments.
Python for all
Presentation
PyQt PyQt-calculadora.zip
calc.py
One difference we see between the code above and shown on slide 57 of presentation, is that in our class have not controlled event clicked () on the exit button . Instead, we defined a handler for this event from the same QtDesigner, redirecting the method close () of the form. In slide 44 we can see where is the editor of signals. Here is a screenshot of this tool:
more about the code, lines 13 through 16 we see how we link to a specific signal buttons, with four different methods located in the same class (line 18 to 28). The block from line 30 onwards is outside the definition of the class, and will be executed when the module is invoked as an entry point for the application.
To run the program: python
calc.py
Well, that we and our powerful calculator running. In a later entry we see the changes or problems that arise in doing this in windows, and more important things, we will use another method of communication between QtDesigner and Python, which is charged directly to the. Ui at runtime, ie without pre-generate the code with pyuic4.
links and attachments.
Python for all
Presentation
PyQt PyQt-calculadora.zip
0 comments:
Post a Comment