QMessageBox如何代码添加按钮并绑定按钮的信号

视频展示效果(结合代码看效果更佳哦,代码在最下面):

QMessageBox手动添加有重试效果的按钮

效果图: 

点击详细文本之后展开如下图:

 

图标可选:

QMessageBox::Critical错误图标
QMessageBox::NoIcon 没有图标
QMessageBox::Question 提问图标
QMessageBox::Information 消息图标
QMessageBox::Warning 警告图标

按钮角色可选:

QMessageBox::InvalidRole 无效;设置之后,这个按钮不会出现在弹框里面
QMessageBox::AcceptRole 确定;设置之后,对话框被接受,点击按钮后,弹窗会消失
QMessageBox::RejectRole取消;设置之后,对话框被拒绝,点击按钮后,弹窗会消失
QMessageBox::DestructiveRole不保存;设置之后,点击按钮后会导致破坏性更改并关闭弹窗
QMessageBox::ActionRole 激活
QMessageBox::HelpRole 帮助
QMessageBox::YesRole
QMessageBox::NoRole
QMessageBox::ApplyRole 应用
QMessageBox::ResetRole 重置


.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMessageBox>
#include <QPushButton>
#include <QTimer>
#include <QDialog>
#define ETIME 10//每次重试的时间
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void slotRetry();
   void slotCountdown();
   void slotClose();
   void slotBtnClick(QAbstractButton* btn);

private:
    Ui::MainWindow *ui;
    QMessageBox *m_mBox;
    QPushButton *m_retryBrn;
    QPushButton *m_closeBtn;
    QTimer *m_tmrRetry;
    int m_iCount;
    int iRetryCount;
};

#endif // MAINWINDOW_H

.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //ui->setupUi(this);

    m_tmrRetry = new QTimer(this);
    m_tmrRetry->setInterval(1000);
    connect(m_tmrRetry,SIGNAL(timeout()),this,SLOT(slotCountdown()));

    m_mBox = new QMessageBox();
    m_mBox->setIcon(QMessageBox::Critical);//设置图标
    m_mBox->setWindowTitle("错误");//设置对话框标题
    m_mBox->setText("测试说明");//设置副标题
    m_mBox->setInformativeText("今天是个好日子");//设置提示说明
    //m_mBox->setDetailedText("点个关注吧");//设置详细文本
    m_retryBrn = new QPushButton();
    m_closeBtn = new QPushButton();

    //添加按钮的方法
    //..1
    m_retryBrn = m_mBox->addButton("重试",QMessageBox::AcceptRole);
    m_closeBtn = m_mBox->addButton("关闭",QMessageBox::RejectRole);

    //..2
    //    m_retryBrn->setText("重试");
    //    m_closeBtn->setText("关闭");
    //    m_mBox->addButton(m_retryBrn,QMessageBox::AcceptRole);
    //    m_mBox->addButton(m_closeBtn,QMessageBox::RejectRole);

    //_________________________________________
    //绑定按钮槽函数的方法
    //..1
    connect(m_mBox,SIGNAL(buttonClicked(QAbstractButton*)),this,SLOT(slotBtnClick(QAbstractButton*)));
    //..2
    connect(m_retryBrn,SIGNAL(clicked(bool)),this,SLOT(slotRetry()));

    //关闭弹窗后触发的信号
    connect(m_mBox,SIGNAL(destroyed(QObject*)),this,SLOT(slotClose()));

    m_mBox->show();
    m_iCount=0;//已经重试了几次
    iRetryCount=0;//总共重试几次
}

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

void MainWindow::slotRetry()
{
    m_mBox->show();//一定要show,不然点击按钮之后,弹窗就会消失,就看不到倒计时的效果了
    if(m_retryBrn->isEnabled())
    {
        m_retryBrn->setEnabled(false);
        m_closeBtn->setEnabled(false);
    }
    iRetryCount++;
    if(iRetryCount>3)//最多只重试3次
    {
        qDebug() << "重试次数已试完";
        m_mBox->close();
        m_mBox->deleteLater();
        return;
    }
    qDebug() << "重试方式一";
    m_tmrRetry->start();
    m_retryBrn->setText(QString("重试 %1").arg(ETIME));
}

void MainWindow::slotCountdown()
{
    m_mBox->show();
    m_iCount++;
    int iTemp = ETIME - m_iCount;
    if(iTemp<=0)
    {
        //可以在倒计时结束后做你想做的事
        m_tmrRetry->stop();
        m_retryBrn->setEnabled(true);
        m_closeBtn->setEnabled(true);
        m_iCount=0;
        iTemp=ETIME;
    }
    m_retryBrn->setText(QString("重试 %1").arg(iTemp));
}

void MainWindow::slotClose()
{
    qDebug() << "弹窗已关闭";
}

void MainWindow::slotBtnClick(QAbstractButton *btn)
{
    if(btn == m_retryBrn)
    {
        qDebug() << "重试 方法2";
    }
    else if(btn == m_closeBtn)
    {
        qDebug() << "关闭 方法2";
    }
}







 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

咸鱼2333号程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值