shencang / note

关于学习的一点东西,个人笔记。系统记录见博客:
http://blog.shencangblue.com
Apache License 2.0
6 stars 1 forks source link

QT:登陆界面 #7

Open shencang opened 6 years ago

shencang commented 6 years ago

#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H
#include"QDialog"

class QLabel;
class QLineEdit;
class QPushButton;

class LoginDialog : public QDialog
{
    Q_OBJECT

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

private:
    QLabel *usrLable;
    QLabel  *pwdLable;
    QLineEdit *usrLineEdit;
    QLineEdit *pwdLineEdit;
    QPushButton *loginBtn;
    QPushButton *exitBtn;

private slots:
    void login();
};

#endif // LOGINDIALOG_H

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
};

#endif // MAINWINDOW_H

#include "logindialog.h"
#include"QLabel"
#include"QLineEdit"
#include"QPushButton"
#include"QMessageBox"
LoginDialog::LoginDialog(QWidget *parent):
    QDialog(parent)
{
    usrLable = new QLabel(this);
    usrLable->move(70,80);
    usrLable->setText(tr("用户名:"));
    usrLineEdit = new QLineEdit(this);
    usrLineEdit->move(140,80);
    usrLineEdit->setPlaceholderText(tr("好歹输一个吧"));

    pwdLable= new QLabel(this);
    pwdLable->move(70,130);
    pwdLable->setText(tr("密码:"));
    pwdLineEdit= new QLineEdit(this);
    pwdLineEdit->move(140,130);
    pwdLineEdit->setPlaceholderText(tr("好歹输一个吧"));

    loginBtn = new  QPushButton(this);
    loginBtn->move(50,200);
    loginBtn->setText(tr("登录"));
    exitBtn = new  QPushButton(this);
    exitBtn->move(210,200);
    exitBtn->setText(tr("退出"));

    connect(loginBtn,&QPushButton::clicked,this,&LoginDialog::login);
    connect(exitBtn,&QPushButton::clicked,this,&LoginDialog::login);

}
LoginDialog::~LoginDialog()
{

}
void LoginDialog::login()
{
    if(usrLineEdit->text().trimmed()==tr("user")&&pwdLineEdit->text()==tr("123456"))
    {
        accept();
    }
    else
    {
        QMessageBox::warning(this,tr("红色警戒"),tr("您的输入有误!"),QMessageBox::Yes);
        usrLineEdit->clear();
        pwdLineEdit->clear();
        usrLineEdit->setFocus();
    }
}

#include "mainwindow.h"
#include <QApplication>
#include"logindialog.h"

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;
}

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
}

MainWindow::~MainWindow()
{

}