alltolove 发表于 2017-2-17 15:01:03

用QT绘图动态画个正方形

这只是我自己练习写的一个程序,其实做动画,QT有专用的qt quick application创建文件,用QML做出有很流畅效果的动画来。我因为是初学先练一下简单的线程绘图,里面有许多BUG只供大家参考
widget.cpp
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    time = new QTimer(this);

    connect(ui->pushButton,&QPushButton::clicked,this,&Widget::startT);//启动计时器
    initOne();//线程初始化
    connect(this,&Widget::destroyed,this,&Widget::closeThread);//程序退出后关闭进程
}

Widget::~Widget()
{
    delete ui;
}
void Widget::getImage(QImage image)
{
    myImage = image;
    update();
}

void Widget::paintEvent(QPaintEvent *)
   {
    QPainter p(this);
    p.drawImage(50,50,myImage);
   }
void Widget::closeThread()
   {
       thread->quit();
       thread->wait();
       delete myt;

   }
void Widget::initOne()
   {
       myt = new MyThread;//给我新建的继承QObject的类分配空间
       thread = new QThread(this);//创建一个线程对象
       myt->moveToThread(thread);//把我的对象挂载到线程上
       thread->start();//启动线程
       /*通过每秒发送QTimer的timeout信号来调用我线程里画图函数*/
       connect(time,&QTimer::timeout,myt,&MyThread::drawImage);
       /*画图函数里发送的信号来给getImage槽函数传值*/
       connect(myt,&MyThread::updataImage,this,&Widget::getImage);

   }
void Widget::startT()
   {
       time->start(100);
   }


void Widget::on_pushButton_2_clicked()//通过ui界面的转到槽功能来停止计时器
{
    time->stop();
}

mythread.cpp
#include "mythread.h"
#include <Qt>
#include <QPainter>
#include <qmath.h>
MyThread::MyThread(QObject *parent) : QObject(parent)
{


    x1=10,y1=10,x2=10,y2=10;

    points=QPoint(10,10);
    points=QPoint(10,90);



}
void MyThread::drawImage()
{

    QImage image(100,100,QImage::Format_ARGB32);
    QPen pen(Qt::SolidLine);
    pen.setColor(Qt::red);
    pen.setWidth(5);
    p = new QPainter(&image);
    p->setPen(pen);
    drawLine();
    emit updataImage(image);
    delete p;

}
/*以下画方形的代码BUG很多
* 我只是瞎写的~
*/
void MyThread::drawLine()
{
    if(y2<90&&x2==10)
    {

      p->drawLine(x1,y1,x2,y2);
      y2++;

    }else if(x2<90&&y2>10)
    {
       p->drawPolyline(points,2);
       p->drawLine(10,90,x2,y2);
      x2++;
    }else if(y2>10&&x2==90)
    {
      points = QPoint(90,90);
      p->drawPolyline(points,3);
      p->drawLine(90,90,x2,y2);
      y2--;
    }else
    {
      points = QPoint(90,10);
      p->drawPolyline(points,4);
      p->drawLine(90,10,x2,y2);
      x2--;
    }
}


alltolove 发表于 2017-2-17 15:02:59

widget.h
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "mythread.h"
#include <QThread>
#include <QEvent>
#include <QPainter>
#include <QTimer>
namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
    MyThread *myt;
    QThread *thread;
    QImage myImage;
    QTimer *time;
    void getImage(QImage image);
    void getImage2(QImage image);
    void closeThread();
    void initOne();
    void startT();
protected:
    void paintEvent(QPaintEvent *);
private slots:
    void on_pushButton_2_clicked();
};

#endif // WIDGET_H


mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QObject>
#include <QImage>
#include <QPen>
#include <QPainter>
#include <QMutex>

class MyThread : public QObject
{
    Q_OBJECT
public:
    explicit MyThread(QObject *parent = 0);
    void drawImage();

signals:
    void updataImage(QImage image);
private:

    QPainter *p;
    int x1,y1,x2,y2;
    QPoint points[];

    void drawLine();


public slots:
};

#endif // MYTHREAD_H

qq413317226 发表于 2017-3-14 09:51:43

跟着楼主学qt
页: [1]
查看完整版本: 用QT绘图动态画个正方形