QTday02(常用类、UI界面下的开发、信号与槽)

今日任务

使用手动连接,将登录框中的取消按钮使用qt4版本的连接到自定义的槽函数中,在自定义的槽函数中调用关闭函数

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

2.思维导图

功能代码:

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setFixedSize(560,430);
    this->setStyleSheet("background-color:#faf7ec");
    this->setWindowFlag(Qt::FramelessWindowHint);//无边框
    QMovie *movie = new QMovie(":/111/cai.gif");
    ui->backLabel->setMovie(movie);
    ui->backLabel->setScaledContents(true);
    movie->start();

    ui->closeButton->setStyleSheet("border-image:url(:/111/basketball.png)");


    ui->avatorLabel->resize(60,60);
    ui->avatorLabel->setStyleSheet("border-image:url(:/111/user.png);border-radius:30px");

    ui->accountLabel->setPixmap(QPixmap(":/111/account.jpg"));
    //ui->accountLabel->resize(40,40);
    ui->accountLabel->setScaledContents(true);

    ui->passwdLabel->setPixmap(QPixmap(":/111/passwd.jpg"));
    //ui->passwdLabel->resize(40,40);
    ui->passwdLabel->setScaledContents(true);

    ui->accoountLine->setPlaceholderText("账号");
    ui->passwdLine->setPlaceholderText("密码");
    ui->passwdLine->setEchoMode(QLineEdit::Password);

    ui->loginLabel->setPixmap(QPixmap(":/111/2.png"));
    ui->loginLabel->setScaledContents(true);

    ui->loginButton->setStyleSheet("background-color:#409EFF;border-radius:5px");

    connect(ui->closeButton,SIGNAL(clicked()),this,SLOT(close()));

    connect(ui->loginButton,&QPushButton::clicked,this,&Widget::loginButton_slot);

}

Widget::~Widget()
{
    delete ui;
}


void Widget::loginButton_slot()
{
    if(ui->accoountLine->text()=="admin"&&ui->passwdLine->text()=="123456"){
        qDebug() << "登录成功" <<endl;
        this->close();
    }else{
        qDebug() << "账号或者密码错误" <<endl;
        ui->passwdLine->setText("");
    }
}

头文件:

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#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();

public slots:
    void loginButton_slot();


private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

UI界面布局:

运行结果:

今日思维导图:

课程代码:参考一下可以

头文件:

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H


#include <QWidget>
#include<QPushButton>
#include<QTextToSpeech> //语音播报类


QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE


class Widget : public QWidget
{
    Q_OBJECT //有关信号和槽的宏


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


signals:     //表示该权限下都是信号函数
    void my_signal();  //自定义一个信号函数




public slots:  //表示该权限下是公共的槽函数
    void my_slot();  //自定义一个槽函数
    void Btn4_slot();  //btn4对应的槽函数声明




private slots:
    void on_Btn2_clicked(); //系统定义的槽函数声明


    void on_Btn6_clicked(); //按钮6对应槽函数的声明


    void on_Btn7_clicked();


private:
    Ui::Widget *ui;




    QPushButton *btn3; //定义一个按钮3


    //定义一个语音播报者
    QTextToSpeech *speecher;
};
#endif // WIDGET_H

源文件

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


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);


    //给语音播报者实例一个空间
    speecher = new QTextToSpeech(this);


    //实例化一个按钮
    btn3 = new QPushButton("按钮3",this);
    btn3->move(ui->Btn2->x(), ui->Btn2->y()+ui->Btn2->height()+10);
    btn3->resize(ui->Btn2->width(),ui->Btn2->height());


    //手动连接信号和系统槽,基于qt4版本  是不友好的连接
    //函数原型:[static] QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
    //参数1:是一个组件的指针 信号的发送者
    //参数2:信号函数,需要宏函数转换
    //参数3:是一个组件的指针 信号的接受者
    //参数4:槽函数,需要宏函数转换
    //connect(btn3, SIGNAL(clicked()), this, SLOT(close()));


    //手动连接信号和系统槽,基于qt4版本  是不友好的连接
    this->connect(btn3, SIGNAL(clicked()), this,SLOT(my_slot()));


    //手动连接信号和自定义槽,基于qt5版本  是友好的连接
    //函数原型:[static] QMetaObject::Connection QObject::connect(const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type = Qt::AutoConnection)
    //参数1:是一个组件的指针 信号的发送者
    connect(ui->Btn4, &QPushButton::clicked, this, &Widget::Btn4_slot);


    //手动连接信号和lambda表达式
    connect(ui->Btn5, &QPushButton::clicked, [&](){
        ui->Btn5->setText("55555");
    });


    //手动连接自定义的信号
    connect(this, &Widget::my_signal,[&](){
        ui->Btn6->resize(ui->Btn6->width()+5, ui->Btn6->height()+5);
    });
}


Widget::~Widget()
{
    delete ui;
}


//自定义槽函数的实现
void Widget::my_slot()
{
    this->close();
}


//按钮4对应的槽函数实现    #include<QTextToSpeech> //语音播报类
void Widget::Btn4_slot()
{
    //this->close();


    static int num = 0;
    if(num%3 == 0)
    {
        speecher->say("咳咳咳");
    }
    else if (num%3 == 1)
    {
        speecher->say(ui->Btn2->text());
    }
    else if (num%3 == 2)
    {
        speecher->say(this->btn3->text());
    }


    ++num;


}


//按钮2对应的槽函数
void Widget::on_Btn2_clicked()
{
    //让按钮1变色
    //ui->Btn1->setStyleSheet("background-color:red");
    static int num = 0;
    if(num%3 == 0)
    {
        ui->Btn1->setStyleSheet("background-color:red");
    }
    else if (num%3 == 1)
    {
        ui->Btn1->setStyleSheet("background-color:green");
    }
    else if (num%3 == 2)
    {
        ui->Btn1->setStyleSheet("background-color:blue");
    }


    ++num;


}


//按钮6对应的槽函数处理
void Widget::on_Btn6_clicked()
{
    emit my_signal(); //触发(发射)自定义的信号
}


void Widget::on_Btn7_clicked()
{
    //断开连接
   disconnect(ui->Btn4, &QPushButton::clicked, this, &Widget::Btn4_slot);
}
相关推荐
reg1833 小时前
包含30个APP移动端网站UI的psd适用于餐厅咖啡店面包店快餐店
ui
zhoxier4 小时前
element-ui 的el-table,多选翻页后,之前选择的数据丢失问题处理
vue.js·ui·elementui
深空数字孪生7 小时前
小程序 UI 设计,怎样在方寸间实现高效交互
ui·小程序·交互
应巅21 小时前
echarts 数据大屏(无UI设计 极简洁版)
前端·ui·echarts
中国软件测试质量协会2 天前
UI自动化测试:现状,效果和最佳实践
ui·自动化·ui自动化·webui·界面自动化
派阿喵搞电子3 天前
在UI界面内修改了对象名,在#include “ui_mainwindow.h“没更新
c++·qt·ubuntu·ui
pop_xiaoli3 天前
OC—UI学习-2
学习·ui·ios
界面开发小八哥3 天前
界面开发框架DevExpress XAF实践:集成.NET Aspire后如何实现数据库依赖?
ui·.net·界面控件·devexpress·ui开发·xaf
Humbunklung4 天前
Rust Floem UI 框架使用简介
开发语言·ui·rust
CodeCraft Studio5 天前
【案例分享】如何借助JS UI组件库DHTMLX Suite构建高效物联网IIoT平台
javascript·物联网·ui