Multi-threaded drawing
I've seen it before. The image drawing device can be used in the thread.
This example. A polygon is randomly drawn in the thread, the picture is emitted by the signal, the picture is accepted in the main thread, and drawn in the drawing event.
There is a button in the window that, when clicked, draws a picture in the thread and then displays the drawn picture in the current window.
Sub-threading Random drawing parsing
Class Mythread:public Qobject
{
void DrawImage (); Self-fitting threading functions
Signals:
void UpdateImage (Qimage temp); Self-fitting signal
}
Implementing threading Functions
void MyThread::D rawimage ()
{
Defining IAMGE Drawing Devices
Qimage image (400,400,QIMAGE::FORMAT_ARGB32);
define painter, specify drawing device
Qpainter p (&image);
Defining brushes
Qpen pen;
Pen.setwidth (5); Brush thickness
Pen.setstyle (qt::solidline);//Brush style
Hand the brush to the painter
P.setpen (pen);
Defining a paint brush
Qbrush Brush;
Brush.setstyle (Qt::solidpattern);//Paint brush style
Brush.setcolor (qt::red); Paint Brush Color
Hand the brush to the painter
P.setbrush (brush);
Randomly generate 5 points to form a polygon
Qpoint arr[]=
{
Qpoint (Qrand ()%400,qrand ()%400),
Qpoint (Qrand ()%400,qrand ()%400),
Qpoint (Qrand ()%400,qrand ()%400),
Qpoint (Qrand ()%400,qrand ()%400),
Qpoint (Qrand ()%400,qrand ()%400)
};
Draw polygons
P.drawpolygon (arr,5);
Send a signal and send the picture
Emit updateimage (image);
}
The main thread accepts the picture and draws the picture parsing
Press the button to invoke the thread's thread handler function
Connect (Ui->pushbutton,&qpushbutton::p ressed,m_mythread,&mythread::D rawimage);
Accept the signal and take out the picture
Connect (m_mythread,&mythread::updateimage,this,&widget::getimage);
void Widget::getimage (Qimage temp)
{
M_image=temp; Remove picture
Update (); Update
}
void Widget::p aintevent (qpaintevent *)
{
Drawing
Qpainter p (this);
P.drawimage (100,100,m_image);
}
Source:
Mythread.h
#ifndef Mythread_h
#define Mythread_h
#include <QObject>
#include <QImage>
Class Mythread:public Qobject
{
Q_object
Public
Explicit MyThread (Qobject *parent = 0);
void DrawImage (); Self-fitting threading functions
Signals:
void UpdateImage (Qimage temp); Self-fitting signal
Public Slots:
};
#endif//Mythread_h
Mythread.cpp
#include "Mythread.h"
#include <QPainter>
#include <QPen>
#include <QBrush>
Mythread::mythread (Qobject *parent):
Qobject (parent)
{
}
Implementing threading Functions
void MyThread::D rawimage ()
{
Defining IAMGE Drawing Devices
Qimage image (400,400,QIMAGE::FORMAT_ARGB32);
define painter, specify drawing device
Qpainter p (&image);
Defining brushes
Qpen pen;
Pen.setwidth (5); Brush thickness
Pen.setstyle (qt::solidline);//Brush style
Hand the brush to the painter
P.setpen (pen);
Defining a paint brush
Qbrush Brush;
Brush.setstyle (Qt::solidpattern);//Paint brush style
Brush.setcolor (qt::red); Paint Brush Color
Hand the brush to the painter
P.setbrush (brush);
Randomly generate 5 points to form a polygon
Qpoint arr[]=
{
Qpoint (Qrand ()%400,qrand ()%400),
Qpoint (Qrand ()%400,qrand ()%400),
Qpoint (Qrand ()%400,qrand ()%400),
Qpoint (Qrand ()%400,qrand ()%400),
Qpoint (Qrand ()%400,qrand ()%400)
};
Draw polygons
P.drawpolygon (arr,5);
Send a signal and send the picture
Emit updateimage (image);
}
Widget.h
#ifndef Widget_h
#define Widget_h
#include <QWidget>
#include "Mythread.h"
#include <QPaintEvent>
#include <QThread>
#include <QImage>
Namespace Ui {
Class Widget;
}
Class Widget:public Qwidget
{
Q_object
Public
Explicit Widget (Qwidget *parent = 0);
~widget ();
void PaintEvent (Qpaintevent *); Drawing events
Public Slots:
void GetImage (Qimage temp); Take out a picture with a signal
Signals:
void Startdraw ();
Private
Ui::widget *ui;
MyThread *m_mythread; Custom Thread Objects
Qthread *m_thread; Child threads
Qimage M_image; Image
};
#endif//Widget_h
Widget.cpp
#include "Widget.h"
#include "Ui_widget.h"
#include <QPainter>
Widget::widget (Qwidget *parent):
Qwidget (parent),
UI (New Ui::widget)
{
UI->SETUPUI (this);
This->resize (600,600);
Custom Class object, no parent object can be specified
M_mythread=new MyThread;
Creating Child Threads
M_thread=new Qthread (this);
Attaching a custom module to a child thread
M_mythread->movetothread (M_thread);
Mode two: Use the button to start the thread, and then send a signal to call the thread handler function
Connect (Ui->pushbutton,&qpushbutton::p ressed,
// [=]()
// {
M_thread->start ();
Emit Startdraw ();
// }
// );
Connect (this,&widget::startdraw,m_mythread,&mythread::D rawimage);
Starts a thread, but does not start a thread-handling function
M_thread->start ();
Press the button to invoke the thread's thread handler function
Connect (Ui->pushbutton,&qpushbutton::p ressed,m_mythread,&mythread::D rawimage);
Accept the signal and take out the picture
Connect (m_mythread,&mythread::updateimage,this,&widget::getimage);
}
Widget::~widget ()
{
Delete UI;
}
void Widget::getimage (Qimage temp)
{
M_image=temp; Remove picture
Update (); Update
}
void Widget::p aintevent (qpaintevent *)
{
Drawing
Qpainter p (this);
P.drawimage (100,100,m_image);
}
30 Thread Drawing