Qt消息对话框的实现

Widget.cpp

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

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this -> setWindowIcon(QIcon(":/picture/hp.jpg"));//设置窗口图标
    this -> setWindowFlag(Qt::FramelessWindowHint, 1);//无边框
    setAttribute(Qt::WA_TranslucentBackground);//窗口透明
//    this->setStyleSheet("border-radius:30px;");
    this->setAttribute(Qt::WA_TranslucentBackground);
}

Widget::~Widget()
{

    delete ui;
}


void Widget::on_pushButton_clicked()
{
    if(ui -> ac_line -> text() == "admin"){
        if(ui -> pa_line -> text() == "123456"){
            succes_login();
        }else if(ui -> pa_line -> text() == ""){
            QMessageBox msg(QMessageBox::Warning,//图标
                            "错误",//标题
                            "请输入密码",//文本
                            QMessageBox::Yes | QMessageBox::No,//提供按钮
                            this//指定父组件
                            );
//            msg.setStyleSheet("background-color: white; border: 2px solid black; "
//                              "QMessageBox::ButtonBox { margin-top: 10px; }"
//                              "QMessageBox QPushButton { margin: 5px; }");
            msg.setStyleSheet("background-color: rgb(164, 164, 255);");
            msg.setWindowFlag(Qt::FramelessWindowHint, 1);

            msg.button(QMessageBox::Yes)->setStyleSheet("background-color: rgb(40, 40, 40);color:white");
            msg.button(QMessageBox::No)->setStyleSheet("background-color: white");

//            this -> setWindowIcon(QIcon(":/picture/hp.jpg"));//设置窗口图标
//            this -> setWindowFlag(Qt::FramelessWindowHint, 1);//无边框
//            setAttribute(Qt::WA_TranslucentBackground);//窗口透明
                    //(QMessageBox{background-color: white; border: 2px solid black; }
//            QMessageBox::ButtonBox { margin-top: 10px; }
//            QMessageBox QPushButton { margin: 5px; });

            int ret = msg.exec();//弹出对话框
            //根据用户的选择,执行不同的功能
            if(QMessageBox::No == ret){
                this -> close();
            }
        }else{
//            ui -> pa_line -> setText("");
            ui -> pa_line -> clear();
            QMessageBox msg(QMessageBox::Warning,//图标
                            "错误",//标题
                            "密码错误, 是否重新登录",//文本
                            QMessageBox::Yes | QMessageBox::No,//提供按钮
                            this//指定父组件
                            );
            int ret = msg.exec();//弹出对话框
            //根据用户的选择,执行不同的功能
            if(ret == QMessageBox::No){
                this -> close();
            }
        }
    }else if(ui -> ac_line -> text() == ""){
        QMessageBox::information(this, "信息", "请填写账号密码", QMessageBox::Ok);
        ui -> pa_line -> setText("");
    }
}

void Widget::succes_login()
{
    this -> close();
    emit my_jump();
}

chat.cpp

复制代码
#include "chat.h"
#include "ui_chat.h"

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

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

void chat::on_pushButton_9_clicked()
{
    //弹出第一个对话框 QFontDialog
    bool OK;
    QFont ft = QFontDialog::getFont(&OK, QFont("黑体", 14, 2), this);
    //判断用户
    if(OK){
        ui -> textEdit -> setCurrentFont(ft);
    }else{
        QMessageBox::information(this, "","未选择正确的字体");
    }
}

void chat::jump_slot()
{
        this -> show();
}

main.cpp

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

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

    chat c;
    QObject::connect(&w, &Widget::my_jump, &c, &chat::jump_slot);

    return a.exec();
}
相关推荐
还债大湿兄1 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
倔强青铜33 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian4 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼4 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上4 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang4 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc4 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
AI视觉网奇4 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀5 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
liulilittle5 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程