• QT开发——QMessageBox添加倒计时


    QMessageBox添加倒计时,一定时间内选择确定或者取消,否则倒计时结束,默认选择确定

     

     直接上代码

    main.cpp

    #include "widget.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Widget w;
        w.show();
        w.resize(800,600);
        return a.exec();
    }

    widget.h

    #ifndef WIDGET_H
    #define WIDGET_H
    
    #include <QWidget>
    #include <QTimer>
    #include <QPushButton>
    #include <QMessageBox>
    #include <QDebug>
    #include <iostream>
    class Widget : public QWidget
    {
      Q_OBJECT
    
    public:
      Widget(QWidget* parent = 0);
      ~Widget();
    
    private:
    
      QTimer*      timer_countdown;
      QPushButton* btn;
      QMessageBox* msgbox;
      QPushButton* okbtn;
      QPushButton* cancelbtn;
      double       msgbox_time_;
    public slots:
      void count_time();
      void btnclick();
    };
    #endif // WIDGET_H

    widget.cpp

    #include "widget.h"
    #define TOTAL_TIME 8.0
    #define INTERVAL 0.1
    Widget::Widget(QWidget* parent)
      : QWidget(parent)
    {
      btn = new QPushButton("点我一下", this);
      btn->setFixedSize(100, 50);
    
      msgbox = new QMessageBox(this);
      msgbox->setGeometry(QRect(200, 150, 400, 300));
      okbtn     = new QPushButton("确定");
      cancelbtn = new QPushButton("取消");
      msgbox->addButton(okbtn, QMessageBox::AcceptRole);
      msgbox->addButton(cancelbtn, QMessageBox::RejectRole);
      msgbox->setText(tr("确定需要发送任务?"));
      msgbox->setStyleSheet("QPushButton {min- 5em; min-height: 2em;}");
    
      timer_countdown = new QTimer(this);
      connect(timer_countdown, SIGNAL(timeout()), this, SLOT(count_time()));
      connect(btn, SIGNAL(clicked()), this, SLOT(btnclick()));
    }
    
    Widget::~Widget()
    {
    }
    
    void Widget::count_time()
    {
      double letf_time = TOTAL_TIME - msgbox_time_;
      if( letf_time <= 0 )
      {
        okbtn->click();
        return;
      }
      else
      {
        QString output;
        output.sprintf("%2.1f秒 后发送任务 ", letf_time);
        msgbox->setText(output);
      }
      msgbox_time_ += INTERVAL;
    }
    
    void Widget::btnclick()
    {
      timer_countdown->start(INTERVAL*1000);
      msgbox_time_ = 0.0;
      msgbox->exec();
      if (msgbox->clickedButton() == okbtn)
      {
        qDebug()<<"确定被按下了";
        timer_countdown->stop();
      }
      else
      {
        qDebug()<<"取消被按下了";
        timer_countdown->stop();
      }
    }





  • 相关阅读:
    yum安装8.0mysql数据库
    free命令详细介绍
    linux 自定义美女欢迎界面
    shll脚本常用格式和规则使用
    liunx常用知识基本命令大全
    liunx系统二进制包安装编译mysql数据库
    CentOS7更改网卡名称
    老男孩教育100道面试题
    非关系型数据库(NoSQL)
    iptables
  • 原文地址:https://www.cnblogs.com/zx-hit/p/11957335.html
Copyright © 2020-2023  润新知