widget.h
#define WIDGET_H
#include <QWidget> //引入父类头文件
#include<QLabel>
#include<QPushButton>
#include<QLineEdit>
#include<QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; } //命名空间的声明,UI界面生成的头文件的命名空间
QT_END_NAMESPACE
class Widget : public QWidget 自定义的类,继承自QWidget
{
Q_OBJECT //处理信号与 槽的元对象
public:
Widget(QWidget *parent = nullptr); //自定义类的有参构造函数的声明
~Widget(); //自定义类的析构函数的声明,是虚函数
private slots: //该权限下声明槽函数
void login_slot();
void cancel_slot();
private:
Ui::Widget *ui; //指向ui界面的指针,如果使用拖拽到界面的组件,需要使用该指针才能找到
public:
QLabel *logoLabel;
QLabel *nameLabel;
QLabel *pwdLabel;
QPushButton *nameBtn;
QPushButton *pwdBtn;
QLineEdit *nameText;
QLineEdit *pwdText;
QPushButton *loginBtn;
QPushButton *cancelBtn;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication> //应用程序的头文件
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //实例化一个应用程序的对象
Widget w; //实例化一个自定义类的对象
w.show(); //由于该类继承自QWidget,由此也有show函数,调用show函数显示界面
return a.exec(); //阻塞等待界面处理,等待信号与槽,等待用户操作界面,等待事件发生
}
widget.cpp
#include "widget.h"
#include "ui_widget.h" //包含ui界面的头文件
#include<QIcon>
Widget::Widget(QWidget *parent) //有参构造函数的实现 调用了父类的构造函数
: QWidget(parent)
, ui(new Ui::Widget) //给ui指针初始化空间
{
ui->setupUi(this); //调用ui界面中的设置界面函数
this->setFixedSize(this->size());
this->setWindowIcon(QIcon(":/icon/qq.png"));
this->setWindowTitle("QQ");
logoLabel = new QLabel(this);
logoLabel->setPixmap(QPixmap(":/icon/logo_green.png"));
logoLabel->resize(500,200);
logoLabel->setScaledContents(true);
nameBtn = new QPushButton(this);
nameBtn->setIcon(QIcon(":/icon/denglu.png"));
nameBtn->move(80,220);
pwdBtn = new QPushButton(this);
pwdBtn->setIcon(QIcon(":/icon/denglumima.png"));
pwdBtn->move(80,260);
nameText = new QLineEdit(this);
nameText->setPlaceholderText("账号");
nameText->resize(300,30);
nameText->move(120,220);
pwdText = new QLineEdit(this);
pwdText->setPlaceholderText("密码");
pwdText->setEchoMode(QLineEdit::Password);
pwdText->resize(nameText->size());
pwdText->move(120,260);
loginBtn = new QPushButton("登录",this);
loginBtn->setIcon(QIcon(":/icon/denglu_1.png"));
loginBtn->move(300,300);
cancelBtn = new QPushButton("取消",this);
cancelBtn->move(400,300);
cancelBtn->setIcon(QIcon(":/icon/quxiao.png"));
connect(loginBtn,SIGNAL(clicked()),this,SLOT(login_slot()));
connect(cancelBtn,SIGNAL(clicked()),this,SLOT(cancel_slot()));
}
Widget::~Widget() //析构函数的实现
{
delete ui; //释放ui指针
}
void Widget::login_slot()
{
QString username;
QString password;
username = nameText->text();
password = pwdText->text();
if(username == "admin" && password == "123456")
{
qDebug() << "登录成功";
this->close();
}
else
{
qDebug() << "账号和密码不匹配,请重新输入";
pwdText->clear();
}
}
void Widget::cancel_slot()
{
this->close();
}