PDA

View Full Version : howto; create an image, fill pixels with color, display on scene



robsofmar
15th August 2012, 17:25
Hi there,

I would like to create a image (on the fly), fill it with colors and then display it using a QGraphicsScene. I managed to write the code but the pixels that I made blue are not visible. Any ideas on what is wrong?

* part of the view class (the main code regarding the question is in the initializeScene function):



cGeometryViewer::cGeometryViewer(QWidget *parent) :
QGraphicsView(parent)
{
m_scene = new QGraphicsScene(0, 0, MATRIXWIDTH, MATRIXHEIGHT);
setScene(m_scene);
}

void cGeometryViewer::setGeometry(cGeometry *geometry)
{
m_geometry = geometry;
initializeScene();
}

void cGeometryViewer::initializeScene()
{
QImage img(MATRIXWIDTH, MATRIXHEIGHT,QImage::Format_ARGB32);

//just to find out if it works fill a rectangle with black pixels
for(int x=0; x<100; x++)
for(int y=0; y<100; y++)
img.setPixel(QPoint(x,y), Qt::black);

QGraphicsPixmapItem pmi(QPixmap::fromImage(img));
m_scene->addItem(&pmi);
show();
}


* and part of the main code;



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

m_geometry = new cGeometry();
m_viewer = new cGeometryViewer(ui->frameMain);
m_viewer->setGeometry(m_geometry);
}


Thanks in advance!
Rob

spirit
15th August 2012, 18:00
You should create all QGraphicsPixmapItem on the heap, because they are going to be destroyed after outing of the function scope.