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();
}
相关推荐
编程版小新2 分钟前
C++初阶:STL详解(四)——vector迭代器失效问题
开发语言·c++·迭代器·vector·迭代器失效
c4fx22 分钟前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll
鸽芷咕1 小时前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
Jhxbdks1 小时前
C语言中的一些小知识(二)
c语言·开发语言·笔记
java6666688881 小时前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存1 小时前
源码分析:LinkedList
java·开发语言
代码雕刻家1 小时前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
Fan_web1 小时前
JavaScript高级——闭包应用-自定义js模块
开发语言·前端·javascript·css·html
梦想科研社1 小时前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
风等雨归期1 小时前
【python】【绘制小程序】动态爱心绘制
开发语言·python·小程序