建設(shè)彩票網(wǎng)站合法嗎品牌網(wǎng)絡(luò)營(yíng)銷案例
視頻展示效果(結(jié)合代碼看效果更佳哦,代碼在最下面):
QMessageBox手動(dòng)添加有重試效果的按鈕
效果圖:?
點(diǎn)擊詳細(xì)文本之后展開如下圖:
?
圖標(biāo)可選:
QMessageBox::Critical | 錯(cuò)誤圖標(biāo) |
QMessageBox::NoIcon | 沒(méi)有圖標(biāo) |
QMessageBox::Question | 提問(wèn)圖標(biāo) |
QMessageBox::Information | 消息圖標(biāo) |
QMessageBox::Warning | 警告圖標(biāo) |
按鈕角色可選:
QMessageBox::InvalidRole | 無(wú)效;設(shè)置之后,這個(gè)按鈕不會(huì)出現(xiàn)在彈框里面 |
QMessageBox::AcceptRole | 確定;設(shè)置之后,對(duì)話框被接受,點(diǎn)擊按鈕后,彈窗會(huì)消失 |
QMessageBox::RejectRole | 取消;設(shè)置之后,對(duì)話框被拒絕,點(diǎn)擊按鈕后,彈窗會(huì)消失 |
QMessageBox::DestructiveRole | 不保存;設(shè)置之后,點(diǎn)擊按鈕后會(huì)導(dǎo)致破壞性更改并關(guān)閉彈窗 |
QMessageBox::ActionRole | 激活 |
QMessageBox::HelpRole | 幫助 |
QMessageBox::YesRole | 是 |
QMessageBox::NoRole | 否 |
QMessageBox::ApplyRole | 應(yīng)用 |
QMessageBox::ResetRole | 重置 |
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QMessageBox>
#include <QPushButton>
#include <QTimer>
#include <QDialog>
#define ETIME 10//每次重試的時(shí)間
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic: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);//設(shè)置圖標(biāo)m_mBox->setWindowTitle("錯(cuò)誤");//設(shè)置對(duì)話框標(biāo)題m_mBox->setText("測(cè)試說(shuō)明");//設(shè)置副標(biāo)題m_mBox->setInformativeText("今天是個(gè)好日子");//設(shè)置提示說(shuō)明//m_mBox->setDetailedText("點(diǎn)個(gè)關(guān)注吧");//設(shè)置詳細(xì)文本m_retryBrn = new QPushButton();m_closeBtn = new QPushButton();//添加按鈕的方法//..1m_retryBrn = m_mBox->addButton("重試",QMessageBox::AcceptRole);m_closeBtn = m_mBox->addButton("關(guān)閉",QMessageBox::RejectRole);//..2// m_retryBrn->setText("重試");// m_closeBtn->setText("關(guān)閉");// m_mBox->addButton(m_retryBrn,QMessageBox::AcceptRole);// m_mBox->addButton(m_closeBtn,QMessageBox::RejectRole);//_________________________________________//綁定按鈕槽函數(shù)的方法//..1connect(m_mBox,SIGNAL(buttonClicked(QAbstractButton*)),this,SLOT(slotBtnClick(QAbstractButton*)));//..2connect(m_retryBrn,SIGNAL(clicked(bool)),this,SLOT(slotRetry()));//關(guān)閉彈窗后觸發(fā)的信號(hào)connect(m_mBox,SIGNAL(destroyed(QObject*)),this,SLOT(slotClose()));m_mBox->show();m_iCount=0;//已經(jīng)重試了幾次iRetryCount=0;//總共重試幾次
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::slotRetry()
{m_mBox->show();//一定要show,不然點(diǎn)擊按鈕之后,彈窗就會(huì)消失,就看不到倒計(jì)時(shí)的效果了if(m_retryBrn->isEnabled()){m_retryBrn->setEnabled(false);m_closeBtn->setEnabled(false);}iRetryCount++;if(iRetryCount>3)//最多只重試3次{qDebug() << "重試次數(shù)已試完";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){//可以在倒計(jì)時(shí)結(jié)束后做你想做的事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() << "彈窗已關(guān)閉";
}void MainWindow::slotBtnClick(QAbstractButton *btn)
{if(btn == m_retryBrn){qDebug() << "重試 方法2";}else if(btn == m_closeBtn){qDebug() << "關(guān)閉 方法2";}
}
?