Qt 自定义提示框

新建 msgbox.h 文件,代码如下

#ifndef MSGBOX_H
#define MSGBOX_H

#include <QPushButton>
#include <QLabel>
#include <QMouseEvent>
#include<QDialog>
#include<QTimer>
#include<QDebug>
#include <windows.h>
#include <QWidget>
class MsgBox : public QDialog
{
    Q_OBJECT
public:
    MsgBox(int style,QString text,QWidget *parent = 0);
    ~MsgBox();
public:
    QPushButton *okBtn;
    QPushButton *cancleBtn;
    QPushButton *closeBtn;
    QPushButton * msgBtn;
    QLabel * titleLabel;
    QLabel *askLabel;

protected:
    QPoint move_point;                                    //移动的距离
    bool mouse_press;                                    //鼠标按下
    void mousePressEvent(QMouseEvent *qevent);            //鼠标按下事件
    void mouseReleaseEvent(QMouseEvent *qevent);         //鼠标释放事件
    void mouseMoveEvent(QMouseEvent *qevent);             //鼠标移动事件

public slots:
    void okBtn_press();
    void cancleBtn_press();
    void closeBtn_press();
    void closeWin();
};
#endif // MSGBOX_H

创建msgbox.cpp 文件,代码如下

#include "msgbox.h"

MsgBox::MsgBox(int style, QString text, QWidget *parent):QDialog(parent)
{
    this->resize(332,167);
  //获取主界面的宽度
    int width = this->width();
    int height = this->height();
    setStyleSheet("QWidget{background-color: rgb(219,229,245);}");
  //初始化为未按下鼠标左键

  //设置标题栏隐藏
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    titleLabel = new QLabel(this);
    titleLabel->setGeometry(0,0,width,30);
    if(style==1)
        titleLabel->setText(QStringLiteral("  警告  "));
    else
        titleLabel->setText(QStringLiteral("  提示  "));
    titleLabel->setStyleSheet("QLabel{font-family :微软雅黑;font-size : 5em;color : rgb(255,255,255);background-color: rgb(139,159,185); border-radius:0px;}");

    closeBtn = new QPushButton(this);
    closeBtn->setGeometry(width-30, 6, 20, 20);
    closeBtn->setStyleSheet("QPushButton{image:url(:/images/images/close.png);background: transparent;border:none;}QPushButton:hover{image:url(:/images/images/closeHover.png);background: transparent;border:none;}");

    msgBtn = new QPushButton(this);
    msgBtn->setGeometry(25,56,44,44);
    if(style == 1)      //警告
        msgBtn->setStyleSheet("QPushButton{image:url(:/images/images/warning.png);background: transparent;border:none;}");
    else if(style == 2)  //提示
        msgBtn->setStyleSheet("QPushButton{image:url(:/images/images/information.png);background: transparent;border:none;}");
    else if(style == 3)  //询问
        msgBtn->setStyleSheet("QPushButton{image:url(:/images/images/question.png);background: transparent;border:none;}");
    else if(style == 4)  //错误
        msgBtn->setStyleSheet("QPushButton{image:url(:/images/images/error.png);background: transparent;border:none;}");

    askLabel = new QLabel(this);
    askLabel->setGeometry(95,52,width,50);
    askLabel->setText(text);
    askLabel->setStyleSheet("QLabel{font-family :微软雅黑;font-size:20px;}");

    okBtn=new QPushButton(this);
    if(style==3)
        okBtn->setGeometry(90,119,85,30);
    else
        okBtn->setGeometry(135,119,85,30);

    okBtn->setText(QStringLiteral("确定"));
    okBtn->setStyleSheet("QPushButton{border:1px groove gray;border-radius:4px;border-color: rgb(139,159,185);}QPushButton:hover{border-radius:4px;background-color: rgb(119,146,183);}QPushButton:pressed{border-radius:4px;background-color: rgb(139,159,185);}");

    cancleBtn=new QPushButton(this);
    cancleBtn->setGeometry(187,119,85,30);
    cancleBtn->setText(QStringLiteral("取消"));
    cancleBtn->setStyleSheet("QPushButton{border:1px groove gray;border-radius:4px;border-color: rgb(139,159,185);}QPushButton:hover{border-radius:4px;background-color: rgb(119,146,183);}QPushButton:pressed{border-radius:4px;background-color: rgb(139,159,185);}");
    if(style == 3)
        cancleBtn->show();
    else
        cancleBtn->hide();

    connect(okBtn,SIGNAL(clicked()),this,SLOT(okBtn_press()));
    connect(closeBtn,SIGNAL(clicked()),this,SLOT(closeBtn_press()));
    connect(cancleBtn,SIGNAL(clicked()),this,SLOT(cancleBtn_press()));

}
MsgBox::~MsgBox()
{
    delete okBtn;
    delete cancleBtn;
    delete closeBtn;
    delete msgBtn;
    delete titleLabel;
    delete askLabel;
}

void MsgBox::mousePressEvent(QMouseEvent *qevent)
{
    if(qevent->button() == Qt::LeftButton)
    {
        mouse_press = true;
        //鼠标相对于窗体的位置(或者使用event->globalPos() - this->pos())
        move_point = qevent->pos();;
    }
}
void MsgBox::mouseMoveEvent(QMouseEvent *qevent)
{
    //若鼠标左键被按下
    if(mouse_press)
    {
        //鼠标相对于屏幕的位置
        QPoint move_pos = qevent->globalPos();
        //移动主窗体位置
        this->move(move_pos - move_point);
    }
}
void MsgBox::mouseReleaseEvent(QMouseEvent *qevent)
{
    //设置鼠标为未被按下
    mouse_press = false;
}

void MsgBox::okBtn_press()
{
    this->accept();
}
void MsgBox::cancleBtn_press()
{
    this->reject();
}
void MsgBox::closeBtn_press()
{
    close();
}

调用举例:

//退出提示框
    if(m_msgBox){
        delete m_msgBox;
    }
    m_msgBox=new MsgBox(3,QStringLiteral("正在试图结束程序"),this);
    if(m_msgBox->exec()==0)
    {
        event->ignore();
        return;
    }
// 警告框
    if(m_msgBox){
         delete m_msgBox;
     }
     m_msgBox=new MsgBox(1,QStringLiteral("请选择固件文件"),this);
     m_msgBox->exec();

在这里插入图片描述
在这里插入图片描述

图标资源:

链接:https://pan.baidu.com/s/1BIaY5k394nxavn9BRfWLUQ
提取码:mlfg

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值