QT建立简单的计时器程序

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.点击开始运行:





猜你喜欢

转载自www.cnblogs.com/sinceret/p/9947379.html