Pyqt (2) dialog box

Source: Internet
Author: User
Pyqt (2) dialog box

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

Discuss newsgroups and documents

Summary

A dialog box is a simple gui. It is not only in development, but also in user use. A dialog box is a basic element of a Common graphic interface program, even complex applications often require a dialog box to complete some projects that interact with users. In addition, it is easy to create a dialog box-based program. Because of this, among the many tools developed by the company, apart from the data verification tool that uses the MFC Document view (which belongs to the intensive information output type), all the other tools are displayed in a dialog box. However, it is also powerful with more tab pages.

The simplest Win32 Assembler is a MessageBox, which is a simple form of dialog box. Many useful common dialogs in Windows greatly simplify development. The emergence of the rad tool is almost set for the dialog box. When the drag-and-drop control trend appears, many people begin to imagine that the next step is not to knock on the code to write the program :)

Basically, the difficulty of developing a dialog box program determines the difficulty of using a GUI library to a certain extent. The following describes how to manually create a simple dialog box pyqt program.

The simplest form of dialog box:

Here is like the example quit. pyw in pyqt (1). That example is a simple button ........ In fact, there is no way to suddenly display a button, so QT automatically adds a border for it, the real program should be like this:

Simplestdialog. pyw

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

 

The two programs are similar, but there are some differences after careful consideration.

The default QT dialog box does not have the maximization or minimization button, but is automatically generated for the button. Then, the button occupies the entire user area, and the dialog box does not have the size automatically generated by the button.

 

From process-oriented to object-oriented

So far, all programs in pyqt (1) are still process-oriented. This is probably the form in the pyqt (1) layout. pyw example.

In terms of process-oriented comparison to object-oriented comparison, the original interesting tutorial has a very exemplary comparison. (Reference 3)

For example, process orientation is like this:

1 #! /Usr/bin/ENV Python
2
3 # pyqt tutorial 3
4
5
6 Import sys
7 from pyqt4 import qtcore, qtgui
8
9
10 APP = qtgui. qapplication (SYS. argv)
11
12 window = qtgui. qwidget ()
13 window. Resize (200,120)
14
15 quit = qtgui. qpushbutton ("quit", window)
16 quit. setfont (qtgui. qfont ("Times", 18, qtgui. qfont. Bold ))
17 quit. setgeometry (10, 40,180, 40)
18 qtcore. qobject. Connect (quit, qtcore. Signal ("clicked ()"),
19 app, qtcore. Slot ("Quit ()"))
20
21 window. Show ()
22 sys.exit(app.exe C _())

 

This is the object-oriented version of the same program:

 

1 #! /Usr/bin/ENV Python
2
3 # pyqt tutorial 4
4
5
6 Import sys
7 from pyqt4 import qtcore, qtgui
8
9
10ClassMywidget (qtgui. qwidget ):
11Def_ Init _ (self, parent = none ):
12 qtgui. qwidget. _ init _ (self, parent)
13
14 self. setfixedsize (200,120)
15
16 self. Quit = qtgui. qpushbutton ("quit", self)
17 self. Quit. setgeometry (62, 40, 75, 30)
18 self. Quit. setfont (qtgui. qfont ("Times", 18, qtgui. qfont. Bold ))
19
20 self. Connect (self. Quit, qtcore. Signal ("clicked ()"),
21 qtgui. qapp, qtcore. Slot ("Quit ()"))
22
23
24 APP = qtgui. qapplication (SYS. argv)
25 widget = mywidget ()
26 widget. Show ()
27 sys.exit(app.exe C _())

 

The above are two almost identical programs except implementation methods.

Implementation:

The process-oriented approach is like generating a default widget, then modifying the widget by calling the function, and then displaying it through the creation of the Child widget button.

The general idea of the object-oriented approach is to first describe the widget we want with a child widget button. The size is ..... Create a widget that we described and display it.

Some people say that the way you program depends on the way you see the world -_-! In fact, this is suitable for process-oriented and object-oriented applications.

As for the benefits of process-oriented and object-oriented, it is far beyond the scope of this article. However, even in such a simple GUI program, without discussing encapsulation, data hiding, and other things, it can be concluded that the object-oriented version is easier to reuse. Although n many books emphasize that the main reason for using object-oriented technology is not to improve the code reuse rate, it is indeed useful and easier to explain through comparison. For example, if we need two similar dialogs to display different titles and different positions at the same time.

The object-oriented version can be implemented as follows:

1 #! /Usr/bin/ENV Python
2 # pyqt tutorial 4 ext objwindow
3
4 Import sys
5 from pyqt4 import qtcore, qtgui
6
7
8ClassMywidget (qtgui. qwidget ):
9Def_ Init _ (self, parent = none ):
10 qtgui. qwidget. _ init _ (self, parent)
11
12 self. setfixedsize (200,120)
13
14 self. Quit = qtgui. qpushbutton ("quit", self)
15 self. Quit. setgeometry (62, 40, 75, 30)
16 self. Quit. setfont (qtgui. qfont ("Times", 18, qtgui. qfont. Bold ))
17
18 self. Connect (self. Quit, qtcore. Signal ("clicked ()"),
19 qtgui. qapp, qtcore. Slot ("Quit ()"))
20
21
22 APP = qtgui. qapplication (SYS. argv)
23 widget = mywidget ()
24 widget. Move)
25 widget. setwindowtitle ("a widget ")
26 widget. Show ()
27 anotherwidget = mywidget ()
28 anotherwidget. Move (50, 50)
29 anotherwidget. setwindowtitle ("another widget ")
30 anotherwidget. Show ()
31 sys.exit(app.exe C _())

 

Try to imagine what the process-oriented version will look like? I can only copy all the code once again. I don't even want to give an example of this mechanical operation. Programmers should hate copying!

In fact, this object-oriented version of the program has some problems, because the original program mixed the dialog box with the application, so the connection button to the qapp exit slot, in this way, when you click a dialog box, both the dialog boxes are closed. A more object-oriented approach is to connect it with the dialog box. Change the line of code:

Self. Connect (self. Quit, qtcore. Signal ("clicked ()"),

Self, qtcore. Slot ("close ()"))

In this way, the two dialogs can be controlled separately, in this simple demonstration example, ququxing's tutorials are not considered so much, so this will happen.

 

Object-oriented dialog box

In the preceding example, the widget is inherited by the qwidget, which is a common widget. If it is inherited from the dialog, It is a dialog, in this case, there is a difference between the two.

For example, change the preceding example to a dialog box.

1 import sys
2 from pyqt4 import qtcore, qtgui
3
4
5ClassMydialog (qtgui. qdialog ):
6Def_ Init _ (self, parent = none ):
7 qtgui. qdialog. _ init _ (self, parent)
8
9 self. setfixedsize (200,120)
10
11 self. Quit = qtgui. qpushbutton ("quit", self)
12 self. Quit. setgeometry (62, 40, 75, 30)
13 self. Quit. setfont (qtgui. qfont ("Times", 18, qtgui. qfont. Bold ))
14
15 self. Connect (self. Quit, qtcore. Signal ("clicked ()"),
16 self, qtcore. Slot ("close ()"))
17
18
19 APP = qtgui. qapplication (SYS. argv)
20 dialog = mydialog ()
21 dialog. Show ()
22 sys.exit(app.exe C _())

 

The display effect is similar to the previous one:

 

Here I didn't describe which is a dialog box, and that is a widget. However, the comparison above is quite obvious. It is worth noting that the differences between the dialog box and common widgets in QT and the Dialog Box and document view in MFC are much smaller. In the above example, we can basically determine the differences, when we only want to display a button, QT actually generates a widget for us by default.

 

More complex dialog box

Chapter 2nd of "C ++ GUI qt4 programming (version 2)" implements a complicated Dialog Box program. I tried its c ++ version with QT creater, A lot of code is required, so I lost the meaning of re-coding to implement the pyqt version ....... Haha, but I actually know the general process and meaning. It is very easy to see the C ++ version of the QT program and then implement a pyqt version of the program.

However, as a learning tool, I still need to implement a simplified pyqt Dialog Box program. Otherwise, I do not know what the private slots, q_object, and TR correspond to in pyqt.

Complicatedialog. pyw

1 import sys
2 from pyqt4 import qtcore, qtgui
3
4ClassMydialog (qtgui. qdialog ):
5Def_ Init _ (self, parent = none ):
6 qtgui. qdialog. _ init _ (self, parent)
7
8 self. Quit = qtgui. qpushbutton ("quit ")
9
10 self. Change = qtgui. qpushbutton ("change ")
11 self. Change. setenabled (false)
12
13 # Funny widget
14 self. LCD = qtgui. qlcdnumber (2)
15
16 self. Slider = qtgui. qslider (qtcore. QT. Horizontal)
17 self. Slider. setrange (0, 99)
18 self. Slider. setvalue (0)
19
20 self. lineedit = qtgui. qlineedit ()
21
22 self. Connect (self. Quit, qtcore. Signal ("clicked ()"),
23 qtgui. qapp, qtcore. Slot ("Quit ()"))
24 self. Connect (self. lineedit, qtcore. Signal ("textchanged (const qstring &)"),
25 self. enablechangebutton)
26 self. Connect (self. Slider, qtcore. Signal ("valuechanged (INT )"),
27 self. sliderchange)
28 self. Connect (self. Change, qtcore. Signal ("clicked ()"),
29 self. Change)
30
31 self. rightlayout = qtgui. qvboxlayout ()
32 self. rightlayout. addwidget (self. lineedit)
33 self. rightlayout. addwidget (self. Change)
34
35 self. leftlayout = qtgui. qvboxlayout ()
36 self. leftlayout. addwidget (self. LCD)
37 self. leftlayout. addwidget (self. Slider)
38
39 self. layout = qtgui. qhboxlayout ()
40 self. layout. addwidget (self. Quit)
41 self. layout. addlayout (self. leftlayout)
42 self. layout. addlayout (self. rightlayout)
43
44 self. setlayout (self. layout );
45
46DefEnablechangebutton (self, text ):
47 self. Change. setenabled (text. isempty () = false)
48
49DefChange (Self ):
50 value = int (self. lineedit. Text ())
51 self. LCD. Display (value)
52 self. Slider. setvalue (value)
53
54DefSliderchange (Self ):
55 value = self. Slider. Value ()
56 self. LCD. Display (value)
57 self. lineedit. settext (STR (value ))
58
59 APP = qtgui. qapplication (SYS. argv)
60 dialog = mydialog ()
61 dialog. Show ()
62 sys.exit(app.exe C _())

 

 

What did I say? A simplified version? -_-! Finally, I wrote that in order to test the new features, the result is that the complex version is not enough.

Effect

This program demonstrates several QT features:

  1. The numbers of LCD results are very interesting. This effect is actually native in QT widgets.
  2. The interaction between the four widgets is demonstrated, including LCD, Slider, change, and lineedit. Three of them are controllable and the LCD displays passively. Each change is triggered, can be reflected in the other three (two. The control method is to connect a singal to a method of the dialog box class. Among them, I found that the connect message method of pyqt is funny. Why? In python, a string like "textchanged (const qstring &)"-_-! appears -_-! What is even more powerful is implemented through C ++... Well, it is estimated that pyqt users without the C ++ background will feel confused about this syntax ...... I am also surprised, why not simply change the singal string to the python syntax at one time? Haha, maybe the workload will be larger. Of course, the slot response method is simplified. At that time, I was still wondering in what form to express the slot parameters with strings .... The result shows that no representation is required .... As shown in the source code. In this article, we will discuss in detail: connecting with signals and slots.

 

  1. Layout nesting implements complex dialog, and in the code, I do not actually control them through any coordinate, they are automatically placed where I want them to be, at this point, QT is more friendly than MFC when writing code manually.

 

However, there are still some problems with the above Code. For example, the lineedit widget can input any form of characters, not just numbers, which should not be allowed in our example, in QT, it also provides a method that is far better than MFC. You don't have to inherit and implement your own lineedit widget to implement very complex restrictions, this is a bit similar to General Algorithm thinking in STL. This is the validator in QT. It is powerful and you can use regular expressions to limit lineedit .... Haha, powerful... Here I use qintvalidator as needed.

In the preceding example, add the following code:

Intvalidator = qtgui. qintvalidator (0, 99, self)

 

Self. lineedit = qtgui. qlineedit ()

Self. lineedit. setvalidator (intvalidator)

 

I don't need to teach you the position, that is, to define the upper and lower positions of self. lineedit. Try again. Except for the integer 0 and 99, nothing can be entered.

 

Digress:

Before using designer, I decided to first read some information about pyqt (I used to read reference1. Simply switching from C ++ to python in this way will inevitably lead to some problems through your own understanding, although the above programs can be written as well :). However, as far as my experience is concerned, I will not clarify the principle of manual code before using a graphical design tool, and I may not want to go back to it to find it completely in the future, (because the graphic design is much simpler), but when it comes to a few exceptions, it will be helpless (the experience of using MFC ).

 

Reference
  1. C ++ GUI qt4 programming (second edition) C ++ GUI programming with qt4, Second Edition. Jasmin Blanchette [Canada], Mark Summerfield (English), Yan fengxin, Zeng quanren, Zhang Zhiqiang, Electronic Industry Press.
  2. Gui programming with Python: QT Edition
  3. Qt tutorial
  4. Pyqt GPL v4.4.4 for python v2.6 examples from riverbankcomputing

 

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.