1.创建一个窗口项目

2.拖一个lcd控件命名lcdNumber,两个pushbutton,分别命名pbStart与pbEnd

3.mainWindow.h添加以下代码

private slots:
void updateTime200();
private:
QTimer * timer200;//200ms更新
int steps;//记录值
bool isStart; //记录是否已经开始计时
4.mainWindow.cpp中添加代码
  4.1在构造函数中添加:
    isStart = false;     //初始为还未计时
timer200=new QTimer;
steps=0;
    ui->lcdNumber->setDigitCount(8);
ui->lcdNumber->setSegmentStyle(QLCDNumber::Flat);
ui->lcdNumber->display("0");

ui->pbEnd->setEnabled(false);
connect(timer200,SIGNAL(timeout()),this,SLOT(updateTime200()));
  
  4.2添加函数
void MainWindow::updateTime200()
{
steps++;
ui->lcdNumber->display(QString("%1").arg(steps));
}

  4.3在pbStart与pbEnd上点击右键,选择“编辑槽函数”
void MainWindow::on_pbStart_clicked()
{
if(!isStart) //尚未开始 开始计时
{
timer200->start(200);
ui->pbStart->setDisabled(true);
ui->pbEnd->setEnabled(true);
}
isStart = !isStart;
}
void MainWindow::on_pbEnd_clicked()
{
ui->pbEnd->setDisabled(true);
ui->pbStart->setEnabled(true);
timer200->stop();
steps=0;
ui->lcdNumber->display("0");
isStart = false;
}
5.点击开始运行:

QT建立简单的计时器程序

 

6.下载地址,https://download.csdn.net/download/sinceret/10965200


相关文章:

  • 2021-10-16
  • 2021-07-01
  • 2021-09-21
  • 2022-12-23
  • 2021-09-18
  • 2022-02-04
  • 2021-09-07
  • 2022-12-23
猜你喜欢
  • 2021-05-23
  • 2021-12-13
  • 2022-12-23
  • 2022-12-23
  • 2021-10-22
  • 2021-09-23
  • 2021-12-10
相关资源
相似解决方案