Results 1 to 8 of 8

Thread: plotting 2d in qt

  1. #1
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default plotting 2d in qt

    Hi,

    I have tried to plot some 2d stuff (like pixels for a start but sprites and stuff would be nice too) on to a Qimage but i tend to get weird results like this screenshot.png with this code
    Qt Code:
    1. RenderArea::RenderArea(QWidget *parent) :
    2. QWidget(parent) {
    3. image = new QImage(500, 500, QImage::Format_ARGB32_Premultiplied);
    4. image->fill(0);
    5. for (int n = 0; n < 500; ++n) {
    6. for (int i = 0; i < 500; ++i) {
    7. image->setPixel(i, n, 0x00ff00ff);
    8. }
    9. }
    10.  
    11. setBackgroundRole(QPalette::Base);
    12. setAutoFillBackground(true);
    13. this->update();
    14. }
    15.  
    16. void RenderArea::paintEvent(QPaintEvent * /* event */) {
    17. QPainter painter(this);
    18. QRect rect(0, 0, image->width(), image->height());
    19. painter.drawImage(rect, *image);
    20.  
    21. this->update();
    22. }
    To copy to clipboard, switch view to plain text mode 
    any ideas on what i have been doing wrong? Is this a good method ( if I get it to work) or is the graphiview / scene route better for "high"speed 2d plotting?

    Best regards Anders Olme

  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: plotting 2d in qt

    I'm suprised you see anything at all apart the white background.

    This is the correct code:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Widget : public QWidget {
    4. public:
    5. Widget() : QWidget() {
    6. img = QImage(500,500, QImage::Format_ARGB32_Premultiplied);
    7. img.fill(0);
    8. for(int n = 0; n < 500; ++n)
    9. for(int i = 0; i< 500; ++i)
    10. img.setPixel(i, n, qRgb(255,0,255));
    11.  
    12. setBackgroundRole(QPalette::Base);
    13. setAutoFillBackground(true);
    14. }
    15. protected:
    16. void paintEvent(QPaintEvent *) {
    17. QPainter painter(this);
    18. painter.drawImage(img.rect(), img);
    19. }
    20. private:
    21. QImage img;
    22. };
    23.  
    24. int main(int argc, char **argv) {
    25. QApplication app(argc, argv);
    26. Widget w;
    27. w.show();
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

    As for the rest of your question --- "it depends".
    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:

    buzzz321 (12th July 2012)

  4. #3
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: plotting 2d in qt

    Ah i missed the QRgb thing, and it is probably better to not use pointers I guess ( makes valgrind abit more happy). Judgeing from the cpu monitor this seems to take alot of cpu but hopefully it will be enough for my testing/playing.
    Btw is there any standard supressions one should run when running a qt program thru valgrind?
    Any how thans for the answer!

  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: plotting 2d in qt

    Your code constantly updates the widget, thus the big cpu usage. Compare your code to mine, there are no calls to update() in my code.

    Btw is there any standard supressions one should run when running a qt program thru valgrind?
    No. Valgrind reports some false positives in Qt code and external libraries Qt depends on but it should report none for your code.
    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. #5
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: plotting 2d in qt

    Mine reports quite alot of possible leaks like this one

    ==6471== 26,624 bytes in 13 blocks are possibly lost in loss record 5,472 of 5,474
    ==6471== at 0x4C2B7B2: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==6471== by 0x6F5DB36: g_realloc (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3)
    ==6471== by 0x6F2CBC8: ??? (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3)
    ==6471== by 0x6F2D0D2: g_array_insert_vals (in /lib/x86_64-linux-gnu/libglib-2.0.so.0.3200.3)
    ==6471== by 0x1385CF31: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0.2400.10)
    ==6471== by 0x1385D123: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0.2400.10)
    ==6471== by 0x1BAA98EB: ??? (in /usr/lib/x86_64-linux-gnu/gtk-2.0/2.10.0/engines/libmurrine.so)
    ==6471== by 0x1385EB0F: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0.2400.10)
    ==6471== by 0x13863028: gtk_rc_get_style (in /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0.2400.10)
    ==6471== by 0x139352F7: ??? (in /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0.2400.10)
    ==6471== by 0x13935C4B: gtk_widget_realize (in /usr/lib/x86_64-linux-gnu/libgtk-x11-2.0.so.0.2400.10)
    ==6471== by 0x5397F22: ??? (in /usr/lib/x86_64-linux-gnu/libQtGui.so.4.8.1)

  7. #6
    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: plotting 2d in qt

    That's not your code so you have no influence on it anyway. And they are probably false positives anyway. I doubt GTK leaks memory.
    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. #7
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: plotting 2d in qt

    Mm would be nice to supress it though.
    Btw if I want to double buffer the graphics is it better to use 2 pixmaps instead?

  9. #8
    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: plotting 2d in qt

    Everything Qt draws is double buffered by default, you don't have to do anything special with it. The pixmap you have is useful strictly for performance reasons so that you don't have to replot the data every time the widget is redrawn.
    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. incremental plotting
    By shiv.072007 in forum Qt Programming
    Replies: 3
    Last Post: 26th June 2012, 10:40
  2. Plotting in 3D
    By FelixB in forum Qwt
    Replies: 1
    Last Post: 1st October 2010, 16:26
  3. Using QGLWidget for plotting
    By llemes4011 in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2010, 14:40
  4. Plotting polylines
    By Indalo in forum Qwt
    Replies: 0
    Last Post: 22nd December 2009, 08:51
  5. Plotting points
    By afflictedd2 in forum Qt Programming
    Replies: 8
    Last Post: 26th February 2009, 08:20

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.