PDA

View Full Version : plotting 2d in qt



buzzz321
12th July 2012, 08:20
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 8006 with this code


RenderArea::RenderArea(QWidget *parent) :
QWidget(parent) {
image = new QImage(500, 500, QImage::Format_ARGB32_Premultiplied);
image->fill(0);
for (int n = 0; n < 500; ++n) {
for (int i = 0; i < 500; ++i) {
image->setPixel(i, n, 0x00ff00ff);
}
}

setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
this->update();
}

void RenderArea::paintEvent(QPaintEvent * /* event */) {
QPainter painter(this);
QRect rect(0, 0, image->width(), image->height());
painter.drawImage(rect, *image);

this->update();
}

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

wysota
12th July 2012, 21:14
I'm suprised you see anything at all apart the white background.

This is the correct code:


#include <QtGui>

class Widget : public QWidget {
public:
Widget() : QWidget() {
img = QImage(500,500, QImage::Format_ARGB32_Premultiplied);
img.fill(0);
for(int n = 0; n < 500; ++n)
for(int i = 0; i< 500; ++i)
img.setPixel(i, n, qRgb(255,0,255));

setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
}
protected:
void paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.drawImage(img.rect(), img);
}
private:
QImage img;
};

int main(int argc, char **argv) {
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}

As for the rest of your question --- "it depends".

buzzz321
12th July 2012, 21:55
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!

wysota
12th July 2012, 22:19
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.

buzzz321
12th July 2012, 22:33
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)

wysota
12th July 2012, 23:03
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.

buzzz321
13th July 2012, 08:53
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?

wysota
13th July 2012, 08:56
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.