头文件
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QIcon>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void loginBtn_sig();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
源文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//设置窗口属性
this->setWindowTitle("软件登录");
this->setWindowIcon(QIcon(":/logo.png"));
this->setFixedSize(500,300);
ui->loginBtn->setIcon(QIcon(":/login.png"));
ui->cancelBtn->setIcon(QIcon(":/cancel.png"));
//设置密码回显模式
ui->pwdEdit->setEchoMode(QLineEdit::Password);
//qt4版本连接
connect(ui->loginBtn,SIGNAL(clicked()),this,SLOT(loginBtn_sig()));
//qt5版本连接
connect(ui->cancelBtn,&QPushButton::clicked,this,&MainWindow::close);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::loginBtn_sig()
{
QString zh = ui->userNameEdit->text();
QString mm = ui->pwdEdit->text();
if(zh=="admin"&&mm=="123456")
{
qDebug()<<"登录成功";
close();
}
else
{
qDebug()<<"登录失败";
ui->pwdEdit->clear();
}
}