"Qt Study Notes" Chapter 1--Login Interface Design

The purpose of this program is to write a login interface program, which includes the function of account password confirmation, the input box is set to prompt characters, the password box adopts the password mode, and the login button is set as the default focus. The basic logic is: when entering the account password, press the Enter key or click the login button, the program judges whether the account password is correct, if it is correct, the main window will pop up, otherwise a warning box will pop up. The program effect is shown in the figure:



head File:

#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H

#include <QDialog>
#include<QMessageBox>

namespace Ui {
class LoginDialog;
}

class LoginDialog : public QDialog
{
    Q_OBJECT

public:
    explicit LoginDialog(QWidget *parent = 0);
    ~ LoginDialog ();

private slots:
    void on_enter_pushButton_clicked();

private:
    Ui::LoginDialog *ui;
};

#endif // LOGINDIALOG_H

cpp file

#include "LoginDialog.h"
#include "ui_LoginDialog.h"

LoginDialog :: LoginDialog (QWidget * parent):
    QDialog(parent),
    ui (new Ui :: LoginDialog)
{
    ui-> setupUi (this);
    ui->enter_pushButton->setDefault(true);
    connect(ui->enter_pushButton,&QPushButton::clicked,this,&LoginDialog::on_enter_pushButton_clicked);
    connect(ui->quit_pushButton,&QPushButton::clicked,this,&LoginDialog::close);
}

LoginDialog :: ~ LoginDialog ()
{
    delete ui;
}

void LoginDialog::on_enter_pushButton_clicked()
{
    if(ui->user_lineEdit->text() == tr("wangcong") && ui->password_lineEdit->text() == tr("lovefzj"))
    {
        accept();
    }
    else
    {
        QMessageBox::warning(this,tr("Warning"),tr("Incorrect username or password"));
        ui->user_lineEdit->clear();
        ui->password_lineEdit->clear();
        ui->user_lineEdit->setFocus();
    }
}

main function:
#include "mainwindow.h"
#include"LoginDialog.h"
#include <QApplication>
#include<QDebug>
#include<QString>

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



Guess you like

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