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:
{
Q_OBJECT
public:
QMainWin();
private:
int x, y;
private slots:
void drawPixels();
};
class QMainWin : public QMainWindow
{
Q_OBJECT
public:
QMainWin();
private:
QScrollArea *area;
QImage *image;
QLabel *imgLabel;
int x, y;
private slots:
void drawPixels();
};
To copy to clipboard, switch view to plain text mode
QMainWin::QMainWin()
{
imgLabel
->setPixmap
(QPixmap::fromImage(*image
));
area
->setBackgroundRole
(QPalette::Dark);
area->setWidget(imgLabel);
setCentralWidget(area);
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
I've omitted forward declarations, includes and so on.
Sry for the bad english.
Regards
Imons
Bookmarks