• Qt 自定义提示框 类似QMessageBox


    前言

    为什么需要设计自定义提示框呢?
    1.Qt自带的提示框样式单一;
    2.提示框的太小;
    3.界面风格跟项目的不搭配;

    程序执行效果

    在这里插入图片描述

    源码下载地址

    https://gitee.com/jiang_bin_yu/qt---custom-prompt-box
    
    • 1

    程序源码

    使用说明
    参数info:输入你需要显示的提示内容
    参数parent:指定此对话框的父类,即窗口会根据父类窗口大小自适应
    参数exec:是否使用模态方式显示
    参数confirmName:确认按钮显示的文本,默认是确 定

        //弹出消息框
        void showMessageBoxInfo(const QString &info,QWidget *parent,bool exec = true,const QString &confirmName="确 定");
    
    • 1
    • 2

    程序应用

    //显示提示框
    void MainWindow::on_pushButton_showInfo_clicked()
    {
        myDialog::Instance()->setAnimationDirection(QtMaterialJbyDialog::UPTODOWN);
        myDialog::Instance()->showMessageBoxInfo("通过单例方式显示的\r\n提示框",this,true);
    }
    //显示询问框
    void MainWindow::on_pushButton_showQuestion_clicked()
    {
        myDialog::Instance()->setAnimationDirection(QtMaterialJbyDialog::RIGHTTOLEFT);
        int dlg = myDialog::Instance()->showMessageBoxQuestion("是否看懂了\r\n此程序",this,true,"简 单!","讲个der~?");
        if(dlg == QMessageBox::Yes)
        {
            qDebug() << "用户看懂了";
        }
        if(dlg == QMessageBox::No)
        {
            qDebug() << "用户是个老六 没看懂";
        }
    }
    //显示警示框
    void MainWindow::on_pushButton_showError_clicked()
    {
        myDialog::Instance()->setAnimationDirection(QtMaterialJbyDialog::LEFTTORIGHT);
        myDialog::Instance()->showMessageBoxError("通过单例方式显示的\r\n提示框",this,true);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    输入框继承说明
    myDialog继承QtMaterialJbyDialog
    QtMaterialJbyDialog继承QtMaterialOverlayWidget
    QtMaterialOverlayWidget继承QWidget在这里插入图片描述
    输入框各父类对应功能讲解
    1.QtMaterialOverlayWidget 主要实现窗口与父类窗口大小一致

    bool QtMaterialOverlayWidget::event(QEvent *event)
    {
        if (!parent()) {
            return QWidget::event(event);
        }
        switch (event->type())
        {
        case QEvent::ParentChange:
        {
            parent()->installEventFilter(this);
            setGeometry(overlayGeometry());
            break;
        }
        case QEvent::ParentAboutToChange:
        {
            parent()->removeEventFilter(this);
            break;
        }
        default:
            break;
        }
        return QWidget::event(event);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2.QtMaterialJbyDialog 主要负责界面动画
    动画1:界面背景模板的淡入淡出

    void QtMaterialJbyDialog::paintEvent(QPaintEvent *event)
    {
        Q_UNUSED(event)
        QPainter painter(this);
    
        QBrush brush;
        brush.setStyle(Qt::SolidPattern);
        brush.setColor(Qt::black);
        painter.setBrush(brush);
        painter.setPen(Qt::NoPen);
        painter.setOpacity(m_opacity);        //值越高越不透明
        painter.drawRect(rect());
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    Q_PROPERTY(qreal opacity WRITE setOpacity READ opacity)
    
    • 1

    动画2:窗口移动

    	//四种动画模式
        enum ANIMATIONDIRECTION
        {
            UPTODOWN = 0,
            DOWNTOUP = 1,
            RIGHTTOLEFT = 2,
            LEFTTORIGHT = 3
        };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
     	showAnimation = new QPropertyAnimation(mainWidget, "geometry",this);
        showAnimation->setDuration(AnimationTime);
        showAnimation->setEasingCurve(QEasingCurve::InCirc);
    
        if(hideAnimation)
            delete hideAnimation;
        hideAnimation = new QPropertyAnimation(mainWidget, "geometry",this);
        hideAnimation->setDuration(AnimationTime);
        hideAnimation->setEasingCurve(QEasingCurve::InCirc);
        connect(hideAnimation, SIGNAL(finished()),this, SLOT(slotHideAnimationFinish()));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
        if(m_animationDirection == UPTODOWN)
        {
            hideAnimation->setStartValue(QRect((mainRect.width()-rect.width())/2, (mainRect.height()-rect.height())/2,rect.width(), rect.height()));
            hideAnimation->setEndValue(QRect((mainRect.width()-rect.width())/2,0,rect.width(), rect.height()));
        }else if(m_animationDirection == DOWNTOUP)
        {
            hideAnimation->setStartValue(QRect((mainRect.width()-rect.width())/2, (mainRect.height()-rect.height())/2,rect.width(), rect.height()));
            hideAnimation->setEndValue(QRect((mainRect.width()-rect.width())/2,mainRect.height()-rect.height(),rect.width(), rect.height()));
        }else if(m_animationDirection == RIGHTTOLEFT)
        {
            hideAnimation->setStartValue(QRect((mainRect.width()-rect.width()), (mainRect.height()-rect.height())/2,rect.width(), rect.height()));
            hideAnimation->setEndValue(QRect(mainRect.width(),(mainRect.height()-rect.height())/2,rect.width(), rect.height()));
        }else if(m_animationDirection == LEFTTORIGHT)
        {
            hideAnimation->setStartValue(QRect(0, (mainRect.height()-rect.height())/2,rect.width(), rect.height()));
            hideAnimation->setEndValue(QRect(-rect.width(),(mainRect.height()-rect.height())/2,rect.width(), rect.height()));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    myDialog 负责提示框的显示

    int myDialog::showDialog(const QString &info, QWidget *parent, bool exec, const QString &confirmName, const QString &cancleName)
    {
        if(info == m_Message)
            return QMessageBox::Cancel;
        if(exec && !this->isHidden())
        {
            qDebug() << "showMessageBoxInfo 已经有一个了";
            myDialog *dlg = new myDialog();
            return dlg->showMessageBoxQuestion(info,parent,exec);
        }
        setParent(parent);
        m_Message = info;
        ui->pushButton_confirm->setText(confirmName);
        ui->pushButton_cancle->setText(cancleName);
        ui->pushButton_confirm->show();
        ui->pushButton_cancle->show();
    
        ui->label_Tip->setText(info);
        update();
        if(exec)
        {
            m_Message.clear();
            QtMaterialJbyDialog::exec();
        }
        else
        {
            QtMaterialJbyDialog::showDialog();
        }
        return DlgState();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    特别需要注意的地方
    myDialog 使用方法
    1.设置动画窗口

    QtMaterialJbyDialog::->setMainWidget(ui->widget_main);
    
    • 1

    2.设置动画方向

    QtMaterialJbyDialog::setAnimationDirection(QtMaterialJbyDialog::UPTODOWN);
    
    • 1

    3.设置父类

    setParent(parent);
    
    • 1
  • 相关阅读:
    Redisson 集成SpringBoot 详解
    Day 03 python学习笔记
    【Prism系列】Prism子窗口实现
    JavaScript -- 02. 变量和数据类型
    【附源码】计算机毕业设计JAVA郑工社团交流服务信息平台
    基于金豺算法的无人机航迹规划-附代码
    高性能MySQL实战第02讲:深入理解事务与锁机制(上)
    vue-router(vue-router功能,跳转方式,路由守卫,路由懒加载,使用流程,3.x和4.x使用区别)
    四大竞争对手敦促欧盟反垄断行动:阻止谷歌成为默认搜索引擎
    (附源码)ssm学生选课系统 毕业设计 170920
  • 原文地址:https://blog.csdn.net/qq_37373742/article/details/127402276