Results 1 to 11 of 11

Thread: Fast drawingPolygons

  1. #1
    Join Date
    Dec 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Fast drawingPolygons

    Hi everyone,
    i am working on a project where i need to draw lot of polygons. Is there any way to draw it without Timer? I am using QPainter... My problem is that i dont know how to draw a lot of polygons in short time, miliseconds.

    Here is my code,
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include "polygonpoints.h"
    4.  
    5.  
    6. Widget::Widget(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::Widget)
    9. {
    10. ui->setupUi(this);
    11.  
    12.  
    13. timer = new QTimer(this);
    14. connect(timer, SIGNAL(timeout()), this, SLOT(AdvanceState()));
    15. timer->start(1);
    16.  
    17. }
    18.  
    19. Widget::~Widget()
    20. {
    21. delete ui;
    22. }
    23. void Widget::AdvanceState(){
    24. this->setAttribute(Qt::WA_NoSystemBackground,true);
    25. if(brojac<300){
    26. update();
    27. std::cout << ++brojac << std::endl;
    28. }
    29. }
    30.  
    31. void Widget::paintEvent(QPaintEvent *event)
    32. {
    33.  
    34. //std::cout << ++brojac << std::endl;
    35. QPainter painter(this);
    36. painter.setRenderHint(QPainter::Antialiasing, true);
    37. int N=9; // broj vrhova
    38. x.set_x(N);
    39. x.generate();
    40. //int N = 4; //broj vrhova
    41. QPointF *points = new QPointF[N];
    42. for(int i = 0; i < N; ++i){
    43. points[i].setX(x.vertices[i].get(0));
    44. points[i].setY(x.vertices[i].get(1));
    45. }
    46.  
    47. painter.setPen(Qt::NoPen);
    48. painter.setBrush(QBrush(QColor(qrand() % 255, qrand() % 255, qrand() % 255, 128)));
    49. painter.drawPolygon(points,N);
    50.  
    51. delete[] points;
    52.  
    53. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Fast drawingPolygons

    Why are you generating a new set of data on each paintEvent ? Typically a paintEvent should only present the current state of the widget, so prepare the data elsewhere and simply display it in paintEvent.
    Btw. you can set the widget's attributes once in the constructor.

  3. #3
    Join Date
    Dec 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Fast drawingPolygons

    I understand, but this code isn't optimized. I just want to know how can i update() widget, 10000 times for example, but without timer. Is there any solution or idea?

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Fast drawingPolygons

    Why do you want to update a widget exactly 10000 times ?

  5. #5
    Join Date
    Dec 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Fast drawingPolygons

    Okej, first i want to draw polygon, then do some calculations, then i want to draw another one at Random place, again calculations, ... Many times... But idea is that i can see it in Widget as i draw them.. In Real i will need much more polygons, Like 1 million..

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Fast drawingPolygons

    If you are looking for a different method of stepping through a time interval, have a look at QTimeLine.

    Cheers,
    _

  7. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Fast drawingPolygons

    But idea is that i can see it in Widget as i draw them.. In Real i will need much more polygons, Like 1 million..
    So simply create a method to update the data periodically and display it in paintEvent, pretty much something like you already have, but with "generate()" moved to AdvanceState. AdvanceState() should represent a single step in your calculations, used to update the data. Using a timer for this task seems reasonable.
    If you want to limit the number of steps in your "simulation" use a member variable
    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. #include "polygonpoints.h"
    4.  
    5.  
    6. Widget::Widget(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::Widget)
    9. {
    10. ui->setupUi(this);
    11. this->setAttribute(Qt::WA_NoSystemBackground,true);
    12.  
    13. this->stepsToDo = 10000;
    14. timer = new QTimer(this);
    15. connect(timer, SIGNAL(timeout()), this, SLOT(AdvanceState()));
    16. timer->start(1);
    17.  
    18. }
    19.  
    20. Widget::~Widget()
    21. {
    22. delete ui;
    23. }
    24. void Widget::AdvanceState(){
    25.  
    26. if(this->stepsToDo){
    27. // generate new polygon or do whatever is required for a single step
    28. --this->stepsToDo;
    29. update(); // update requests will be scheduled, this is not an immediate repaint
    30. } else{
    31. this->timer->stop();
    32. }
    33. }
    34.  
    35. void Widget::paintEvent(QPaintEvent *event)
    36. {
    37.  
    38. QPainter painter(this);
    39. painter.setRenderHint(QPainter::Antialiasing, true);
    40. // present the data generated in AdvanceStep()
    41. }
    To copy to clipboard, switch view to plain text mode 
    Btw. human eye can process around 10 separate images per second if I remember correctly, you can't even notice if you try to display millions of different polygons in a short time.

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

    Default Re: Fast drawingPolygons

    Quote Originally Posted by CROmpir View Post
    Okej, first i want to draw polygon, then do some calculations, then i want to draw another one at Random place, again calculations, ... Many times... But idea is that i can see it in Widget as i draw them.. In Real i will need much more polygons, Like 1 million..
    If you only add polygons and never remove them then paint on a pixmap and then "blit" the pixmap to the widget. When a next polygon comes, draw just this one polygon on the pixmap that already contains all the previous ones and re-blit the pixmap to the widget. If you combine that with repainting only that area of the pixmap which contains the new polygon, it should be super fast.

    Other than that, you can always use OpenGL. And I don't think you really need to draw the widget at 1kHz, 20Hz would be more than enough.
    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.


  9. #9
    Join Date
    Dec 2014
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Fast drawingPolygons

    Thanks all for quick answers. I have another problem, my palette color is always black, but i set it to be white. Any suggestions? Also, what is the best solution to draw some polygons, and then delete some of them? How can i remove something what i draw?

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Fast drawingPolygons

    my palette color is always black, but i set it to be white. Any suggestions?
    Yes, show us the code where you think you are setting the palette to white.

    Also, what is the best solution to draw some polygons, and then delete some of them?
    Create a data structure (like QVector< QPolygon * >) to keep track of your polygons. When you want to remove one, erase it from the vector and delete it. Call update() and draw the contents of the vector.

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

    Default Re: Fast drawingPolygons

    QVector is not a right data type, it doesn't respond well to adding/removing items from arbitrary positions in the vector. I would start with QGraphicsView and QGraphicsScene.
    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.


Similar Threads

  1. fast quering
    By codeman in forum Qt Programming
    Replies: 3
    Last Post: 29th December 2009, 19:51
  2. fast drawing in Qt
    By franco.amato in forum Qt Programming
    Replies: 0
    Last Post: 23rd November 2009, 21:22
  3. Qt3 more fast than Qt4 ?
    By commarmi in forum Qt Programming
    Replies: 11
    Last Post: 14th September 2007, 14:36
  4. Are fast reg exps too much to ask?
    By fullmetalcoder in forum Qt Programming
    Replies: 10
    Last Post: 15th June 2007, 21:02
  5. Fast Keyboard, help !
    By Alex63 in forum Qt Programming
    Replies: 2
    Last Post: 27th June 2006, 18:18

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
  •  
Qt is a trademark of The Qt Company.