一,代码完善
头文件
#ifndef ZUOYE_H
#define ZUOYE_H
#include <QWidget>
#include <QDebug>
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
//#include <QTextToSpeech>
QT_BEGIN_NAMESPACE
namespace Ui { class Zuoye; }
QT_END_NAMESPACE
class Zuoye : public QWidget
{
Q_OBJECT
QPushButton *btn_enter; //登录按钮
QPushButton *btn_cancel; //取消按钮
QLabel *lab_id; //账号
QLabel *lab_pwd; //密码
QLabel *lab_logo; //logo
QLineEdit *edit_id;//行编辑器账号
QLineEdit *edit_pwd;//行编辑器密码
public:
Zuoye(QWidget *parent = nullptr);
~Zuoye();
//槽函数
public slots:
void my_cancel(); //关闭
void my_enter();
void my_back_slots(); //接收返回函数
signals:
void my_jump(); //跳转信号
private:
Ui::Zuoye *ui;
};
#endif // ZUOYE_H
源文件
#include "zuoye.h"
#include "ui_zuoye.h"
Zuoye::Zuoye(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Zuoye)
{
ui->setupUi(this);
//界面
this->resize(540,420); //设置尺寸
this->setFixedSize(540,420);//固定尺寸
this->setStyleSheet("background-color:white;");//背景颜色
this->setWindowOpacity(0.95);//透明度
this->setWindowIcon(QIcon(":/icon/qq.png"));//标题栏图标
this->setWindowTitle("马❤哥❤快❤聊");//标题栏名字
//按钮 1-登录 2-取消 QPushbutton
btn_enter = new QPushButton;//构造按钮
btn_enter->setParent(this);//设置父组件
btn_enter->setText("登录");//设置文本内容
btn_enter->resize(75,40);//设置按钮大小
btn_enter->setStyleSheet("background-color:skyblue;border-radius:10px");//设置样式,背景色,
btn_enter->setIcon(QIcon(":/icon/denglu_1.png"));//设置按钮图标
btn_enter->setEnabled(true);//设置可用状态
btn_enter->move(170,320);//移动组件
btn_cancel = new QPushButton;//构造按钮
btn_cancel->setParent(this);//设置父组件
btn_cancel->setText("取消");//设置文本内容
btn_cancel->resize(75,40);//设置按钮大小
btn_cancel->setStyleSheet("background-color:skyblue;border-radius:10px");//设置样式,背景色,
btn_cancel->setIcon(QIcon(":/icon/quxiao.png"));//设置按钮图标
btn_cancel->setEnabled(true);//设置可用状态
btn_cancel->move(290,320);//移动组件
//设置标签 1-账户 2-密码 3-logo label
lab_id = new QLabel;//构造标签
lab_id->setParent(this);//设置父组件
lab_id->resize(40,40);//设置尺寸
lab_id->setPixmap(QPixmap(":/icon/denglu.png"));//设置图标
lab_id->setScaledContents(true);//设置内容自适应
lab_id->move(100,170);//移动
lab_pwd = new QLabel;//构造标签
lab_pwd->setParent(this);//设置父组件
lab_pwd->resize(40,40);//设置尺寸
lab_pwd->setPixmap(QPixmap(":/icon/denglumima.png"));//设置图标
lab_pwd->setScaledContents(true);//设置内容自适应
lab_pwd->move(100,250);//移动
lab_logo = new QLabel;//构造标签
lab_logo->setParent(this);//设置父组件
lab_logo->resize(120,120);//设置尺寸
lab_logo->setPixmap(QPixmap(":/icon/qq.png"));//设置图标
lab_logo->setScaledContents(true);//设置内容自适应
lab_logo->move(205,20);//移动
//设置行编辑器 1-账号, 2-密码
edit_id = new QLineEdit;//构造
edit_id->setParent(this);//设置父组件
edit_id->resize(250,50);//重新设置尺寸
edit_id->setStyleSheet("background-color:red;");//设置颜色//
edit_id->setPlaceholderText("QQ号码/手机/邮箱");//设置占位符
edit_id->setEchoMode(QLineEdit::Normal);//设置密文模式
edit_id->setStyleSheet("border:none;""border-bottom:2px solid blue;");//更改样式表
edit_id->move(180,160);//移动
QFont font;
font.setPointSize(12); // 设置字体大小为12
edit_id->setFont(font);// 将字体应用于行编辑器
edit_pwd = new QLineEdit;//构造
edit_pwd->setParent(this);//设置父组件
edit_pwd->resize(250,50);//重新设置尺寸
edit_pwd->setStyleSheet("background-color:red;");//设置颜色//
edit_pwd->setPlaceholderText("密码");//设置占位符
edit_pwd->setEchoMode(QLineEdit::Password);//设置密文模式
edit_pwd->setStyleSheet("border:none;""border-bottom:2px solid blue;");//更改样式表
edit_pwd->move(180,240);//移动
edit_pwd->setFont(font);// 设置字体大小为12
connect(btn_cancel, SIGNAL(clicked()), this,SLOT(my_cancel())); //取消按钮连接槽函数
QObject::connect(btn_enter, & QPushButton::clicked, this, &Zuoye::my_enter); //登录按钮连接槽函数
}
Zuoye::~Zuoye()
{
delete ui;
}
//取消按钮槽函数
void Zuoye::my_cancel()
{
this->close();
}
//登录按钮槽函数
void Zuoye::my_enter()
{
if("admin" == this->edit_id->text() && "123456" == edit_pwd->text()){
qDebug() << "登录成功..." << endl;
emit my_jump();
close();
}else{
qDebug() << "账号密码不匹配,请重新输入..." << endl;
this->edit_pwd->clear();
}
}
void Zuoye::my_back_slots()
{
this->show();
}
界面二 头文件
#ifndef JUMP_INTERFACE_H
#define JUMP_INTERFACE_H
#include <QWidget>
namespace Ui {
class jump_interface;
}
class jump_interface : public QWidget
{
Q_OBJECT
public:
explicit jump_interface(QWidget *parent = nullptr);
~jump_interface();
private slots:
void on_back_clicked();
//信号 返回登录界面
signals:
void my_back();
//槽 接收登录界面的跳转
public slots:
void my_jump_slot();
private:
Ui::jump_interface *ui;
};
#endif // JUMP_INTERFACE_H
源文件
#include "jump_interface.h"
#include "ui_jump_interface.h"
jump_interface::jump_interface(QWidget *parent) :
QWidget(parent),
ui(new Ui::jump_interface)
{
ui->setupUi(this);
}
jump_interface::~jump_interface()
{
delete ui;
}
//返回按钮槽函数
void jump_interface::on_back_clicked()
{
emit my_back();
this->close();
}
//接收登录界面跳转 槽函数
void jump_interface::my_jump_slot()
{
this->show();
}
运行结果
二,新建一个工程文件,将默认提供的代码加上注释信息
工程管理文件
QT += core gui
#引入Qt苏需要的类库,核心库,图形化界面库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#超过4.0版本,加上 widgets库
CONFIG += c++11
#支持C++11以上版本
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
#管理源文件
HEADERS += \
widget.h
#管理头文件
FORMS += \
widget.ui
#管理UI文件
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
头文件
#ifndef WIDGET_H
#define WIDGET_H
//防止文件重复包含
#include <QWidget> //头文件
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:
Ui::Widget *ui; //使用UI界面对应头文件中的命名空间中的类定义的指针
//后期,如果想要使用ui界面中拖拽出来的组件,可以该指针找到
}; //自己定义的组件,使用this指针找到
#endif // WIDGET_H
源文件
#include "widget.h" //自定义头文件
#include "ui_widget.h" //ui界面的头文件
//构造函数
Widget::Widget(QWidget *parent) //调用父类的有参构造
: QWidget(parent)
, ui(new Ui::Widget) //构造出ui界面拖拽出来的成员,并且将地址赋值给ui指针
{
ui->setupUi(this); //调用设置界面函数,给ui界面上的组件申请空间
}
Widget::~Widget()
{
delete ui; //释放ui界面上的组件空间
}
主函数
#include "widget.h" //自定义头文件
#include <QApplication> //应用程序的头文件
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //使用应用程序类,实例化一个类对象,
//使用自定义类实例化的对象(栈区)
Widget w; //无参构造,实例化一个对象,改界面没有父组件,独立存在,别的组件依附于该对象
w.show();
return a.exec();//阻塞等待界面相关工作:用户在界面上的操作,信号与槽,事件处理
}
三,思维导图