PDA

View Full Version : Freeze_drawing



prashantbasvat
24th June 2015, 06:28
in my application i am drawing sine-wave using qpainter widget.now i have to implement such functionality that on button pressed wave drawing should be stopped at that instant. and when i pressed same button again it should resume wave drawing from last instant.how to implement freeze functionalit
drawthread.cpp


#include "drawthread.h"
#include <QtCore>
#include <QtGui>

DrawThread::DrawThread(QObject *parent):QThread(parent)
{
X1=XStartpoint=50;
Y1=YStartpoint=150;
amplitude = 10;
Ttime = 5;
}

void DrawThread::run()
{
while(1)
{
qreal tempAmp;


tempAmp=amplitude;

for(int a=1;a<amplitude;a++)
{
XStartpoint = X1;
YStartpoint = Y1;

X1+=Ttime/tempAmp;
tempAmp--;
Y1-=1;

emit DrawLine(XStartpoint,YStartpoint,X1,Y1);
}

tempAmp=1;
for(int g=amplitude;g>0;g--)
{
XStartpoint = X1;
YStartpoint = Y1;

X1+=Ttime/tempAmp;
tempAmp++;
Y1+=1;

emit DrawLine(XStartpoint,YStartpoint,X1,Y1);
}

// second

tempAmp=amplitude;

for(int a=1;a<amplitude;a++)
{
XStartpoint = X1;
YStartpoint = Y1;

X1+=Ttime/tempAmp;
tempAmp--;
Y1+=1;

emit DrawLine(XStartpoint,YStartpoint,X1,Y1);
}

tempAmp=1;
for(int g=amplitude;g>0;g--)
{
XStartpoint = X1;
YStartpoint = Y1;

X1+=Ttime/tempAmp;
tempAmp++;
Y1-=1;

emit DrawLine(XStartpoint,YStartpoint,X1,Y1);
}
if(X1 > 300)
{
X1=XStartpoint=50;
Y1=YStartpoint=150;

emit DrawLine(XStartpoint,YStartpoint,X1,Y1);
}
msleep(250);
}
}

-------------------------------------------------------------------------------------------------------------------------------------------
pixmap.cpp


#include "pixmap.h"
#include "ui_pixmap.h"
#include "drawthread.h"

DrawThread thread1;

pixmap::pixmap(QWidget *parent) :
QDialog(parent),
ui(new Ui::pixmap)
{
ui->setupUi(this);
X1=XStartpoint=50;
Y1=YStartpoint=150;

amplitude = 10;
Ttime = 5;

drawpath=new QPainterPath(QPointF(50,150));

drawpixmap = new QPixmap(size());
drawpixmap->fill(Qt::transparent);

connect(&thread1,SIGNAL(DrawLine(int ,int ,qreal ,qreal )),this,SLOT(display(int ,int ,qreal ,qreal)));
thread1.start();
}

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

void pixmap::paintEvent(QPaintEvent *pEvent)
{

QWidget::paintEvent(pEvent);
QPainter painter(this) ;

painter.begin(drawpixmap);
QPen pen1(Qt::red, 2);
QPen pen2(Qt::magenta, 2);
QPen pen3(Qt::green, 1);


painter.setRenderHint(QPainter::SmoothPixmapTransf orm,true);
painter.setPen(pen1);

painter.drawLine(50,100,50,250); //Y-AXIS
painter.drawLine(50,150,350,150); //X-AXIS


painter.drawText(360,150,"Time ");
painter.drawText(50,80,"Amplitude");

//y-arrow
painter.drawLine(50,100,40,110);
painter.drawLine(50,100,60,110);

// //x-arrows

painter.setPen(pen2);

painter.drawLine(XStartpoint,YStartpoint,XStartpoi nt-5,YStartpoint-5);
painter.drawLine(XStartpoint,YStartpoint,XStartpoi nt-5,YStartpoint+5);

painter.setPen(pen3);

if(!drawpath->isEmpty())
{
painter.drawPath(*drawpath);
painter.drawPixmap(0,0,*drawpixmap);
}
}

void pixmap::display(int lastx,int lasty,qreal currx,qreal curry)
{
X1=currx;
Y1=curry;
XStartpoint=lastx;
YStartpoint=lasty;

if(X1==XStartpoint)
{
*drawpath=drawpath->subtracted(*drawpath);
}

drawpath->moveTo(XStartpoint,YStartpoint);
drawpath->lineTo(X1,Y1);
repaint();
}

anda_skoa
24th June 2015, 10:18
Not sure what you are asking for.

How to temporarily make a thread wait for something?
QWaitCondition could be your friend for that.

Cheers,
_