prashantbasvat
17th June 2015, 06:29
i am begineer in qt development, i am doing qt project in that project i have to draw ECG waves in qt.so i have developed basic qt-app for wave drawing,but i am facing issues is that after execution of app ,after some time(2-3 minutes) wave drawing getting slows.
so how to resolve this issue; so here is my code
(NOTE : I HAVE ATTACHED MY APP SOURCE ALSO)
sinewave.h
#ifndef sinewave_H
#define sinewave_H
#include <QMainWindow>
#include "multithread.h"
namespace Ui {
class sinewave;
}
class sinewave : public QMainWindow {
Q_OBJECT
public:
qreal amplitude;
qreal Ttime;
qreal X1; // X Cordinate
qreal Y1; // Y Cordinate
int sinCount;
int lbl1;
sinewave(QWidget *parent = 0);
~sinewave();
signals:
void drawCord(qreal x, qreal y);
protected:
void changeEvent(QEvent *e);
void paintEvent(QPaintEvent *);
private:
Ui::sinewave *ui;
QThread *thread;
MultiThread *multithread;
QPixmap *p;
private slots:
// void on_actionPause_activated();
//void on_actionThreadStart_activated();
// void on_actionStart_activated();
// void on_actionSTART_activated();
void display();
void on_pushButton_clicked();
};
#endif // sinewave_H
sinewave.cpp
#include "sinewave.h"
#include "ui_sinewave.h"
#include "multithread.h"
#include <QtGui>
#include <QPalette>
#include <cmath>
#include <QPainterPath>
#include <QPainter>
#include <QThread>
#include <QDebug>
#include <QTimer>
QTimer timer1;
QPainterPath path1;
volatile bool drawonce = 1;
static int i=0;
static int XStartpoint=0;
static int YStartpoint=0;
static int jCount=5;
sinewave::sinewave(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::sinewave)
{
ui->setupUi(this);
X1=XStartpoint=50;
Y1=YStartpoint=250;
amplitude = 10;
sinCount = 1;
Ttime = 5;
// repaint();
connect(&timer1, SIGNAL(timeout()), this, SLOT(display()));
connect(this, SIGNAL(drawCord(qreal,qreal)), this, SLOT(repaint()));
timer1.start(250); // Interval 0 means to refresh as fast as possible
// thread = new QThread();
// multithread = new MultiThread();
// multithread->moveToThread(thread);
//// connect(multithread, SIGNAL(valueChanged(QString)), ui->labelCount, SLOT(setText(QString)));
// connect(multithread, SIGNAL(multithreadRequested()), thread, SLOT(start()));
// connect(thread, SIGNAL(started()), multithread, SLOT(doMultiThraed()));
// connect(multithread, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
// // receive the emited signal
// connect(multithread, SIGNAL(value()),SLOT(display()));
}
sinewave::~sinewave()
{
// multithread->abort();
// thread->wait();
// qDebug()<<this->QObject::thread()->currentThreadId();
// delete thread;
// delete multithread;
delete ui;
}
void sinewave::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void sinewave::paintEvent(QPaintEvent *pEvent)
{
qreal tempx,tempy;
QWidget::paintEvent(pEvent);
QPen pen1(Qt::green, 1);
QPainter painter(this) ;
painter.setRenderHint(QPainter::SmoothPixmapTransf orm,true);
// setAutoFillBackground(false);
// setAttribute(Qt::WA_OpaquePaintEvent, true);
// setAttribute(Qt::WA_NoSystemBackground, true);
painter.setPen(pen1);
painter.drawLine(50,150,50,350);
painter.drawLine(50,250,350,250);
painter.drawText(360,250,"Time ");
painter.drawText(50,130,"Amplitude");
// //x-arrows
QPen pen2(Qt::red, 2);
painter.setPen(pen2);
painter.drawLine(XStartpoint,YStartpoint,XStartpoi nt-5,YStartpoint-5);
painter.drawLine(XStartpoint,YStartpoint,XStartpoi nt-5,YStartpoint+5);
//y-arrow
painter.drawLine(50,150,40,160);
painter.drawLine(50,150,60,160);
painter.setPen(pen1);
path1.moveTo(XStartpoint,YStartpoint);
// qreal tempAmp;
tempx = X1;
tempy = Y1;
path1.lineTo(QPointF(tempx,tempy));
painter.drawPath(path1);
}
/*
void sinewave::on_actionStart_activated()
{
// code to start multithread
multithread->abort();
thread->wait();
multithread->requestThread();
}
*/
/*
void sinewave::on_actionPause_activated()
{
// Pause mulithread
multithread->abort();
thread->wait();
}
*/
void sinewave::display()
{
qreal tempAmp;
if(X1 > 300)
{
// painter->eraseRect(50,150,250,200);
X1=XStartpoint=50;
Y1=YStartpoint=250;
QApplication::processEvents();
QApplication::processEvents();
emit drawCord(X1,Y1);
return;
}
for(int SS=0;SS<sinCount;SS++)
{
tempAmp=amplitude;
for(int a=1;a<amplitude;a++)
{
XStartpoint = X1;
YStartpoint = Y1;
X1+=Ttime/tempAmp;
tempAmp--;
Y1-=1;
QApplication::processEvents();
QApplication::processEvents();
emit drawCord(X1,Y1);
}
tempAmp=1;
for(int g=amplitude;g>0;g--)
{
XStartpoint = X1;
YStartpoint = Y1;
X1+=Ttime/tempAmp;
tempAmp++;
Y1+=1;
QApplication::processEvents();
QApplication::processEvents();
emit drawCord(X1,Y1);
}
// second
tempAmp=amplitude;
for(int a=1;a<amplitude;a++)
{
XStartpoint = X1;
YStartpoint = Y1;
X1+=Ttime/tempAmp;
tempAmp--;
Y1+=1;
QApplication::processEvents();
QApplication::processEvents();
emit drawCord(X1,Y1);
}
tempAmp=1;
for(int g=amplitude;g>0;g--)
{
XStartpoint = X1;
YStartpoint = Y1;
X1+=Ttime/tempAmp;
tempAmp++;
Y1-=1;
QApplication::processEvents();
QApplication::processEvents();
emit drawCord(X1,Y1);
}
}
}
void sinewave::on_pushButton_clicked()
{
// copy code to generate sin wave
amplitude=0.0;
Ttime=0.0;
X1=100.0;
Y1=400.0;
QString amp=ui->amplitude_le->text();
QString tym=ui->time_le->text();
amplitude=amp.toInt();
Ttime=tym.toInt();
XStartpoint=100;
YStartpoint=400;
sinCount=5;
repaint();
}
so how to resolve this issue; so here is my code
(NOTE : I HAVE ATTACHED MY APP SOURCE ALSO)
sinewave.h
#ifndef sinewave_H
#define sinewave_H
#include <QMainWindow>
#include "multithread.h"
namespace Ui {
class sinewave;
}
class sinewave : public QMainWindow {
Q_OBJECT
public:
qreal amplitude;
qreal Ttime;
qreal X1; // X Cordinate
qreal Y1; // Y Cordinate
int sinCount;
int lbl1;
sinewave(QWidget *parent = 0);
~sinewave();
signals:
void drawCord(qreal x, qreal y);
protected:
void changeEvent(QEvent *e);
void paintEvent(QPaintEvent *);
private:
Ui::sinewave *ui;
QThread *thread;
MultiThread *multithread;
QPixmap *p;
private slots:
// void on_actionPause_activated();
//void on_actionThreadStart_activated();
// void on_actionStart_activated();
// void on_actionSTART_activated();
void display();
void on_pushButton_clicked();
};
#endif // sinewave_H
sinewave.cpp
#include "sinewave.h"
#include "ui_sinewave.h"
#include "multithread.h"
#include <QtGui>
#include <QPalette>
#include <cmath>
#include <QPainterPath>
#include <QPainter>
#include <QThread>
#include <QDebug>
#include <QTimer>
QTimer timer1;
QPainterPath path1;
volatile bool drawonce = 1;
static int i=0;
static int XStartpoint=0;
static int YStartpoint=0;
static int jCount=5;
sinewave::sinewave(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::sinewave)
{
ui->setupUi(this);
X1=XStartpoint=50;
Y1=YStartpoint=250;
amplitude = 10;
sinCount = 1;
Ttime = 5;
// repaint();
connect(&timer1, SIGNAL(timeout()), this, SLOT(display()));
connect(this, SIGNAL(drawCord(qreal,qreal)), this, SLOT(repaint()));
timer1.start(250); // Interval 0 means to refresh as fast as possible
// thread = new QThread();
// multithread = new MultiThread();
// multithread->moveToThread(thread);
//// connect(multithread, SIGNAL(valueChanged(QString)), ui->labelCount, SLOT(setText(QString)));
// connect(multithread, SIGNAL(multithreadRequested()), thread, SLOT(start()));
// connect(thread, SIGNAL(started()), multithread, SLOT(doMultiThraed()));
// connect(multithread, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
// // receive the emited signal
// connect(multithread, SIGNAL(value()),SLOT(display()));
}
sinewave::~sinewave()
{
// multithread->abort();
// thread->wait();
// qDebug()<<this->QObject::thread()->currentThreadId();
// delete thread;
// delete multithread;
delete ui;
}
void sinewave::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
void sinewave::paintEvent(QPaintEvent *pEvent)
{
qreal tempx,tempy;
QWidget::paintEvent(pEvent);
QPen pen1(Qt::green, 1);
QPainter painter(this) ;
painter.setRenderHint(QPainter::SmoothPixmapTransf orm,true);
// setAutoFillBackground(false);
// setAttribute(Qt::WA_OpaquePaintEvent, true);
// setAttribute(Qt::WA_NoSystemBackground, true);
painter.setPen(pen1);
painter.drawLine(50,150,50,350);
painter.drawLine(50,250,350,250);
painter.drawText(360,250,"Time ");
painter.drawText(50,130,"Amplitude");
// //x-arrows
QPen pen2(Qt::red, 2);
painter.setPen(pen2);
painter.drawLine(XStartpoint,YStartpoint,XStartpoi nt-5,YStartpoint-5);
painter.drawLine(XStartpoint,YStartpoint,XStartpoi nt-5,YStartpoint+5);
//y-arrow
painter.drawLine(50,150,40,160);
painter.drawLine(50,150,60,160);
painter.setPen(pen1);
path1.moveTo(XStartpoint,YStartpoint);
// qreal tempAmp;
tempx = X1;
tempy = Y1;
path1.lineTo(QPointF(tempx,tempy));
painter.drawPath(path1);
}
/*
void sinewave::on_actionStart_activated()
{
// code to start multithread
multithread->abort();
thread->wait();
multithread->requestThread();
}
*/
/*
void sinewave::on_actionPause_activated()
{
// Pause mulithread
multithread->abort();
thread->wait();
}
*/
void sinewave::display()
{
qreal tempAmp;
if(X1 > 300)
{
// painter->eraseRect(50,150,250,200);
X1=XStartpoint=50;
Y1=YStartpoint=250;
QApplication::processEvents();
QApplication::processEvents();
emit drawCord(X1,Y1);
return;
}
for(int SS=0;SS<sinCount;SS++)
{
tempAmp=amplitude;
for(int a=1;a<amplitude;a++)
{
XStartpoint = X1;
YStartpoint = Y1;
X1+=Ttime/tempAmp;
tempAmp--;
Y1-=1;
QApplication::processEvents();
QApplication::processEvents();
emit drawCord(X1,Y1);
}
tempAmp=1;
for(int g=amplitude;g>0;g--)
{
XStartpoint = X1;
YStartpoint = Y1;
X1+=Ttime/tempAmp;
tempAmp++;
Y1+=1;
QApplication::processEvents();
QApplication::processEvents();
emit drawCord(X1,Y1);
}
// second
tempAmp=amplitude;
for(int a=1;a<amplitude;a++)
{
XStartpoint = X1;
YStartpoint = Y1;
X1+=Ttime/tempAmp;
tempAmp--;
Y1+=1;
QApplication::processEvents();
QApplication::processEvents();
emit drawCord(X1,Y1);
}
tempAmp=1;
for(int g=amplitude;g>0;g--)
{
XStartpoint = X1;
YStartpoint = Y1;
X1+=Ttime/tempAmp;
tempAmp++;
Y1-=1;
QApplication::processEvents();
QApplication::processEvents();
emit drawCord(X1,Y1);
}
}
}
void sinewave::on_pushButton_clicked()
{
// copy code to generate sin wave
amplitude=0.0;
Ttime=0.0;
X1=100.0;
Y1=400.0;
QString amp=ui->amplitude_le->text();
QString tym=ui->time_le->text();
amplitude=amp.toInt();
Ttime=tym.toInt();
XStartpoint=100;
YStartpoint=400;
sinCount=5;
repaint();
}