QT day3

widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#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 jump();//自定义一个跳转信号

private slots:
    void on_loginbtn_clicked();

    void on_cancelbtn_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 jumpSlot(); //对应第一个窗口信号的槽函数声明

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;

    //将一个窗口的jump信号和第二窗口的jumpSlot槽函数连接
    QObject::connect(&w, &Widget::jump, &s, &second::jumpSlot);
    return a.exec();
}

widget.cpp

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //窗口的相关设置
    this->setWindowTitle("QQ");
    this->setWindowIcon(QIcon(":/pictrue/qq.png"));

    //标签的相关设置
    ui->logolabel->setPixmap(QPixmap(":/pictrue/logo.png"));
    ui->logolabel->setScaledContents(true);

    //账号和密码
    ui->usernamelabel->resize(35,35);
    ui->usernamelabel->setPixmap(QPixmap(":/pictrue/userName.jpg"));
    ui->usernamelabel->setScaledContents(true);

    ui->passwordlabel->resize(35,35);
    ui->passwordlabel->setPixmap(QPixmap(":/pictrue/passwd.jpg"));
    ui->passwordlabel->setScaledContents(true);

    ui->usernamelineEdit->setPlaceholderText("QQ账号/手机号码/邮箱");
   // ui->usernamelineEdit->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
   // ui->usernamelineEdit->adjustSize();
    ui->passwordlineEdit->setEchoMode(QLineEdit::Password);

    //登录按钮和取消登录按钮
    ui->loginbtn->setIcon(QIcon(":/pictrue/login.png"));
    ui->cancelbtn->setIcon(QIcon(":/pictrue/cancel.png"));

    //qt5版本 判断账号是否为admin,密码是否为123456
    connect(ui->loginbtn,&QPushButton::clicked,this,&Widget::on_loginbtn_clicked);

}

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

//登录按钮对应的槽函数
void Widget::on_loginbtn_clicked()
{

    if(ui->usernamelineEdit->text()=="admin" && ui->passwordlineEdit->text()=="123456")
    {
        QMessageBox msg(
                   QMessageBox::Information,//图标
                    "信息对话框",   //消息对话框标题
                    "登录成功",    //对话框文本
                    QMessageBox::Ok, //提供OK按钮
                    this);  //父对象

        //用exec()函数执行对话框
        int ret = msg.exec();
        //对用户选中的按钮进行判断
        if(ret == QMessageBox::Ok)
        {
           this->close();
           emit jump();
        }
    }
    else
    {
        QMessageBox msg(
                    QMessageBox::Critical, //错误图标
                    "错误对话框",    //错误对话框标题
                    "账号和密码不匹配", //错误对话框文本
                    QMessageBox::Yes | QMessageBox::No,  //提供Yes/No按钮
                    this);  //父对象

        //用exec()函数执行对话框
        int ret = msg.exec();
        //对用户选中的按钮进行判断
        if(ret == QMessageBox::Yes)
        {
            //清除密码框中的内容
            ui->passwordlineEdit->setText("");

        }
        else if(ret == QMessageBox::No)
        {
            //关闭登录
            this->close();
        }
    }

}

//取消登录按钮对应的槽函数
void Widget::on_cancelbtn_clicked()
{
    //直接调用静态成员函数
    int ret = QMessageBox::question(this,
                                    "问题对话框",
                                    "您是否确定要退出登录?",
                                    QMessageBox::Yes | QMessageBox::No);

    //对用户选中的按钮进行判断
    if(ret == QMessageBox::Yes)
    {
        this->close();
    }
    else if(ret == QMessageBox::No)
    {

    }

}

second.cpp

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

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

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

void second::jumpSlot()
{
    this->show();
}

相关推荐
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号2 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_2 天前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty2 天前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再2 天前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame