趣玩-Qt高精度千分位秒表

写这个小软件的原因:

          要找个PC可以用的带有千分位高精度秒表,要求以120fps录制视频,要求每帧的时间都不一样,网上找了一圈,没有找到符合自己要求的软件。既然找不到,那就自己用Qt写呗。

软件界面演示:

软件说明:

基于Qt的高精度千分位秒表,界面简洁美观:

  1. 秒表时间刷新频率可调,1-2000ms,鼠标滚动调节
  2. 极简设计,就3个控制按钮,开始计时/停止计时/清除计时
  3. Key-Space 快捷键可以开始暂停/计时
  4. 使用帮助信息,直接鼠标悬停在时间控件上就可以获取
  5. 资源包包含Qt源码及Windows可以直接跑的exe文件(下载地址:https://download.csdn.net/download/wllw7176/88510525

主要代码:

#include "MyStopWatch.h"

MyStopWatch::MyStopWatch(QWidget *parent)
    :QWidget(parent)

{
    //qss settings
    QString qss_str;

    QFont font("Microsoft YaHei", 40, 25);
    QFont timefont("Microsoft YaHei", 120, 100);

    //更新定时器设置
    pTimer_ = new QTimer;
    update_interval_ms = 1;
    pTimer_->setInterval(update_interval_ms);
    connect(pTimer_, SIGNAL(timeout()), this, SLOT(updatedisplay()));

    //定时时间显示标签设置
    timelabel_ = new  QLabel;
    timelabel_->setGeometry(20, 20, 140, 400);
    timelabel_->setFont(timefont);
    timelabel_->setText("00 : 00 : 00 : 000");
    timelabel_->setAlignment(Qt::AlignCenter);
    QString verion_str;
    QTextStream(&verion_str) << "app_version:" <<  MY_STOP_WATCH_MAJOR_VERSION << "." << MY_STOP_WATCH_MINOR_VERSION;
    timelabel_->setToolTip(QString("鼠标滚轮调整时间更新周期,向上增加,向下减小,周期范围为[1-2000]ms\nKey-Space 可以暂停/开始计时\n").append(verion_str));

    //控制按钮设置
    startbtn_ = new QPushButton;
    startbtn_->setFont(font);
    startbtn_->setText(QStringLiteral("开始计时"));
    startbtn_->setToolTip(QString("有问题请联系717628525@qq.com,没问题就不要联系了,联系也不回>_<"));
    startbtn_->setFocusPolicy(Qt::NoFocus);
    connect(startbtn_, SIGNAL(clicked()), this, SLOT(startstopwatch()));

    stopbtn_ = new QPushButton;
    stopbtn_->setFont(font);
    stopbtn_->setText(QStringLiteral("停止计时"));
    stopbtn_->setToolTip(QString("点了我,时间停止更新,不想停就不要点我^_^"));
    stopbtn_->setFocusPolicy(Qt::NoFocus);
    connect(stopbtn_, SIGNAL(clicked()), this, SLOT(stopstopwatch()));

    cleanbtn_ = new QPushButton;
    cleanbtn_->setFont(font);
    cleanbtn_->setText(QStringLiteral("清除计时"));
    cleanbtn_->setToolTip(QString("点了我,时间清零,一切从头再来V_V"));
    cleanbtn_->setFocusPolicy(Qt::NoFocus);
    connect(cleanbtn_, SIGNAL(clicked()), this, SLOT(clear()));

    //整体布局设置
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setMargin(10);
    mainLayout->setSpacing(20);
    mainLayout->addWidget(timelabel_);
    mainLayout->addWidget(startbtn_);
    mainLayout->addWidget(stopbtn_);
    mainLayout->addWidget(cleanbtn_);
    setLayout(mainLayout);

    //窗口Icon及标题设置
    setWindowIcon(QIcon(":/logo/stop_watch.ico"));

    win_title_str = QString("秒表计时系统|时间刷新周期:").append(QString::number(update_interval_ms).append("ms"));
    setWindowTitle(win_title_str);

    //Qss美化设置
    //timelabel_ qss设置
    qss_str = "\
        QLabel{ \
            border:2px solid rgb(255, 0, 0);\
            color: rgb(0,0,0); \
            background-color:rgb(137,209,224); \
            border-radius:24;\
        }";
    timelabel_->setStyleSheet(qss_str);
    //按钮Qss设置
    qss_str = "\
        QPushButton{ \
            border:2px solid rgb(255, 166, 247); \
            border-radius:8; \
            background-color:rgb(137,209,224);\
        }\
        /* 鼠标按下时的效果 */ \
        QPushButton:pressed {\
            background-color:rgb(255,255,157);\
            /* 改变边框风格 */ \
            border-style:inset; \
            /* 使文字有一点移动 */ \
            padding-left:6px;\
            padding-top:6px;\
        }";

    setStyleSheet(qss_str);

    setGeometry(0, 0, 1280, 720);

    time_ms_timer = 0;
    //Install key press event filter
    //key_preess_filter = new KeyPressEater(pTimer_);
    //installEventFilter(key_preess_filter);

}

MyStopWatch::~MyStopWatch()
{

}

//Mouse wheel to adust timelabl update interval
void MyStopWatch::wheelEvent(QWheelEvent *event)
 {
      if (event->angleDelta().y() > 0) { //鼠标滚轮向上滚动,增加时间更新周期
        update_interval_ms = update_interval_ms + 1;
        if (update_interval_ms > 2000) {
            update_interval_ms = 2000;
        }
      } else {//鼠标滚轮向上滚动,减少时间更新周期
        update_interval_ms = update_interval_ms - 1;
        if (update_interval_ms <= 0) {
            update_interval_ms = 1;
        }
      }

      //用新的周期重新开始定时器
      pTimer_->stop();
      pTimer_->setInterval(update_interval_ms);
      pTimer_->start();

      win_title_str = QString("秒表计时系统,时间刷新周期:").append(QString::number(update_interval_ms).append("ms"));
      setWindowTitle(win_title_str);

      event->accept();
}

//Space key to pause and coninue update_timer
void MyStopWatch::keyPressEvent(QKeyEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
        if (keyEvent->key() == Qt::Key_Space) {
            if (pTimer_->isActive()) {
                pTimer_->stop();
                previous_time_ms_timer = time_ms_timer;
            } else {
                time_ms_timer = previous_time_ms_timer;
                pTimer_->start();
            }
        }
    }

}

void MyStopWatch::startstopwatch()
{
    pTimer_->start();
}

void  MyStopWatch::updatedisplay()
{
    QTime showtime(00 , 00 , 00 , 000);
    time_ms_timer = time_ms_timer + pTimer_->interval();
    showtime = showtime.addMSecs(time_ms_timer);
    timeStr_ = showtime.toString("hh : mm : ss : zzz");
    timelabel_->setText(timeStr_);
}

void MyStopWatch::stopstopwatch()
{
    pTimer_->stop();
    previous_time_ms_timer = time_ms_timer;
}

void MyStopWatch::clear()
{
    timelabel_->setText("00 : 00 : 00 : 000");
    time_ms_timer = 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值