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()
{

}

运行结果:

相关推荐
程似锦吖6 分钟前
无中生有 之 从0开始写一个动态定时任务管理
java·开发语言
Dxy123931021634 分钟前
Python 去除 HTML 标签获取纯文本
开发语言·python·html
洛的地理研学1 小时前
Python下载并处理MOD13A3植被指数数据
开发语言·python
humcomm1 小时前
Java 新特性2026年5月速览
java·开发语言
xiao_li_ya1 小时前
C++学习日记1(`*`的理解、const关键词)
开发语言·c++
码力斜杠哥1 小时前
Rust初习录(6)Rust的 if 玩法
开发语言·python·rust
聆风吟º1 小时前
【C标准库】深入理解C语言 isalpha 函数详解:判断字符是否为字母
c语言·开发语言·库函数·isalpha
WL_Aurora2 小时前
Java字符输入全攻略
java·开发语言
茉莉玫瑰花茶2 小时前
LangGraph 拓展核心知识点
开发语言·windows·python
老鱼说AI2 小时前
现代 LangChain 开发指南:从 LCEL 原理到企业级 RAG 与 Agent 实战
java·开发语言·人工智能·深度学习·神经网络·算法·机器学习