Pyqt Learning (1) Quick Start

Source: Internet
Author: User
Pyqt Learning (1) Quick Start

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

Discuss newsgroups and documents

 

Motivation:

After learning python, I learned from my previous experience in C ++. At that time, I used MFC to consolidate C ++ learning and make practical programs, it gave me the motivation to continue learning, and I learned QT for a similar purpose.

Why do I need to write to learn. Not only will there be a usable, easy-to-use cross-platform GUI library in the future, but also a GUI library for rapid development using python, but does not like TK, so, in a comprehensive consideration, QT is a good consideration.

 

Summary

These articles basically follow the C ++ gui qt 4 programming version 2nd (C ++ GUI programming with qt4, Second Edition) process, the text may be very small, unless you come up with an example, the example is the Python version of the C ++ example in the book,Basically, we can use this book as a blueprint to compare the examples in these articles.

Although the usage is not too big, considering that pyqt's good teaching materials are so scarce, This is not completely worthless. After all, there are people who only understand Python and do not understand C ++.

Here is a pyqt book GUI programming with Python: QT edition, but it is basically a thing of the qt2 era.

As for pyqt, you don't know how to search for it on Google, right? The installation method is so simple. After clicking Next, you can use this library in Python by using import pyqt4.

 

Helloworld

Since K & R, it seems that all program languages are started with helloworld. We are not vulgar. We started with helloqt. In addition, this example can also be used as a test program to test whether the installation of pyqt is successful.

Helloqt. pyw

1 import sys
2 from pyqt4 import qtgui
3
4
5 APP = qtgui. qapplication (SYS. argv)
6 label = qtgui. qlabel ("Hello QT! ")
7 label. Show ()
8
9 sys.exit(app.exe C _())

 

In this way, it is a real "Hello QT" window, using the qlabel component. -- In QT, the things that are often called Control in MFC are called widgets.

Here we can also see that QT is concise enough. I really appreciate it, and its object style and method are also good. Otherwise, try to generate a similar MFC program using? ^

In fact, this is not enough to witness the strength of QT. In the following example, qlabel is actually the HTML style ..... -_-!

Morehelloqt. pyw

1 import sys
2 from pyqt4 import qtgui
3
4
5 APP = qtgui. qapplication (SYS. argv)
6 label = qtgui. qlabel ("
HelloQt!

") # Here I cannot make csdn not to interpret it as HTML... So refer to the source code of the interface to see what it is.
7 label. Show ()
8
9 sys.exit(app.exe C _())

 

Powerful :)

 

Qt (C ++) vs pyqt

Here, we can compare the differences between pyqt and general QT (C ++) generation programs. In general, the speed of the two is not comparable, but speed is not a major problem here, because the core of pyqt is the QT library, which is written in C ++, in general, it does not take too much time for the logic code to speed down, it does not become a bottleneck, so that pyqt programs can run fast enough. However, in terms of usage, we have not lost the elegant syntax and fast development capabilities of Python. It also integrates the strength of QT, advertisement Language ........... To be practical.


On the left is the QT program developed with C ++, and on the right is the program developed by pyqt. Because both use the same library, there is no difference between the two. Basically, if you understand more than one language, you have more than one choice... For example, python at this time.

 

Establish a connection

Quit. pyw

1 import sys
2 from pyqt4 import qtcore, qtgui
3
4 APP = qtgui. qapplication (SYS. argv)
5
6 quit = qtgui. qpushbutton ("quit ")
7
8 qtcore. qobject. Connect (quit, qtcore. Signal ("clicked ()"),
9 app, qtcore. Slot ("Quit ()"))
10
11 quit. Show ()
12 sys.exit(app.exe C _())

 

Here we show the QT message slot mode. It is a bit strange that in Python, signals and slots are represented by strings -_-! This seems a bit strange.

I still don't understand the principles of QT, and I haven't read the source code of QT, but I always feel strange here, so I looked at the signal and slot macros of QT, so everything is not that strange.

# Define slot (a) qflaglocation ("1" # A qlocation)

# Define signal (a) qflaglocation ("2" # A qlocation)

They are represented by strings ........ I originally thought that in QT, these are all in the form of callback functions ....... Khan -_-! What is the difference between the wxwidget principle and the general wxwidget principle... It seems that the principles of QT have to be well understood. In fact, the current information is nothing more than an extension of the observer mode.

 

In the pyqt installation package, there is a tutorial that shows more complex button usage methods. For details, refer

Morequit. pyw

1 import sys
2 from pyqt4 import qtcore, qtgui
3
4
5 APP = qtgui. qapplication (SYS. argv)
6
7 quit = qtgui. qpushbutton ("quit ")
8 quit. Resize (75, 30)
9 quit. setfont (qtgui. qfont ("Times", 18, qtgui. qfont. Bold ))
10
11 qtcore. qobject. Connect (quit, qtcore. Signal ("clicked ()"),
12 app, qtcore. Slot ("Quit ()"))
13
14 quit. Show ()
15 sys.exit(app.exe C _())

 

Set the size of the button and the font and style of the button text respectively. At this stage, we will not have to go into details, just understand the concept and the approximate style of QT.

 

Widget Layout

Anyone who has used MFC knows how difficult it is to create a program in MFC that dynamically changes the control size with the window ...... We need to re-calculate the widget size every onmove and onsize. In order to maintain a reasonable layout, we 'd better calculate the positions of all controls by percentage. The pain is self-evident, for more information, see the regular expression test program version 0.3 (or the older version) I wrote ). However, in QT, it seems to be much simpler. Take a look at the example:

Layout. pyw

1 import sys
2 from pyqt4 import qtcore, qtgui
3
4 APP = qtgui. qapplication (SYS. argv)
5
6 window = qtgui. qwidget ()
7
8 spinbox = qtgui. qspinbox ()
9 slider = qtgui. qslider (qtcore. QT. Horizontal)
10 spinbox. setrange (0,130)
11 slider. setrange (0,130)
12
13 qtcore. qobject. Connect (spinbox, qtcore. Signal ("valuechanged (INT )"),
14 slider, qtcore. Slot ("setvalue (INT )"))
15 qtcore. qobject. Connect (slider, qtcore. Signal ("valuechanged (INT )"),
16 spinbox, qtcore. Slot ("setvalue (INT )"))
17 spinbox. setvalue (35)
18
19 layout = qtgui. qhboxlayout ()
20 layout. addwidget (spinbox)
21 layout. addwidget (slider)
22 window. setlayout (layout)
23
24 window. Show ()
25 sys.exit(app.exe C _())

 

Okay. Basically, how does the pyqt program look like? I 'd like to know about it. :). The next step is coming. I also need to go to bed.

 

 

 

Write by nine days Yan Ling (jtianling) -- blog.csdn.net/vagrxie

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.