In this novel,I’ll demonstrate how to read the database and display it , with Python’s dynamic mechanism and Qt’s signal slot.
This is a program that can detect network connections. The screenshot of the interface is as follows
There will be very few opportunities to write the client in the future(I mean many professional clients are mainly developed by C#/.net,although actually dropbox developed its’ client with PyQT)… So I won’t pursue the best practices. I just need to make a poke-marked GUI .⊙﹏⊙b
UI and interface corresponding
PyQt itself is similar to Qt’s signal slot mechanism. When a button is clicked, a signal is sent and the processing mechanism of the signal needs to be defined.
The design interface is designed directly with the designer. Drag
button/listview/model and customize the name.
After generating the .ui file, use
pyuic4 –x ping_ui.ui –o ping_ui.py to see that the ping_ui.py file contains
class Ui_Form(object), which has various lengths/locations defined. It’s More convenient than handwriting
Then define a
pyqt_example2.py which contains the following files:
then define the execution function：
After execution, you can see that the interface has been successfully displayed.
The next step is to establish a signal slot mechanism. For example, when the button of the ui was originally defined, there is a button named
add_url . We want to associate it with the function of
add_url , just define it as follows (preferably in init ):
CRUD and Display
The model defined in PyQt is
QSqlTableModel, and the definition of table is
For example, define the method of initialize_model() :
To display, just connect the listview with the corresponding model:
You can see the latest database, equivalent to refreshing, by calling the self.show_view() method after each addition, deletion, and change.
How to execute a query
For example, to find the content containing ‘test’, the code is as follows:
And if you want to show with a better way,then redefine QSqlQueryModel
Define the popup: Use the simplest of the following functions:
Ui and execution thread separation
How to set the ui and execute task thread separation so that the application won’t be non-reactive:
There are many ways to do this. I chose the following method:
The progress bar in PyQt is displayed using QProgressBar. When set in ui, there will be a minimum value (default is 0) and a maximum value (default is 100).
Just set it when updating
The ping calls the ping API that comes with the system.
Tracert calls the tracert API in windows (the corresponding Linux command is traceroute)
Telnet is to detect if the port is open. Directly calling the system command has two drawbacks:
1 Windows requires the user to manually open the configuration
2 telnet uses the cursor to flash to determine the port status with no return value, it is not convenient for program processing.
So final decision is to use socket to connect:
PyQt comes with a lot of demos in site-packages, and it’s faster to see the code directly than the documentation. At the time of the study, I wrote a calculator demo that adds genuine. When the input number or the selected calculation symbol changes, the calculation result is automatically changed (well, eval()~ and the Python decorator is used. characteristic.