#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QTextStream>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
ui->label_user_name->setScaledContents(true); //图片自适应label大小
ui->label_pwd->setScaledContents(true); //图片自适应label大小
ui->lineE_pwd->setEchoMode(QLineEdit::Password);//设置为小黑点
connect(ui->btn_1,SIGNAL(clicked(bool)),this,SLOT(set_style()));
connect(ui->btn_2,SIGNAL(clicked(bool)),this,SLOT(set_style()));
connect(ui->btn_3,SIGNAL(clicked(bool)),this,SLOT(set_style()));
connect(ui->btn_4,SIGNAL(clicked(bool)),this,SLOT(set_style()));
this->connect(ui->btn_login,SIGNAL(clicked()),this,SLOT(my_slot_login()));
this->connect(ui->btn_pwd,SIGNAL(clicked()),this,SLOT(my_slot_unlogin()));
}
/*
* 槽函数-皮肤设置
*/
QPushButton* btn;
void Widget::set_style()
{
btn = qobject_cast<QPushButton*>(sender());//获取发射信号的对象
QString filePath;
if("btn_1" == btn->objectName()) //粉色
{
filePath = ":/res/qss/style-1.qss";
}else if("btn_2" == btn->objectName()) //黄蓝
{
filePath = ":/res/qss/style-2.qss";
}else if("btn_3" == btn->objectName()) //浅紫
{
filePath = ":/res/qss/style-3.qss";
}else if("btn_4" == btn->objectName()) //青绿
{
filePath = ":/res/qss/style-4.qss";
}
/*皮肤设置*/
QFile file(filePath);/*QSS文件所在的路径*/
file.open(QFile::ReadOnly);
QTextStream filetext(&file);
QString stylesheet = filetext.readAll();
this->setStyleSheet(stylesheet);
file.close();
}
Widget::~Widget()
{
delete ui;
}
void Widget::my_slot_login()
{
if(ui->lineE_user_name->text()=="admin"&&ui->lineE_pwd->text()=="123456")
{
qDebug() << "登录成功";
close();
}
else
{
qDebug() << "请重新输入";
ui->lineE_pwd->setText("");
}
}
void Widget::my_slot_unlogin()
{
close();
}
1.注释
#工程项目所包含的类库 core核心库 gui图形化界面库
QT += core gui
#4.0版本之后自动加widgets,4.0版本之前widgets包含在core库中
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#支持c++11,包含lambda表达式
CONFIG += 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 \
firstwindow.cpp
#管理头文件
HEADERS += \
firstwindow.h
#管理ui界面文件
FORMS += \
firstwindow.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 FIRSTWINDOW_H
#define FIRSTWINDOW_H
//父类头文件
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class FirstWindow; }//两个类不是同一个类
QT_END_NAMESPACE
//自己定义的类继承QWidget类
class FirstWindow : public QWidget
{
Q_OBJECT //处理信号与槽的元对象
public:
FirstWindow(QWidget *parent = nullptr);//构造函数声明
~FirstWindow();//析构函数的声明
private:
Ui::FirstWindow *ui;//指向ui界面的指针 ui命名空间内的FirstWindow类继承了Ui_FirstWindow
};
#endif // FIRSTWINDOW_H
#include "firstwindow.h" //自定义头文件
#include "ui_firstwindow.h" //ui界面的头文件
FirstWindow::FirstWindow(QWidget *parent) //构造函数的实现
: QWidget(parent) //给父类的构造完成初始化
, ui(new Ui::FirstWindow) //给ui指针初始化空间
{
ui->setupUi(this); //【setupUi方法:把拖拽的组件实例化】
}
FirstWindow::~FirstWindow() //析构函数的实现
{
delete ui; //释放指针空间
}
#include "firstwindow.h" //引入自定义头文件
#include <QApplication> //引入应用程序的头文件
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //实例化应用程序对象
FirstWindow w; //栈区实例化对象,ui组件会把FirstWindow当作父组件【对象树模型:父组件挂了,会把子组件也析构】
w.show(); //调用show方法,将界面显示出来
return a.exec(); //阻塞窗口,防止程序结束【1.等待信号与槽 2.等待用户操作界面 3.等待事件发生】 轮询等待事件触发
}
2.
#include "login.h"
#include "ui_login.h"
login::login(QWidget *parent)
: QWidget(parent)
, ui(new Ui::login)
{
ui->setupUi(this);
this->setMaximumSize(500,400);
this->setMinimumSize(500,400);
this->setFixedSize(500,400);
this->setWindowTitle("login");
this->setWindowIcon(QIcon(":/1.png"));
logo = new QLabel(this);
logo->setPixmap(QPixmap(":/1.png"));
logo->move(215,45);
logo->resize(85,85);
logo->setScaledContents(true);
l1 = new QLabel("用户名:",this);
l1->setPixmap(QPixmap(":/denglu.png"));
l1->resize(25,25);
l1->setScaledContents(true);
l1->move(160,145);
l2 = new QLabel("密 码:",this);
l2->setPixmap(QPixmap(":/denglumima.png"));
l2->resize(25,25);
l2->setScaledContents(true);
l2->move(160,195);
ed1 = new QLineEdit("user",this);
ed1->move(200,145);
ed2 = new QLineEdit(this);
ed2->setEchoMode(QLineEdit::Password);
ed2->move(200,195);
ed2->setPlaceholderText("密码");
b1 = new QPushButton(QIcon(":/denglu_1.png"),"登录",this);
b1->move(150,270);
this->connect(b1,SIGNAL(clicked()),this,SLOT(my_slot_login()));
b2 = new QPushButton(QIcon(":/quxiao.png"),"取消",this);
b2->move(280,270);
this->connect(b2,SIGNAL(clicked()),this,SLOT(my_slot_unlogin()));
this->setStyleSheet("background-color:rgb(240,248,255)");
this->setWindowOpacity(0.9);
}
login::~login()
{
delete ui;
}
void login::my_slot_login()
{
if(ed1->text()=="admin"&&ed2->text()=="123456")
{
qDebug() << "登录成功";
close();
}
else
{
qDebug() << "请重新输入";
ed2->setText("");
}
}
void login::my_slot_unlogin()
{
close();
}
#ifndef LOGIN_H
#define LOGIN_H
#include <QWidget>
#include <QIcon>
#include <QDebug>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui { class login; }
QT_END_NAMESPACE
class login : public QWidget
{
Q_OBJECT
signals:
void my_signal_login();
void my_signal_unlogin();
protected slots:
void my_slot_login();
void my_slot_unlogin();
public:
QLabel *logo;
QLabel *l1;
QLabel *l2;
QPushButton *b1;
QPushButton *b2;
QLineEdit *ed1;
QLineEdit *ed2;
login(QWidget *parent = nullptr);
~login();
private:
Ui::login *ui;
};
#endif // LOGIN_H