qt------->login

该代码示例展示了如何使用Qt创建一个带有登录功能的GUI应用,包括图片自适应、密码显示为小黑点以及通过点击不同按钮切换皮肤。当用户点击登录按钮时,检查用户名和密码是否匹配;若不匹配,则清空密码框。同时,定义了槽函数处理按钮点击事件,实现不同风格的皮肤切换。
摘要由CSDN通过智能技术生成

#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

#include "mylogin.h" mylogin::mylogin(QWidget *parent) : QDialog(parent) { this->init_ui(); connect(this->bnt_login, &QPushButton::clicked, this, &mylogin::do_login); connect(this->bnt_register, &QPushButton::clicked , this ,&mylogin::do_enroll); } mylogin::~mylogin() { } void mylogin::init_ui() { this->setFixedSize(QSize(600,350)); this->setWindowTitle(tr("岑超升")); this->setWindowIcon(QIcon(":/src/1.png")); this->lb1 = new QLabel(); this->lb2 = new QLabel(); this->lb3 = new QLabel(); this->lb1->setFixedSize(QSize(560,200)); QPixmap pic; pic.load(":/src/2.png"); //this->lb1->setPixmap(pic.scaled(this->lb1->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); this->lb1->setPixmap(QPixmap(":/src/2.png")); this->lb2->setText(tr("用户名:")); this->lb3->setText(tr("密 码:")); this->usr_name_le = new QLineEdit(); //这两个参数后面传入信号中去,然后emit发射出去 this->usr_pass_le = new QLineEdit(); this->usr_pass_le->setEchoMode(QLineEdit::Password); this->bnt_login = new QPushButton(tr("登 陆")); this->bnt_register = new QPushButton(tr("注 册")); this->hb1 = new QHBoxLayout(); this->hb2 = new QHBoxLayout(); this->hb3 = new QHBoxLayout(); this->hb1->addWidget(this->lb2); this->hb1->addWidget(this->usr_name_le); this->hb2->addWidget(this->lb3); this->hb2->addWidget(this->usr_pass_le); this->hb3->addWidget(this->bnt_login); this->hb3->addWidget(this->bnt_register); this->vb1 = new QVBoxLayout(); this->vb1->addWidget(this->lb1); this->vb1->addLayout(this->hb1); this->vb1->addLayout(this->hb2); this->vb1->addLayout(this->hb3); this->setLayout(this->vb1); } void mylogin::do_login() { emit sig_login(usr_name_le->text(), usr_pass_le->text()); //需要把这里输入的账号密码信息发送到myapp那去,用到的函数是emit //需要自定义一个信号,sig_login //这个槽函数能够发出信号 } void mylogin::do_enroll() { emit sig_enroll(); }用C++语言解释并注释每行代码,并说明他们的功能和作用
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值