PDA

View Full Version : "Render" into QImage



Imons
29th March 2010, 10:15
Hi all,

i have a little probelm. I wanted to write a renderer and use qt as a frontend. Now my question is, how do i display and update my image _efficently_. As the rendering process needs time there are only a few pixels which gets updated in a certain interval.

I've written a little program which shows how i would solve this problem. But i'm not very happy with this solution because i don't know how much copying of the image is done under the hood. Maybe theres a better way?

Here the code:



class QMainWin : public QMainWindow
{
Q_OBJECT

public:
QMainWin();

private:
QScrollArea *area;
QImage *image;
QLabel *imgLabel;
int x, y;

private slots:
void drawPixels();
};




QMainWin::QMainWin()
{
image = new QImage(200, 200, QImage::Format_ARGB32);
imgLabel = new QLabel;
imgLabel->setPixmap(QPixmap::fromImage(*image));

area = new QScrollArea;
area->setBackgroundRole(QPalette::Dark);
area->setWidget(imgLabel);

setCentralWidget(area);

QAction* action = new QAction(tr("Draw something"), this);
connect(action, SIGNAL(triggered()), this, SLOT(drawPixels()));

QMenu* menu = menuBar()->addMenu(tr("Drawing"));
menu->addAction(startAction);

x = y = 0;
}

void QMainWin::drawPixels()
{
for(int i = y; i < y+20; i++)
for(int j = x; j < x+20; j++)
image->setPixel(j, i, qRgb(255, 0, 0));

x += 20;
y += 20;

// This line is needed, else the picture doesn't get updated
imgLabel->setPixmap(QPixmap::fromImage(*image));
imgLabel->update();
}


I've omitted forward declarations, includes and so on.
Sry for the bad english.

Regards
Imons

wysota
29th March 2010, 13:45
What exactly are you not satisfied with? QImage (and QPixmap as well) is an implicitly shared class. If you copy an object to a new one only a shallow copy is performed but when you change at least one bit of information of any of the incarnations, a deep copy of the whole image is made for the instance being changed.

Anyway, the loop where you do the painting can be replaced by a single call to QPainter::drawRect(). Of course you need to modify the way you operate on QPixmap but then you can get rid of QImage at all.