1. Simple login verification and simple login verification

Source: Internet
Author: User
Tags qt designer

1. Simple login verification and simple login verification

Create a Qt Widgets Application project named login, which generates a GUI project. As shown in,

Add a new Qt designer interface class. Select Dialog without Buttons as the template and name it LoginDialog. For example:

Drag two labels, two Line Edit controls, and two Push Button controls to the interface. The design interface is shown in:

Select the Line Edit control after the user name, change the objectName attribute to userLineEdit in the property Editor, change the password to pwdLineEdit, log on to loginBtn, and exit to exitBtn. For example:

Then, set the Line Edit echoModel attribute next to the Password to Password, so that the input box does not display plain text, but the reality is small. Then, change the placeholderText attribute to "Enter Password", and change the placeholderText attribute of Line Edit after the user name to "enter user name ". The palceholderText attribute is a watermark (placeholder). You can set some prompts as follows:

Select the login button, right-click to go To the slot, and select clicked (), as shown below:

The slot modification function is as follows:

Void LoginDialog: on_loginBtn_clicked () {// if the user name and password are correct, the accept () function is called. The accept function in QDialog closes itself and sends the qdidit: accepted ID if (ui-> userlineEdit-> text () = "username" & ui-> pwdlineEdit-> text () = "123456") accept (); else {QMessageBox: warning (this, "warning ", "The user name or password is incorrect! ", QMessageBox: Yes); // clear the content and locate the cursor ui-> userlineEdit-> clear (); ui-> pwdlineEdit-> clear (); ui-> userlineEdit-> setFocus ();}}

Double-click logindialog. the ui file enters the design mode, click the button, and then left-click the exit button on the interface to drag it, connect to the logindialog interface, select the clicked () signal, and select close () as the slot (), in this way, the program will be exited by pressing the exit button.

Finally, in main. cpp, change the main function as follows:

int main(int argc, char *argv[]){    QApplication a(argc, argv);    MainWindow w;    LoginDialog dlg;    if (dlg.exec() == QDialog::Accepted)    {        w.show();        return a.exec();    }    else return 0;}

Run the program.

 

Pure code implementation:

Recreate a project and name it login. Add a new c ++ class named loginDialog, and select QDialog as the parent class.

Modify the logindialog. h file as follows:

Lines 4 to 8 are the header files required for import. Then declare six private variables, that is, the display part of the window, and two slot functions to respond to the button click event. Q_OBJECT must be added to use the signals and slots in Qt and the metadata system functions. Enter the logindialog. cpp file to initialize the component.

LoginDialog: loginDialog () {// create a window display part user = new QLabel (this); user-> setText ("username"); user-> move ); pwd = new QLabel (this); pwd-> setText ("password"); pwd-> move (70,130); userLineEdit = new QLineEdit (this ); userLineEdit-> setPlaceholderText ("enter username"); userLineEdit-> move (); pwdLineEdit = new QLineEdit (this); pwdLineEdit-> setPlaceholderText ("Enter Password "); pwdLineEdit-> setEchoMode (QLineEdit: Password); pwdLineEdit-> move (140,130); loginBtn = new QPushButton (this); loginBtn-> setText ("login "); loginBtn-> move (50,200); exitBtn = new QPushButton (this); exitBtn-> setText ("exit"); exitBtn-> move (210,200 ); // connection SIGNAL and SLOT connect (loginBtn, SIGNAL (clicked (), this, SLOT (on_login_click (); connect (exitBtn, SIGNAL (clicked (), this, SLOT (on_exit_click ()));}

Then implement the corresponding slot functions of the login button and exit button

Void loginDialog: on_login_click () {// trimmed () function: removes spaces at both ends of the string if (userLineEdit-> text (). trimmed () = "username" & pwdLineEdit-> text () = "123456") {accept ();} else {QMessageBox: warning (this, "warning! "," Username or password error ", QMessageBox: Yes); userLineEdit-> clear (); pwdLineEdit-> clear (); userLineEdit-> setFocus ();}} void loginDialog: on_exit_click () {close ();}

Finally, go to the main. cpp file and change the main function:

int main(int argc, char *argv[]){    QApplication a(argc, argv);    MainWindow w;    loginDialog ldg;    if (ldg.exec() == QDialog::Accepted)    {        w.show();        return a.exec();    }    else return 0;}

It is best to use the loginDialog ldg method to create objects in the main function, because this method is created on the stack and the memory is automatically managed by the system. When the program is closed, the memory occupied by ldg is automatically released. If it is changed to loginDialog * ldg = new loginDialog, it is created on the stack, and the app is on the stack. This means that the ldg will analyze the structure after the app, that is to say, the lifecycle of ldg is longer than that of app. This may cause the memory to be incorrectly released and cause memory leakage.

 

Related Article

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.