QT day2 2.21

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

代码:

复制代码
#include "mywidget.h"
#include "ui_mywidget.h"

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    Btn2 = new QPushButton("取消",this);
    Btn2->resize(ui->Btn1->width(),ui->Btn1->height());
    Btn2->move(ui->Btn1->x(),ui->Btn1->y()+ui->Btn1->height()+20);
    connect(Btn2,SIGNAL(clicked()),this,SLOT(close()));
}

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

运行结果:

按下取消后窗口关闭

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

代码:

复制代码
#include "mywidget.h"
#include "ui_mywidget.h"

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWidget)
{
    ui->setupUi(this);
    Btn2 = new QPushButton("取消",this);
    Btn2->resize(ui->Btn1->width(),ui->Btn1->height());
    Btn2->move(ui->Btn1->x(),ui->Btn1->y()+ui->Btn1->height()+20);
    //按下取消按钮则窗口关闭
    connect(Btn2,SIGNAL(clicked()),this,SLOT(close()));

    //按下登陆按钮时,判断账号密码
    connect(ui->Btn1, &QPushButton::clicked, this, &MyWidget::my_slot);

    ui->edit2->setEchoMode(QLineEdit::Password);
    connect(ui->Btn3,SIGNAL(clicked()),this,SLOT(close()));
    //connect(ui->Btn1,&QPushButton::clicked,ui->Btn3,&QPushButton::clicked);
}

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

void MyWidget::my_slot()
{
    if((ui->edit1->text()=="admin")&&(ui->edit2->text()=="123456"))
    {
        ui->Btn1->setText("登陆成功");

        qDebug() << "登陆成功" ;
        this->close();
//        connect(ui->Btn1,SIGNAL(clicked()),this,SLOT(close()));
    }
    else
    {
        ui->Btn1->setText("登陆失败");
        ui->edit2->setText("");
    }
}


void MyWidget::on_Btn1_clicked()
{

}

运行结果:

相关推荐
Eiceblue16 分钟前
通过 C# 将 HTML 转换为 RTF 富文本格式
开发语言·c#·html
故渊ZY17 分钟前
Java 代理模式:从原理到实战的全方位解析
java·开发语言·架构
leon_zeng024 分钟前
Qt Modern OpenGL 入门:从零开始绘制彩色图形
开发语言·qt·opengl
会飞的胖达喵26 分钟前
Qt CMake 项目构建配置详解
开发语言·qt
ceclar12328 分钟前
C++范围操作(2)
开发语言·c++
一个尚在学习的计算机小白28 分钟前
java集合
java·开发语言
IUGEI36 分钟前
synchronized的工作机制是怎样的?深入解析synchronized底层原理
java·开发语言·后端·c#
z***I39441 分钟前
Java桌面应用案例
java·开发语言
来来走走1 小时前
Android开发(Kotlin) LiveData的基本了解
android·开发语言·kotlin
明洞日记1 小时前
【数据结构手册002】动态数组vector - 连续内存的艺术与科学
开发语言·数据结构·c++