如何让我的Qpainter滴答时钟动画在按下停止按钮后回到原来的位置?

时间:2017-03-11 15:11:40

标签: qt qpainter qtimer

所以我一直为我们的最终项目制作这个计划。它是一个秒表,我添加了一个滴答动画,每秒钟滴答一次。我唯一的问题是在我按下停止按钮后,它没有回到原来的位置。这是代码:

#include "dialog.h"
#include "ui_dialog.h"
#include <QTimer>
#include <QPainter>
#include <QMessageBox>

Dialog::Dialog(QWidget *parent) :
  QDialog(parent),
  ui(new Ui::Dialog)
{
  ui->setupUi(this);
  timer = new QTimer(this);  
  connect(timer,SIGNAL(timeout()),this,SLOT(update()));
  connect(timer,SIGNAL(timeout()),this,SLOT(tick()));
  sec = 0;
  sec2 = 0;
  sec3 = 0;
  setUpdatesEnabled(this);
}

Dialog::~Dialog()
{
  delete ui;
}

void Dialog::on_start_clicked()
{
  timer->start(10);
}

void Dialog::on_stop_clicked()
{
  sec = 0;
  sec2 = 0;
  sec3 = 0;
  timer->stop();
  ui->label->setNum(sec);
  ui->label_2->setNum(sec2);
  ui->label_3->setNum(sec3);
}

void Dialog::on_record_clicked()
{
  if(sec == 0 && sec2 == 0 && sec3 == 0) {
    QMessageBox msgbox;
    msgbox.setText("Cannot record a zero");
    msgbox.exec();
  }
  else
    ui->listWidget->addItem(
      ui->label_3->text() + ":" + ui->label_2->text() 
      + ":" + ui->label->text());
}

void Dialog::on_clear_clicked()
{
  ui->listWidget->clear();
}

void Dialog::tick()
{
  sec++;
  sec2++;
  sec3++;
  float x = sec * 0.01;
  int y = sec2 / 6000;
  int z = sec3 / 360000;
  ui->label->setNum(x);
  if(x == 60) {
    sec = 0;
  }
  ui->label_2->setNum(y);
  if(y == 60) {
    sec2 = 0;
  }
  ui->label_3->setNum(z);
}

void Dialog::paintEvent(QPaintEvent *event)
{
  QColor minuteColor(0, 127, 127, 191);
  int side = qMin(width(), height());

  painter = new QPainter(this);
  painter->setRenderHint(QPainter::Antialiasing);
  painter->translate(width() / 2, height() / 2);
  painter->scale(side /350.0, side / 350.0);

  painter->setPen(QPen(minuteColor));
  painter->drawArc(QRect(-95, -95, 190, 190),90 * 16, 360 * 16);

  painter->setPen(minuteColor);
  for (int j = 0; j < 60; j++) {
    painter->drawLine(92, 0, 96, 0);
    painter->rotate(6.0);
  }

  painter->setPen(QPen(QColor(127, 0, 127,191),5));
  painter->rotate(6.0 * sec * 0.01);
  painter->drawLine(0, -92, 0, -96);
}

0 个答案:

没有答案