PDA

View Full Version : Using QGraphicsScene instead of OpenGL



ntp
3rd March 2008, 18:32
Hello,

I am currently using openGL to do my image drawing but I thought I would write a test program to see if I could use all QT instead. What I need to do is to display an image that is changing rapidly (up to 64 times a second). I was using glDrawPixels but once I started doing zooming and drawing lines on it, I started to have problems.

I am crashing deep in fillRect(rr.toRect(), &textureData, d); inside of QRasterPaintEngine::drawImage.

Am I approaching this wrong? Has anyone gotten this to work? I have put up a test string in the window and that appears so something can appear in my scene.

I have a class that gets created by my main.cpp and it looks like this:


imageTester::imageTester(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);

m_imageScene = new ImageScene();

m_view = new QGraphicsView(m_imageScene);
m_view->show();
}


Then my imageScene (derived from QGraphcisScene) starts up a timer in the constructor and when the timer goes off, it does the following:





if (m_pixmap != NULL) {
removeItem(m_pixmap);
delete m_pixmap;
m_pixmap = NULL;
}
else {
setSceneRect(0,0, w, h);
}

unsigned char* displayBuffer = getBuffer();

// inside of getBuffer: displayBuffer = (unsigned char*) (malloc ((w*h) * 3));
// and then filled with RGB values for w * h

if (displayBuffer != NULL) {
QImage* image = new QImage(displayBuffer, w, h, QImage::Format_RGB32);
QPixmap pixmap = QPixmap::fromImage(*image);
m_pixmap = addPixmap(pixmap);
m_pixmap->setVisible(true);
update();
free (displayBuffer);
}



Side question: Can I free the image at the end of this method along with displayBuffer?

Thanks for any help.

wysota
3rd March 2008, 23:02
Don't recreate the item each time you change the image. Simply set the new image on the already existing item. And create the item on stack and not on heap. Apart from that your approach seems fine (although using OpenGL for that is ok too).

ntp
7th April 2008, 21:16
Here is a followup. Turns out the real problem was that I wasn't allocating enough memory for the incoming image. It needed to be displayBuffer = (unsigned char*) (malloc ((w*h) * 4)); not (... * 3).

I am using Format_RGB32 but I was focusing on the RGB part and needed to allocate 32 bits and the last 8 bits are used for padding.