Introduction to the PYQT5 GUI Application Toolkit (3)-Signal slots

Source: Internet
Author: User

In the last lecture, we developed a simple login dialog box. The Real Login dialog box needs to send the user name and password that you entered for processing. PYQT communicates between objects through signal (signal) and slot (slot) mechanisms. The specific method is: by an event (such as: click the control) to trigger the signal, the slot function receives the signal automatically executes. Definition of built-in signal slots in Qtdesigner


Fig. 1 The definition of the signal slot in Qtdesigner

In Figure 1, the Blue Arrow window shows the existing signal slot, so there is no definition of any signal slot in the window because we haven't edited any signal slots yet.
Click on the button indicated by the red arrow in Figure 1 to enter the signal slot editing mode. You can define a signal slot.

Figure 2 Defining a signal slot

After entering the signal slot editing mode, drag the mouse from the window to the blank position (shown in Figure 2), releasing the mouse, which is the pop-up signal Slot Configuration Connection dialog box (shown in Figure 3).

Figure 3 Signal Slot Configuration Connection dialog box

Displays all signals and slots by selecting the "Show signals and slots that are inherited from Qwidget". Here we want the "cancel" key to press when the window closes. So the signal on the left chooses "clicked ()", the right slot selects "Close ()", and click OK to define the "Cancel" button signal slot. and is displayed in the signal/slot editor (shown in Figure 4).

Fig. 4 Effect of signal slot configuration

Of course, we can also click the + number in the signal/slot editor to add a signal slot directly select the Send control, signal function, receive control, and slot function. Defining a custom signal slot

In this case, click OK and the slot function needs to send the username and password, so you can only use a custom slot function. Enter the signal slot editing mode as in the previous section, and drag to create a slot function. Then click Edit in the Signal Slot Configuration connection dialog shown in Figure 3 to enter the custom Signal Slot dialog box as shown in Figure 5. Click the + number to add a slot function. Then, after the build, define the corresponding function in your code.


Figure 5 Custom Signal Slot dialog box Handwriting definition slot function

The handwriting definition slot function is very simple, and the syntax is:

Sender control. Signal function. Connect (slot function)

Example (the signal slot function of the cancellation key):

Self.pushButton_2.clicked.connect (Form.close)

When writing a custom function, simply define a custom function in the Ui_form class. We will expand the dialog box in the previous section as follows:

#-*-Coding:utf-8-*-

From pyqt5.qtwidgets import Qwidget,qformlayout,qlabel,qlineedit,qpushbutton

Class Ui_form (Qwidget):

def __init__ (self):

Super (). __INIT__ ()

Self.setwindowtitle ("Login Interface")

Self.resize (200,100)

Lay=qformlayout ()

Lab1=qlabel ("User name")

Self. Line1=qlineedit ()

Lay.addrow (lab1,self. LINE1)

LAB2 = Qlabel ("password")

Self. Line2 = Qlineedit ()

Lay.addrow (LAB2, self. LINE2)

Okb=qpushbutton ("OK")

Cb=qpushbutton ("Cancel")

Lay.addrow (OKB,CB)

Self.setlayout (Lay)

CB.clicked.connect (Lambda:self.close ())

OkB.clicked.connect (lambda:self. Senduserinf ())

def senduserinf (self):

Print (user name:, self. Line1.text ())

Print ("Password:", self. Line2.text ())

Code Description: Line 11th and 14, add "self." to the two text boxes to make it a class variable so that the text can be sent out. Line 21st and 22, define the signal slots for the two buttons. Line 24th to 26th, defines the event that the OK key clicks. The function is wrapped with "lambda:" or there will be an error.

Commend the author


The author of this article

Stream

Master of Civil Engineering, the master period using Python data processing and abaqus two development, is now working in a civil engineering consulting company in Shanghai, engaged in civil engineering software development.

Click to read the original, join the Codinggo programming community, more reading please click:

Introduction to the PYQT5 GUI Application Toolkit (1)

Introduction to the PYQT5 GUI Application Toolkit (2)

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.