16,173
社区成员
发帖
与我相关
我的任务
分享
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
Init();
}
void MainWindow::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setPen(QPen(Qt::red));
QPoint origin(0,0);
painter.translate(origin);
painter.drawPath(*path);
QMainWindow::paintEvent(event);
}
void MainWindow::Init()
{
timer = new QTimer;
point = new QPoint;
path = new QPainterPath;
p = t = 0;
connect(ui->startBtn, SIGNAL(clicked()), this, SLOT(StartPaint()) );
connect(ui->stopBtn, SIGNAL(clicked()), this, SLOT(StopPaint()) );
connect(timer, SIGNAL(timeout()), this, SLOT(TimerUpdate()) );
}
void MainWindow::StartPaint()
{
qDebug("start");
timer->start(100);
}
void MainWindow::StopPaint()
{
qDebug("stop");
timer->stop();
p = t = 0;
}
void MainWindow::TimerUpdate()
{
qDebug("update");
t += 10;
//create random number
QTime time;
time= QTime::currentTime();
qsrand(time.msec()+time.second()*1000);
int rand=qrand()%this->height();
//the next point of the painted line
point->setX(t);
point->setY(rand);
path->lineTo(*point);
//
if(t > this->width())
{
p -= 10;
}
//in case update() method is called, the system will trigger paintEvent to repaint the window
this->update();
}
MainWindow::~MainWindow()
{
delete ui;
}