1.头文件
#ifndef WORK_H
#define WORK_H
#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
QT_BEGIN_NAMESPACE
namespace Ui { class work; }
QT_END_NAMESPACE
class work : public QMainWindow
{
Q_OBJECT
public:
work(QWidget *parent = nullptr);
~work();
QPushButton *btn;
QPushButton *btn_1;
QLineEdit *line_pass;
QLineEdit *line_name;
void name_pass();
void quit();
private:
Ui::work *ui;
};
#endif // WORK_H
2.主函数
#include "work.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
work w;
w.show();
return a.exec();
}
3.执行代码
#include "work.h"
#include "ui_work.h"
work::work(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::work)
{
ui->setupUi(this);
//更改尺寸的面积
this->setFixedSize(540,410);
//设置窗口图标以及标签
this->setWindowIcon(QIcon("D:\\QT\\qtC++\\qt_day1\\02_qdebug\\chick.png"));
this->setWindowTitle("真爱家族");
//在中间放置一个log图
QLabel *label=new QLabel(this);
label->setPixmap(QPixmap("D:\\QT\\qtC++\\qt_day1\\05_work\\chick_1.png"));
label->resize(540,205);
label->setScaledContents(true);
//用户名和密码使用图片完成
QLabel *label_name=new QLabel(this);
label_name->setPixmap(QPixmap("D:\\QT\\qtC++\\qt_day1\\05_work\\name.png"));
label_name->resize(50,50);
label_name->move(150,240);
label_name->setScaledContents(true);
//密码
QLabel *label_pass=new QLabel(this);
label_pass->setPixmap(QPixmap("D:\\QT\\qtC++\\qt_day1\\05_work\\pass.png"));
label_pass->resize(50,50);
label_pass->move(150,290);
label_pass->setScaledContents(true);
//输入一个账户
line_name=new QLineEdit(this);
line_name->resize(150,35);
line_name->setStyleSheet("background-color:pink");
line_name->move(200,240);
//输入密码
line_pass=new QLineEdit(this);
line_pass->resize(150,35);
line_pass->setStyleSheet("background-color:pink");
line_pass->move(200,305);
line_pass->setEchoMode(QLineEdit::Password);
//设置登陆以及取消按钮
btn=new QPushButton("登陆",this);
btn->move(180,350);
btn->resize(40,40);
btn_1=new QPushButton("取消",this);
btn_1->move(280,350);
btn_1->resize(40,40);
//账户密码比较
connect(btn,&QPushButton::clicked,this,&work::name_pass);
connect(btn_1,&QPushButton::clicked,this,&work::quit);
}
work::~work()
{
delete ui;
}
void work::name_pass()
{
QString s1("admin");
if(this->line_name->text()==s1)
{
s1="123456";
if(this->line_pass->text()==s1)
printf("登陆成功\n");
}
else
printf("账户密码不匹配");
this->line_pass->clear();//清空密码框
}
void work::quit()
{
printf("关闭当前窗口\n");
// work_1->close();
this->close();
}