Results 1 to 7 of 7

Thread: Fast(est) way of drawing simple objects

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Fast(est) way of drawing simple objects

    Hi,

    I would like to be able to make an item (let's say a rectangle) flash at constant high frequencies on the screen. I know this isn't something that can be solved prefectly (due to the scheduler of the OS, imperfect timing, etc.) but I would like to get fairly good results.

    The objects to be drawn are extremely simple, so I guess that most of the time is spent on actually redrawing the image on the screen. For this reason I'd like to ask your opinion on what you think is the fastest method of performing this.

    Right now I've come up with something like this:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. rect = scene.addRect(QRectF(0, 0, 100, 100), QPen(), Qt::black);
    7.  
    8. ui->graphicsView->setViewport(new QGLWidget);
    9. ui->graphicsView->setScene(&scene);
    10. ui->graphicsView->show();
    11.  
    12. timeLine = new QTimeLine(40000, this);
    13. timeLine->setCurveShape(QTimeLine::LinearCurve);
    14. timeLine->setFrameRange(0, 1000);
    15. connect(timeLine, SIGNAL(frameChanged(int)), this, SLOT(updateRect(int)));
    16. timeLine->start();
    17. }
    18.  
    19. void MainWindow::updateRect(int frame)
    20. {
    21. if (frame % 2)
    22. {
    23. this->rect->setBrush(Qt::blue);
    24. }
    25. else
    26. {
    27. this->rect->setBrush(Qt::black);
    28. }
    29. this->rect->update();
    30. }
    To copy to clipboard, switch view to plain text mode 

    This works, but it can be clearly seen that redrawing isn't done "instantaneously". Searching on the forum I've found that OpenGL should be used as the viewport renderer, and it did actually make a difference, but I think this could still be improved.

    This is something that is really difficult to measure (I don't really know if this could be measured at all), that's why I ask you to share your thoughts on this matter. If you have something in your mind that you think could significantly improve the drawing performance (throwing out Graphics View in favor of a simple QWidget? using two fixed QPixmaps?), please share it with me.

    Thanks,
    Adam

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Fast(est) way of drawing simple objects

    If you want the code to run real time, that is, without interruptions, keep the code small and keep everything else (events, other programs asking cpu time, ...) to a minimum.

    If this is software to test some psychological aspect of a subject, you might want to invest some time in understanding how to use the hardware timers on your system. This will require you to look into the api's of the operating system.

    My first try would be a simple subclassed widget.

  3. #3
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Fast(est) way of drawing simple objects

    Yes, I know that, however as a first version I wanted to implement something that is platform-independent.

    Regarding the timing issues, I have changed from QTimeLine to a simple QTImer implementation (I can't even tell you why it wasn't my first choice), and it also made a difference.
    Qt Code:
    1. timer = new QTimer(this);
    2. connect(timer, SIGNAL(timeout()), this, SLOT(updateRect()));
    3. timer->start(35);
    4.  
    5. QTimer *timer2 = new QTimer(this);
    6. timer2->singleShot(2000, this, SLOT(endTest()));
    To copy to clipboard, switch view to plain text mode 

    Still, if you have some ideas about optimizing the redrawing itself, I'm open to any suggestions. (I'll try the simple widget method soon)

  4. #4
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Fast(est) way of drawing simple objects

    Hi!

    Concerning QTimer accuracy: Also we get slightly off topic we get back to it at the end:
    http://www.qtcentre.org/threads/4031...Timer-accuracy

    So I recommend timer->start(16); That will give you approximately 60 fps. Just change your modulo for slower flips accordingly.

    If you want really accurate timing, you will need to sync your updates with the screen refresh. Thats called vertical-sync (vsync) and can easily be enabled on a QGLWidget.

    Qt Code:
    1. QGLFormat glFormat(QGL::SampleBuffers);
    2. glFormat.setSwapInterval(1);
    3. QGLWidget* glwidget = new QGLWidget(glFormat);
    To copy to clipboard, switch view to plain text mode 
    Implement a frames per second counter - while that one is fixed at your screen refresh rate you don't need to worry about faster drawing routines.

    You will probably want to subclass a QGLWidget. Manual swapbuffer is shown in the helloGL ES examples shipped with qt. Couldn't find a doc link for them. when vsync is active the call to swapBuffers will not return until the refresh occured. Thats how the timing is going to be very precise. If you update your GLWidget with a timer as the helloGL example does, you should call qApp()->processEvents() somewhere to keep your GUI responsive. The FPS counter implementation of that example is mod 100. You will have to adapt that logic, maybe by starting a custom timer with an intervall of 1000ms and resetting the frames-counter in it.

    HIH

    Joh
    Last edited by JohannesMunk; 8th April 2011 at 00:20.

  5. The following user says thank you to JohannesMunk for this useful post:

    eperfa (9th April 2011)

  6. #5
    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(est) way of drawing simple objects

    Quote Originally Posted by eperfa View Post
    Still, if you have some ideas about optimizing the redrawing itself, I'm open to any suggestions. (I'll try the simple widget method soon)
    I would second what tbscope says - get rid of GraphicsView and use a plain widget.
    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.


  7. #6
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Fast(est) way of drawing simple objects

    That was quite a cross posting.. Guess I spent too long editing that post.

    Joh

  8. #7
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Fast(est) way of drawing simple objects

    Thank you guys, especially Joh! That was exactly the knowledge I was missing - I'll definitely give it a try in the next couple of days.

Similar Threads

  1. 3D engine to create simple 3D objects
    By been_1990 in forum General Discussion
    Replies: 11
    Last Post: 15th December 2010, 11:57
  2. Replies: 1
    Last Post: 29th July 2010, 13:35
  3. fast drawing in Qt
    By franco.amato in forum Qt Programming
    Replies: 0
    Last Post: 23rd November 2009, 21:22
  4. QGraphicsView and fast moving objects
    By deMarco in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2009, 11:07
  5. Fast image drawing/scaling in Qt 3.3
    By eriwik in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 10:45

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.