Qt study notes - realize the user from the dialog box login authentication to enter the main interface

step:

1. Click "new project", create a new "Qt Widgets Application", click the button "choose" in the lower right corner; customize a project name YonghuLogin, select "QWidget" for the base class name, and the class name is optional, click "Next" "Finish" "; run to generate a main interface with QWidget as the base class.

2. Click "File - New Project - Qt - Qt Designer Interface Class", click "choose" in the lower right corner, select "Dialog without Buttons", the custom class name is logindialog, click "Finish" to generate a new .h Three files with .cpp and .ui; add the button controls required for the user login interface on this dialog box.

3. After adding a text input box, login, and exit buttons, add the click function in logindialog.cpp as follows:

     Click the "Login" button, click "+" in the Signals and slots editor box below to pull down the associated signals and slots, that is, the sender is set to enterBtn, the Signal signal is set to clicked(), the Receiver is set to myDlg, and the Slot slot is set to It is accept(), as shown below:

                                          

code show as below:

 
 
#include"logindialog.h" 
#include"ui_logindialog.h" 
//______________
#include <QMessageBox> //Header file of QmessageBox  
//______________
 
 
 
 
 
 
 
 
//Constructor
LoginDialog :: LoginDialog ( QWidget  * parent)  :
    QDialog(parent),
    ui ( new Ui :: LoginDialog ) 
{
   ui -> setupUi ( this );
   ui -> pwdLineEdit ->setEchoMode( QLineEdit :: Password ); //In the constructor, set the display of the password box to a black dot, which is invisible
}

//destructor
LoginDialog :: ~ LoginDialog ()
{
   deleteui; 
}


//Log in
voidLoginDialog::on_loginBtn_clicked() 
{
    //accept();//Accept function is: hide the current window (so that the function of the sub-window disappears), and send the accepted signal, that is, set the result code to Accepted.
    if(ui->usrLineEdit->text()/*.trimmed()*/==tr("QT")&&ui->pwdLineEdit->text()==tr("123456"))
    {
      //Add trimmed() to remove whitespace characters at the beginning and end of the username string
      accept();
    }
    else
    {
      //If incorrect, pop up a warning box
      QMessageBox ::warning( this ,tr( "warning" ),tr( "Incorrect username or password!" ), QMessageBox :: Yes );
      //If you still want to clear the username and password boxes, and the cursor automatically jumps to the username input box, continue below
      ui->usrLineEdit->clear();
      ui->pwdLineEdit->clear();
      ui -> usrLineEdit ->setFocus(); //Move the cursor to the username box
    }
}
 
 


//quit
voidLoginDialog::on_exitBtn_clicked() 
{
  close();
}
 
 

In the main.cpp file, modify main() as follows:

#include"widget.h" 
#include<QApplication> 
//_______
#include"logindialog.h" 
//_______
 
 
 
 
 
 


intmain(intargc,char*argv[])    
{
    QApplication a(argc, argv);
    Widget w;
    //w.show();//Display the main interface
    //returna.exec(); 
    // rewrite as follows 
 
 
    LoginDialog  logindlg; //Create an object of your own new class
    if (logindlg.exec ()== QDialog :: Accepted ) //Use the Accepted signal to determine whether enterBtn is pressed
    {
      w.show(); //If pressed, show the main window
      return  a.exec(); //The program is executed until the main window is closed
    }
    else
    {
      return 0 ;//If it is not pressed, it will not enter the main window, and the entire program will end running 
    }
}

【Note:】Be careful to add header files


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326521311&siteId=291194637