This example demonstrates how to use the standard input box. QT provides a qinputdialog class. The qinputdialog class provides a simple dialog box to obtain a single user input, currently, four data types are provided, including a string, an int, a double, or a drop-down list. This includes a prompt label and an input control. If the string input box is called, it is a qlineedit. If the Int or double type is called, it is a qspinbox. If the List entry input box is called, it is a qcombobox, it also includes an OK button and a Cancel button.
The implementation result of this instance is as follows.
The instance lists the above four input types. the right button is used to modify the values of each item in the pop-up standard input dialog box. The specific implementation code is as follows:
#-*-Coding: UTF-8-*-<br/> from pyqt4.qtcore import * <br/> from pyqt4.qtgui import * <br/> Import sys </P> <p> qtextcodec. setcodecfortr (qtextcodec. codecforname ("utf8") </P> <p> class inputdlg (qdialog): <br/> def _ init _ (self, parent = none ): <br/> super (inputdlg, self ). _ init _ (parent) </P> <p> label1 = qlabel (self. TR ("name") <br/> label2 = qlabel (self. TR ("gender") <br/> label3 = qlabel (self. TR ("Age") <br/> label4 = qlabel (self. TR ("height") </P> <p> self. namelabel = qlabel ("tengwei") <br/> self. namelabel. setframestyle (qframe. panel | qframe. sunken) <br/> self. sexlabel = qlabel (self. TR ("male") <br/> self. sexlabel. setframestyle (qframe. panel | qframe. sunken) <br/> self. agelabel = qlabel ("25") <br/> self. agelabel. setframestyle (qframe. panel | qframe. sunken) <br/> self. staturelabel = qlabel ("168") <br/> self. staturelabel. setframestyle (qframe. panel | qframe. sunken) </P> <p> namebutton = qpushbutton ("... ") <br/> sexbutton = qpushbutton ("... ") <br/> agebutton = qpushbutton ("... ") <br/> staturebutton = qpushbutton ("... ") </P> <p> self. connect (namebutton, signal ("clicked ()"), self. slotname) <br/> self. connect (sexbutton, signal ("clicked ()"), self. slotsex) <br/> self. connect (agebutton, signal ("clicked ()"), self. slotage) <br/> self. connect (staturebutton, signal ("clicked ()"), self. slotstature) </P> <p> layout = qgridlayout () <br/> layout. addwidget (label1, 0, 0) <br/> layout. addwidget (self. namelabel, 0, 1) <br/> layout. addwidget (namebutton, 0, 2) <br/> layout. addwidget (label2, 1, 0) <br/> layout. addwidget (self. sexlabel, 1, 1) <br/> layout. addwidget (sexbutton, 1, 2) <br/> layout. addwidget (label3, 2, 0) <br/> layout. addwidget (self. agelabel, 2, 1) <br/> layout. addwidget (agebutton, 2, 2) <br/> layout. addwidget (label4, 3, 0) <br/> layout. addwidget (self. staturelabel, 3, 1) <br/> layout. addwidget (staturebutton, 3, 2) </P> <p> self. setlayout (layout) </P> <p> self. setwindowtitle (self. TR ("Data Collection") </P> <p> def slotname (Self): <br/> name, OK = qinputdialog. gettext (self, self. TR ("username"), <br/> self. TR ("enter a new name:"), <br/> qlineedit. normal, self. namelabel. text () <br/> If OK and (not name. isempty (): <br/> self. namelabel. settext (name) </P> <p> def slotsex (Self): <br/> List = qstringlist () <br/> list. append (self. TR ("male") <br/> list. append (self. TR ("female") <br/> sex, OK = qinputdialog. getitem (self, self. TR ("gender"), self. TR ("select gender"), list) </P> <p> If OK: <br/> self. sexlabel. settext (sex) </P> <p> def slotage (Self): <br/> Age, OK = qinputdialog. getinteger (self, self. TR ("Age"), <br/> self. TR ("Enter age:"), <br/> int (self. agelabel. text (), 0,150) <br/> If OK: <br/> self. agelabel. settext (STR (AGE) </P> <p> def slotstature (Self): <br/> stature, OK = qinputdialog. getdouble (self, self. TR ("height"), <br/> self. TR ("Enter Height:"), <br/> float (self. staturelabel. text (), 0, 2300.00) <br/> If OK: <br/> self. staturelabel. settext (STR (stature) </P> <p> APP = qapplication (sys. argv) <br/> form = inputdlg () <br/> form. show () <br/> app.exe C _()
The constructor declares the controls used in the dialog box and the slot functions triggered by each button.
Click the modify button after the name to trigger the slotname () function. The standard string input dialog box is displayed, as shown in.
Call the gettext () function of qinputdialog to bring up the standard string input dialog box. The prototype of the gettext () function is as follows:
(Qstring, bool OK) qinputdialog. gettext (qwidget, qstring, qstring, qlineedit. echomode mode = qlineedit. Normal, qstring text = qstring (), QT. windowflags flags = 0)
The first parameter of this function is the parent window of the standard input dialog box, the second parameter is the title name of the standard input dialog box, and the third parameter is the tag prompt of the standard input dialog box, the fourth parameter mode specifies the input mode of the qlineedit control in the standard input dialog box. The fifth parameter text is the default text of the qlineedit control displayed in the standard string input dialog box, the last parameter specifies the Form ID of the standard input dialog box.
Row 3rd in the slotname () function determines the OK value. If the user clicks the "OK" button, the new name is updated to the display label.
Click the modify button after gender to trigger the slotsex () function. The standard entry selection dialog box is displayed, as shown in.
Row 1, 2, and 3 create a qstringlist object, which includes two qstring items for display of entries in the drop-down list box in the standard input dialog box.
The standard entry selection dialog box is displayed when the getitem () function of qinputdialog is called in Row 3. The getitem () function prototype is as follows:
(Qstring, bool OK) getitem (qwidget, qstring, qstring, qstringlist, int current = 0, bool editable = true, QT. windowflags flags = 0)
The first parameter of this function is the parent window of the standard input dialog box, the second parameter is the title name of the standard input dialog box, and the third parameter is the tag prompt of the standard input dialog box, the fourth parameter specifies the optional entries displayed by the qcombobox control in the standard input dialog box, which is a qstringlist object. The fifth parameter current is the default entry number displayed in the qcombobox control in the standard entry selection dialog box, the sixth parameter editable specifies whether the text displayed in the qcombobox control can be edited. The last parameter specifies the Form ID of the standard input dialog box.
Row 3 determines the OK value. If the user clicks the OK button, the newly entered gender is updated to the display label.
Click the modify button after age to trigger the slotage () function. The standard int type input dialog box is displayed, as shown in.
Call the getinteger () function of qinputdialog to bring up the standard int type input dialog box. The prototype of the getinteger () function is as follows:
(INT, bool OK) getinteger (qwidget, qstring, qstring, int value = 0, int min =-2147483647, int max = 2147483647, int step = 1, QT. windowflags flags = 0)
The first parameter of this function is the parent window of the standard input dialog box, the second parameter is the title name of the standard input dialog box, and the third parameter is the tag prompt of the standard input dialog box, the fourth parameter value specifies the default value of the qspinbox control in the standard input dialog box. The fifth six Parameters specify the value range of the qspinbox control, and the seventh parameter step specifies the step value of the qspinbox control.
Row 3 determines the OK value. If the user clicks the OK button, the new age value is updated to the display label.
Click the modify button after height to trigger the slotstature () function. The standard double type input dialog box is displayed, as shown in.
Call the getdouble function of qinputdialog to bring up the standard double type input dialog box. The prototype of the getdouble () function is as follows:
(Float, bool OK) getdouble (qwidget, qstring, qstring, float value = 0, float min =-2147483647, float max = 2147483647, int decimals = 1, QT. windowflags flags = 0)
The first parameter of this function is the parent window of the standard input dialog box, the second parameter is the title name of the standard input dialog box, and the third parameter is the tag prompt of the standard input dialog box, the fourth parameter value specifies the default value displayed by the qspinbox control in the standard input dialog box. The fifth six Parameters specify the value range of the qspinbox control, and the seventh parameter decimals specifies the step value of the qspinbox control.
Row 3 determines the value of OK. If the user clicks the "OK" button, the new height value is updated to the display label.
Note: This article is based on pyqt4 for windows, a book adapted from "Linux Window Programming-qt4 wonderful instance analysis ".