Qt 12.28 day3

作业:

1】 思维导图

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

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

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

3】 自己完成一个使用qss的登陆窗口界面【资源文件的添加、qss语法】。

已更新12.29题目为新增功能代码

untitled.pro

cpp 复制代码
QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

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 \
    second.cpp \
    widget.cpp

HEADERS += \
    second.h \
    widget.h

FORMS += \
    second.ui \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    ppppp.qrc

widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPushButton>
#include <QMessageBox>


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_jump();//第一个界面准备的信号


private slots:
    void on_pushButton_2_clicked();

    void on_pushButton_clicked();

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

second.h

cpp 复制代码
#ifndef SECOND_H
#define SECOND_H

#include <QWidget>

namespace Ui {
class Second;
}

class Second : public QWidget
{
    Q_OBJECT

public:
    explicit Second(QWidget *parent = nullptr);
    ~Second();

public slots:
    void jump_slot();//第二个界面准备的槽函数声明

private:
    Ui::Second *ui;
};

#endif // SECOND_H

main.cpp

cpp 复制代码
#include "widget.h"
#include "second.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    //实例化第二界面
    Second s;
    QObject::connect(&w,&Widget::my_jump,&s,&Second::jump_slot);
    return a.exec();
}

widget.cpp

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)

{
    ui->setupUi(this);
    //去掉标题,纯净模式
    this->setWindowFlag(Qt::FramelessWindowHint);
    //去掉空白部分
    this->setAttribute(Qt::WA_TranslucentBackground);
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(my_slot()));
}

Widget::~Widget()
{
    delete ui;
}
//基于静态成员函数版本
void Widget::on_pushButton_2_clicked()
{
    int ret = QMessageBox::question(this,
                                    "问题对话框",
                                    "您是否确认要退出登录?",
                                    QMessageBox::Yes|QMessageBox::No
                                    );
    if(ret==QMessageBox::Yes)
    {
        this->close();
    }
    else{

    }
}

void Widget::on_pushButton_clicked()
{
    if(ui->lineEdit->text()=="admin"&&ui->lineEdit_2->text()=="123456")
    {

        //基于基本属性版本
        QMessageBox msg(QMessageBox::Information,
                        "信息对话框",
                        "登录成功",
                        QMessageBox::Ok,
                        this);
        //调用exec()弹出信息对话框
        int ret  = msg.exec();
        //
        if(ret == QMessageBox::Ok)
        {
            this->close();
            emit my_jump();

        }

    }else
    {
        //基于静态成员函数版本
        int ret = QMessageBox::critical(this,
                                        "错误对话框",
                                        "账号和密码不匹配,是否重新登录",
                                        QMessageBox::Yes|QMessageBox::No,
                                        QMessageBox::Yes);
        if(ret == QMessageBox::Yes)
        {
            ui->lineEdit->clear();
            ui->lineEdit_2->clear();
        }
        else{
            this->close();

        }
        qDebug()<<"登录失败";
    }
}

second.cpp

cpp 复制代码
#include "second.h"
#include "ui_second.h"

Second::Second(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Second)
{
    ui->setupUi(this);
    //去掉标题,纯净模式
    this->setWindowFlag(Qt::FramelessWindowHint);
    //去掉空白部分
    this->setAttribute(Qt::WA_TranslucentBackground);
}

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

//第二个界面来准备的槽函数实现
void Second::jump_slot()
{
    //显示
    this->show();
}


相关推荐
布朗克16814 分钟前
34 JVM深入理解
java·jvm
Flittly22 分钟前
【AgentScope Java新手村系列】(4)结构化输出
java·spring boot·spring·ai
wzg19690226wzg23 分钟前
rust 学习 泛型
开发语言·学习·rust
techdashen25 分钟前
Rust 基础设施团队 2025 Q4 回顾与 2026 Q1 计划
开发语言·后端·rust
红宝村村长25 分钟前
torch.autograd.Function.apply()
开发语言·python
AI科技星26 分钟前
《数术工坊:非欧射影录》类型:硬核光影·几何本源
c语言·开发语言·网络·量子计算·agi
何以解忧,唯有..34 分钟前
Python 中的继承机制:从基础到高级用法详解
java·开发语言·python
Yiyaoshujuku39 分钟前
化合物数据集API接口(数据结构及样例)
java·网络·数据结构
plainGeekDev1 小时前
算法刷题笔记:一维DP没那么难,状态想清楚就赢了一半
java·算法·面试
IceBing1 小时前
还在一个个连接 Arthas?这个开源平台支持批量诊断 JVM
java