1. 思维导图
2.使用手动连接,将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数; 将登录按钮使用qt5版本的连接到自定义的槽函数中,在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456",如果账号密码匹配成功,则输出“登录成功”,并关闭该界面,如果匹配失败,则输出登录失败,并将密码框中的内容清空
project structure
代码
mywidget.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QIcon>
#include <QMovie>
#include <QPushButton>
class MyWidget : public QWidget
{
Q_OBJECT
public:
QPushButton *pbtn_submit;
QLineEdit *plnedt_usr;
QLineEdit *plnedt_psw;
public:
MyWidget(QWidget *parent = nullptr);
~MyWidget();
public slots:
void on_btn_submit_clicked();
};
#endif // MYWIDGET_H
main.cpp
#include "mywidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();
return a.exec();
}
mywidget.cpp
#include "mywidget.h"
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent),
pbtn_submit(new QPushButton("login", this)),
plnedt_usr(new QLineEdit(this)),
plnedt_psw (new QLineEdit(this))
{
//=== windows setup ===
//setup windows size
this->resize(600, 370);
//set window fixed size
this->setFixedSize(600, 370);
//set window without frame
this->setWindowFlag(Qt::FramelessWindowHint);
//=== set label moive ===
QLabel *plbl_screen = new QLabel(this);
plbl_screen->resize(230,370);
plbl_screen->move(0, 0);
//setup label background color
plbl_screen->setStyleSheet("background-color:#8EDE99 "); //#8EDDF9 , 142, 221, 249 rgb(147, 226, 254 )
// create movie
QMovie *pmv = new QMovie(":/pictrue/screen.gif");
pmv->scaledSize();
//set lable's movie
plbl_screen->setMovie(pmv);
//start moive
pmv->start();
//set to fit label
plbl_screen->setScaledContents(true);
//setup lable title
QLabel *plbl_title = new QLabel("smart screen system",this);
plbl_title->resize(150,20);
plbl_title->move(460, 10);
//setup LineEdit user name input
//QLineEdit *plnedt_usr = new QLineEdit(this);
plnedt_usr->setEchoMode(QLineEdit::Normal);
plnedt_usr->setPlaceholderText("please input user name");
plnedt_usr->resize(270, 30);
plnedt_usr->move(290,180);
//setup LineEdit password input
//QLineEdit *plnedt_psw = new QLineEdit(this);
plnedt_psw->setEchoMode(QLineEdit::Password);
plnedt_psw->setPlaceholderText("please input password");
plnedt_psw->resize(270, 30);
plnedt_psw->move(290, 230);
//setup pushbutton
pbtn_submit->resize(270, 30);
pbtn_submit->move(290, 290);
pbtn_submit->setStyleSheet("background-color: rgb(147, 226, 254 )");
//setup cancel btn
QPushButton *pbtn_canel = new QPushButton("cancel", this);
pbtn_canel->resize(270, 30);
pbtn_canel->move(290, 330);
pbtn_canel->setStyleSheet("background-color: rgb(147, 226, 254 )");
//btn_cancel connect based on qt4
connect(pbtn_canel, SIGNAL(clicked()), this, SLOT(close()));
//btn_login connect based on qt5
connect(pbtn_submit, &QPushButton::clicked, this, &MyWidget::on_btn_submit_clicked);
}
MyWidget::~MyWidget()
{
}
void MyWidget::on_btn_submit_clicked()
{
if(plnedt_usr->text() == "admin" && plnedt_psw->text()=="123456")
{
qDebug("success in login\n");
this->close();
}
else
{
plnedt_psw->setText("");
}
}
运行效果:
输入正确的用户名和密码,打印 登录成功
输入错误的用户名和密码,清除密码框内容