Results 1 to 5 of 5

Thread: Qt Multi Threading RePainting Problem

  1. #1
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Qt Multi Threading RePainting Problem

    Hi i am working on college task that involve a recursion drawing question and i am working with Qt.
    my problem when the drawing function begin, the gui freezing until drawing finish,even though i used multi threading .
    is there any way to avoid freezing while repainting main window ???
    here is the code:
    Qt Code:
    1. class draw:public QThread
    2. {
    3. public :
    4. QWidget *t;
    5. draw(){}
    6. void run()
    7. {
    8. Drawing(10,40,0,3.1415926535897932384626433832795/4,200,200,d);
    9. }
    10.  
    11. void Drawing(int n,double d,double q1,double q2,int x,int y,QPainter *dr)
    12. {
    13. if (n > 0)
    14. {
    15. int x1 = x - (sin(q1) * d);
    16. int y1 = y - (cos(q1) * d);
    17. dr->drawLine(x, y, x1, y1);
    18. t->repaint();
    19. int x2 = x1- (cos(q1) * d);
    20. int y2 = y1 + (sin(q1) * d);
    21. dr->drawLine(x1, y1, x2, y2);
    22. t->repaint();
    23. int x3 = x2 + (sin(q1) * d);
    24. int y3 = y2 + (cos(q1) * d);
    25. dr->drawLine(x2 , y2 , x3 , y3);
    26. t->repaint();
    27. dr->drawLine(x3 , y3 , x , y );
    28. t->repaint();
    29. double d1 = d / sqrt( 2.0 ); //d1 = d/?2
    30. int x4 = x1 - ( cos(q2) * d1);
    31. int y4 = y1 - ( sin(q2) * d1);
    32. Drawing(n - 1, d1, q1 - 3.1415926535897932384626433832795/4, q2 + 3.1415926535897932384626433832795/4 , x1 , y1,dr);
    33.  
    34. Drawing(n - 1, d1, q1 + 3.1415926535897932384626433832795/4, q2 - 3.1415926535897932384626433832795/4 , x4 , y4,dr);
    35. }
    36. }
    37. };
    38.  
    39.  
    40. void MainWindow::on_pushButton_clicked()
    41. {
    42. image=new QPixmap(this->geometry().width(),this->geometry().height());
    43. image->fill(this,0,0);
    44. dr=new QPainter(this->image);
    45. r=this->geometry();
    46. draw d;
    47. d.d=dr;
    48. d.t=this;
    49. d.run();
    50. delete dr;
    51. }
    52.  
    53.  
    54.  
    55.  
    56.  
    57. void MainWindow::paintEvent(QPaintEvent *e)
    58. {
    59. QPainter p(this);
    60. p.drawPixmap(r,*this->image);
    61. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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 Multi Threading RePainting Problem

    You cannot draw on widgets from within threads. However since you are calling run() and not start(), your thread is not created at all and run() is called on behalf of the main thread. Have a look at the mandlebrot example to see how to use threads for drawing.
    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.


  3. The following user says thank you to wysota for this useful post:

    Mohammad (12th January 2012)

  4. #3
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Multi Threading RePainting Problem

    thanks wysota for your helpful reply .
    my problem is not big computation of the recursive shape but freezing while Repainting (i want user see how it's created -draw step by step about (2^n) repaint - not show it totally drawn in one step) .
    can any one present a tick to do this ???
    Last edited by Mohammad; 12th January 2012 at 16:43.

  5. #4
    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 Multi Threading RePainting Problem

    To do what you want you need to divide your drawing process into steps and then redraw the canvas for each step separately introducing some kind of delay. More or less like so:

    Qt Code:
    1. public slots:
    2. void setCurrentStep(int s) {
    3. m_step = s;
    4. update();
    5. }
    6.  
    7. void XX::paintEvent(QPaintEvent *) {
    8. QPainter p(this);
    9. p.draw...
    10. p.draw...
    11. if(m_step == 0) return; // stops rendering at this point if step == 0
    12. p.draw...
    13. p.draw...
    14. if(m_step == 1) return; // stops rendering at this point if step == 1
    15. //...
    16. //...
    17. }
    To copy to clipboard, switch view to plain text mode 

    And then you can do something like that:

    Qt Code:
    1. QTimeLine *tl = new QTimeLine(5000, this);
    2. tl->setFrameRange(0, 4);
    3. connect(tl, SIGNAL(frameChanged(int)), this, SLOT(setCurrentStep(int)));
    4. connect(tl, SIGNAL(fnished()), tl, SLOT(deleteLater()));
    5. tl->start();
    To copy to clipboard, switch view to plain text mode 

    This will render 5 steps of your animation in the total duration of 5 seconds (1s/frame). No threads 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.


  6. The following user says thank you to wysota for this useful post:

    Mohammad (12th January 2012)

  7. #5
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qt Multi Threading RePainting Problem

    thank you wysota that is what i need.

Similar Threads

  1. Multi-threading in Console QCoreApplication
    By Dazed in forum Qt Programming
    Replies: 4
    Last Post: 4th July 2010, 16:27
  2. Slow Multi-Threading
    By AwDogsgo2Heaven in forum Qt Programming
    Replies: 5
    Last Post: 19th July 2009, 22:36
  3. Multi-threading
    By lixo1 in forum Qt Programming
    Replies: 5
    Last Post: 22nd June 2009, 14:22
  4. Multi threading ...
    By kiranraj in forum Qt Programming
    Replies: 2
    Last Post: 18th June 2007, 17:51
  5. Multi-threading
    By shamik in forum Qt Programming
    Replies: 15
    Last Post: 28th November 2006, 11:22

Tags for this Thread

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.