9.9(QT Day 2)

将day1做的登录界面升级优化【资源文件的添加】

在登录界面的登录取消按钮进行以下设置:

使用手动连接,将登录框中的取消按钮使用第2种方式的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数

将登录按钮使用qt4版本的连接到自定义的槽函数中,在槽函数中判断ui界面上输入的账号是否为"admin",密码是否为"123456",如果账号密码匹配成功,则输出"登录成功",并关闭该界面,如果匹配失败,则输出登录失败,并将密码框中的内容清空

1.Widget.cpp

复制代码
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget),
      btn2(new QPushButton("注册",this)),
      btn3(new QPushButton("登录",this)),
      lab4(new QLabel(this))
{
    ui->setupUi(this);

    //-------------窗口相关设置----------------
    this->setWindowTitle("用户登录"); //窗口标题
    this->setWindowIcon(QIcon(":/pictrue/jye.png")); //窗口图标
    this->resize(510,531); //重设窗口大小
    this->setFixedSize(510,531); //固定窗口大小
    //this->setWindowFlag(Qt::FramelessWindowHint); //隐藏头部
    //this->setStyleSheet("background-color:white");
    this->lower();


    //-------------标签相关设置----------------
    QMovie *mv = new QMovie(":/pictrue/longmao.gif");
    ui->loginlab->setMovie(mv); //将动图设置到标签中
    mv->start(); //让动图动起来
    ui->loginlab->setScaledContents(true); //让图自动适应

    QMovie *mv2 = new QMovie(":/pictrue/gongqijun.gif");
    lab4->setMovie(mv2); //将动图设置到标签中
    lab4->move(0,221);
    lab4->resize(510,440);
    lab4->lower();
    mv2->start(); //让动图动起来
    lab4->setScaledContents(true); //让图自动适应

    //----------------------------------------------
    ui->usernamelab->setPixmap(QPixmap(":/pictrue/R-C.png"));
    ui->usernamelab->setScaledContents(true);

    ui->passwdlab->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
    ui->passwdlab->setScaledContents(true);

    //-------------行编译器相关设置----------------
    ui->usernameEdit->setPlaceholderText("用户名");
    ui->passwdEdit->setPlaceholderText("设置密码");
    ui->passwdEdit->setEchoMode(QLineEdit::Password);

    //-------------按钮设置----------------
    btn2->move(ui->passwdlab->x(),ui->passwdlab->y()+ui->passwdlab->height()+70); //移动取消与登录的坐标位置,并重设大小
    btn2->resize(101,51);
    btn2->setStyleSheet("5px");
    btn3->move(ui->passwdlab->x()+ui->passwdlab->width()+170,ui->passwdlab->y()+ui->passwdlab->height()+70);
    btn3->resize(101,51);
    btn3->setStyleSheet("5px");
    connect(btn3,SIGNAL(clicked()),this,SLOT(my_slots()));

}

Widget::~Widget()
{
    delete ui;

}

void Widget::my_slots()
{
    if(ui->usernameEdit->text() == "admin" && ui->passwdEdit->text() == "123456")
    {
        qDebug("登录成功");
    }
    else
    {
        qDebug("登陆失败");
        ui->passwdEdit->clear();
    }
}

void Widget::on_pushButton_clicked()
{
   this->close();
}

2.Widget.h

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QLabel> //标签类
#include <QPushButton> //按钮类
#include <QIcon> //图标类
#include<QMovie> //动图类
#include <QDebug>

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_pushButton_clicked();

public slots:
    void  my_slots();

private:
    Ui::Widget *ui;
    QPushButton *btn2;
    QPushButton *btn3;
    QLabel *lab4;
};
#endif // WIDGET_H

实现效果:

相关推荐
用户805533698034 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner4 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz9 天前
QML Hello World 入门示例
qt
xcyxiner12 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner13 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner13 天前
DicomViewer (添加模型类)3
qt
xcyxiner14 天前
DicomViewer (目录调整) 2
qt
xcyxiner14 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR00616 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术16 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript