Results 1 to 14 of 14

Thread: Qt_Drwa_Issue

  1. #1
    Join Date
    Sep 2012
    Posts
    11
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Qt_Drwa_Issue

    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

    Qt Code:
    1. #ifndef sinewave_H
    2. #define sinewave_H
    3.  
    4. #include <QMainWindow>
    5. #include "multithread.h"
    6.  
    7. namespace Ui {
    8. class sinewave;
    9. }
    10.  
    11. class sinewave : public QMainWindow {
    12. Q_OBJECT
    13. public:
    14.  
    15. qreal amplitude;
    16. qreal Ttime;
    17. qreal X1; // X Cordinate
    18. qreal Y1; // Y Cordinate
    19. int sinCount;
    20. int lbl1;
    21.  
    22.  
    23. sinewave(QWidget *parent = 0);
    24. ~sinewave();
    25.  
    26. signals:
    27. void drawCord(qreal x, qreal y);
    28.  
    29. protected:
    30. void changeEvent(QEvent *e);
    31. void paintEvent(QPaintEvent *);
    32.  
    33. private:
    34. Ui::sinewave *ui;
    35. QThread *thread;
    36. MultiThread *multithread;
    37. QPixmap *p;
    38.  
    39. private slots:
    40. // void on_actionPause_activated();
    41. //void on_actionThreadStart_activated();
    42. // void on_actionStart_activated();
    43. // void on_actionSTART_activated();
    44. void display();
    45. void on_pushButton_clicked();
    46. };
    47.  
    48. #endif // sinewave_H
    To copy to clipboard, switch view to plain text mode 

    sinewave.cpp

    Qt Code:
    1. #include "sinewave.h"
    2. #include "ui_sinewave.h"
    3. #include "multithread.h"
    4. #include <QtGui>
    5. #include <QPalette>
    6. #include <cmath>
    7. #include <QPainterPath>
    8. #include <QPainter>
    9. #include <QThread>
    10. #include <QDebug>
    11. #include <QTimer>
    12.  
    13. QTimer timer1;
    14.  
    15. volatile bool drawonce = 1;
    16.  
    17. static int i=0;
    18. static int XStartpoint=0;
    19. static int YStartpoint=0;
    20. static int jCount=5;
    21.  
    22. sinewave::sinewave(QWidget *parent) :
    23. QMainWindow(parent),
    24. ui(new Ui::sinewave)
    25. {
    26. ui->setupUi(this);
    27. X1=XStartpoint=50;
    28. Y1=YStartpoint=250;
    29.  
    30. amplitude = 10;
    31. sinCount = 1;
    32. Ttime = 5;
    33.  
    34.  
    35.  
    36. // repaint();
    37.  
    38. connect(&timer1, SIGNAL(timeout()), this, SLOT(display()));
    39. connect(this, SIGNAL(drawCord(qreal,qreal)), this, SLOT(repaint()));
    40. timer1.start(250); // Interval 0 means to refresh as fast as possible
    41.  
    42. // thread = new QThread();
    43. // multithread = new MultiThread();
    44.  
    45. // multithread->moveToThread(thread);
    46. //// connect(multithread, SIGNAL(valueChanged(QString)), ui->labelCount, SLOT(setText(QString)));
    47. // connect(multithread, SIGNAL(multithreadRequested()), thread, SLOT(start()));
    48. // connect(thread, SIGNAL(started()), multithread, SLOT(doMultiThraed()));
    49. // connect(multithread, SIGNAL(finished()), thread, SLOT(quit()), Qt::DirectConnection);
    50. // // receive the emited signal
    51. // connect(multithread, SIGNAL(value()),SLOT(display()));
    52. }
    53.  
    54. sinewave::~sinewave()
    55. {
    56. // multithread->abort();
    57. // thread->wait();
    58. // qDebug()<<this->QObject::thread()->currentThreadId();
    59. // delete thread;
    60. // delete multithread;
    61. delete ui;
    62. }
    63.  
    64. void sinewave::changeEvent(QEvent *e)
    65. {
    66. QMainWindow::changeEvent(e);
    67. switch (e->type()) {
    68. case QEvent::LanguageChange:
    69. ui->retranslateUi(this);
    70. break;
    71. default:
    72. break;
    73. }
    74. }
    75.  
    76.  
    77. void sinewave::paintEvent(QPaintEvent *pEvent)
    78. {
    79. qreal tempx,tempy;
    80. QWidget::paintEvent(pEvent);
    81. QPen pen1(Qt::green, 1);
    82. QPainter painter(this) ;
    83.  
    84. painter.setRenderHint(QPainter::SmoothPixmapTransform,true);
    85. // setAutoFillBackground(false);
    86. // setAttribute(Qt::WA_OpaquePaintEvent, true);
    87. // setAttribute(Qt::WA_NoSystemBackground, true);
    88.  
    89.  
    90. painter.setPen(pen1);
    91. painter.drawLine(50,150,50,350);
    92. painter.drawLine(50,250,350,250);
    93.  
    94. painter.drawText(360,250,"Time ");
    95. painter.drawText(50,130,"Amplitude");
    96.  
    97. // //x-arrows
    98.  
    99. QPen pen2(Qt::red, 2);
    100. painter.setPen(pen2);
    101.  
    102. painter.drawLine(XStartpoint,YStartpoint,XStartpoint-5,YStartpoint-5);
    103. painter.drawLine(XStartpoint,YStartpoint,XStartpoint-5,YStartpoint+5);
    104.  
    105. //y-arrow
    106. painter.drawLine(50,150,40,160);
    107. painter.drawLine(50,150,60,160);
    108.  
    109. painter.setPen(pen1);
    110.  
    111. path1.moveTo(XStartpoint,YStartpoint);
    112. // qreal tempAmp;
    113.  
    114. tempx = X1;
    115. tempy = Y1;
    116.  
    117. path1.lineTo(QPointF(tempx,tempy));
    118. painter.drawPath(path1);
    119. }
    120.  
    121.  
    122. /*
    123. void sinewave::on_actionStart_activated()
    124. {
    125.   // code to start multithread
    126.   multithread->abort();
    127.   thread->wait();
    128.   multithread->requestThread();
    129. }
    130. */
    131.  
    132. /*
    133. void sinewave::on_actionPause_activated()
    134. {
    135.   // Pause mulithread
    136.   multithread->abort();
    137.   thread->wait();
    138. }
    139. */
    140.  
    141.  
    142. void sinewave::display()
    143. {
    144. qreal tempAmp;
    145. if(X1 > 300)
    146. {
    147. // painter->eraseRect(50,150,250,200);
    148. X1=XStartpoint=50;
    149. Y1=YStartpoint=250;
    150.  
    151. QApplication::processEvents();
    152. QApplication::processEvents();
    153. emit drawCord(X1,Y1);
    154. return;
    155. }
    156. for(int SS=0;SS<sinCount;SS++)
    157. {
    158. tempAmp=amplitude;
    159.  
    160. for(int a=1;a<amplitude;a++)
    161. {
    162. XStartpoint = X1;
    163. YStartpoint = Y1;
    164.  
    165. X1+=Ttime/tempAmp;
    166. tempAmp--;
    167. Y1-=1;
    168.  
    169. QApplication::processEvents();
    170. QApplication::processEvents();
    171. emit drawCord(X1,Y1);
    172. }
    173.  
    174. tempAmp=1;
    175. for(int g=amplitude;g>0;g--)
    176. {
    177. XStartpoint = X1;
    178. YStartpoint = Y1;
    179.  
    180. X1+=Ttime/tempAmp;
    181. tempAmp++;
    182. Y1+=1;
    183.  
    184. QApplication::processEvents();
    185. QApplication::processEvents();
    186. emit drawCord(X1,Y1);
    187. }
    188.  
    189. // second
    190.  
    191. tempAmp=amplitude;
    192.  
    193. for(int a=1;a<amplitude;a++)
    194. {
    195. XStartpoint = X1;
    196. YStartpoint = Y1;
    197.  
    198. X1+=Ttime/tempAmp;
    199. tempAmp--;
    200. Y1+=1;
    201.  
    202. QApplication::processEvents();
    203. QApplication::processEvents();
    204. emit drawCord(X1,Y1);
    205. }
    206.  
    207. tempAmp=1;
    208. for(int g=amplitude;g>0;g--)
    209. {
    210. XStartpoint = X1;
    211. YStartpoint = Y1;
    212.  
    213. X1+=Ttime/tempAmp;
    214. tempAmp++;
    215. Y1-=1;
    216.  
    217. QApplication::processEvents();
    218. QApplication::processEvents();
    219. emit drawCord(X1,Y1);
    220. }
    221. }
    222.  
    223. }
    224.  
    225.  
    226.  
    227.  
    228. void sinewave::on_pushButton_clicked()
    229. {
    230. // copy code to generate sin wave
    231. amplitude=0.0;
    232. Ttime=0.0;
    233. X1=100.0;
    234. Y1=400.0;
    235. QString amp=ui->amplitude_le->text();
    236. QString tym=ui->time_le->text();
    237. amplitude=amp.toInt();
    238. Ttime=tym.toInt();
    239. XStartpoint=100;
    240. YStartpoint=400;
    241. sinCount=5;
    242. repaint();
    243.  
    244. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 17th June 2015 at 05:53. Reason: missing [code] tags

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Qt_Drwa_Issue

    When you post code, please use the [code][/code] tags. Nobody wants to waste time looking over poorly formatted code and you are much more likely to get help if you make it easier to review your code.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt_Drwa_Issue

    Why are you calling processEvents() all the time?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Sep 2012
    Posts
    11
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt_Drwa_Issue

    to clear all pending paint events,even though i wont call processevents, wave drawing getting slow after some time

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt_Drwa_Issue

    Quote Originally Posted by prashantbasvat View Post
    to clear all pending paint events
    You don't have any pending paint events as you are using repaint() instead of update(). Don't do that.

    even though i wont call processevents, wave drawing getting slow after some time
    You are emitting a bunch of drawCord() signals, each of which results in repainting the whole widgets. I don't know what this is supposed to achieve. Why are you doing that? You end up repainting the widget thousands of times instead of once for each call to display().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2012
    Posts
    11
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt_Drwa_Issue

    actually i wont to display wave drawing so i am emitting signal to draw n display every line segment of wave

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt_Drwa_Issue

    Quote Originally Posted by prashantbasvat View Post
    actually i wont to display wave drawing so i am emitting signal to draw n display every line segment of wave
    So why are you surprised it draws slowly if you want it to draw slowly?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2012
    Posts
    11
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt_Drwa_Issue

    when i execute an application,at that instant drawing speed is fast as drawing continues gradually drawing speed reduces,this is actually problem u can execute that code and see difference in drawing speed gradually

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt_Drwa_Issue

    Quote Originally Posted by prashantbasvat View Post
    when i execute an application,at that instant drawing speed is fast as drawing continues gradually drawing speed reduces,this is actually problem u can execute that code and see difference in drawing speed gradually
    You have a timer set to 250ms. If it takes your application 5 seconds to draw a single pass then you can't expect the app to keep up drawing at 250ms. Adjust your timer to something greater than the amount of time it takes the application to draw a single pass and see if the problem persists.

    Your approach is everything but optimal so don't be surprised that the program lags behind. You are drawing much too much than required.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Sep 2012
    Posts
    11
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt_Drwa_Issue

    after increasing timer period also drawing getting slowed after some time

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt_Drwa_Issue

    Quote Originally Posted by prashantbasvat View Post
    after increasing timer period also drawing getting slowed after some time
    How did you measure how long it takes for your application to render a single pass?

    By the way, the slowdown you observe is probably that you are constantly adding elements to the path in the paint event.

    To be honest, I haven't recently seen code broken so badly as yours.
    Last edited by wysota; 17th June 2015 at 11:15.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt_Drwa_Issue

    Ah, software to monitor ECG signals. I'm remembering my Qt Prayer at the moment.

  13. #13
    Join Date
    Sep 2012
    Posts
    11
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Qt_Drwa_Issue

    dear d_stranz's ,
    do you have any expeince in any such kind of biomedical applications.so please share m urs knowledge

  14. #14
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt_Drwa_Issue

    Let me try to expand a bit on the other remarks in this thread, because I believe you are missing the main point.

    QWidget::paintEvent() is called when a widget needs to redraw a whole region of its surface, and its implementation needs to reflect that. In your case, sinewave::paintEvent() must draw all chords, not just one. You need to refactor your code. You do not need sinewave::display(), sinewave::drawCord(); you do not need to call QApplication::processEvents() or QWidget::repaint().

    Here is a design proposal.
    • Identify the parameters of your sinewave, and declare them as private members of sinewave. From a quick look at your code it seems that amplitude and Ttime are the only parameters.
    • In sinewave::paintEvent(), read the values of those parameters and draw the whole sinewave accordingly. You can organize the code by defining a private helper function paintChord if you like.
    • After you change the values of the parameters (e.g. in sinewave::on_pushButton_clicked()), just call QWidget::update() (NOT QWidget::repaint()) to let Qt know that the widget should be repainted.

    By the way, sinewave::on_pushButton_clicked() suggests that sinewave contains child widgets. Shouldn't the sinewave be painted on a sibling of the button?

    When everything works, and only then, consider introducing optimizations, such as ignoring the chords outside the region to repaint in sinewave::paintEvent(), but only if you identify that as a performance bottleneck.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.