PDA

View Full Version : Loading image from subclass QGraphicsview



2lights
26th August 2013, 08:09
How do I upload an image to a graphicsview from a subclass of QGraphicsview

I created new class "TestGV" as a child class of QGraphicsView
I promoted my graphicsView widget on form to "TestGV" class

Where do I create the scene in the mainwindow class or the "TestGV" class
This is what i got, but does not work:confused:


definitions in public "TesGv Class"
QGraphicsScene *scene;
QGraphicsPixmapItem *newImage;




//In mainWindow Class -> PushButton "Upload Image"
ui->graphicsView->getImageFileName(); // call function in subclass to get and set image
ui->graphicsView->scene = new QGraphicsScene(this);
ui->graphicsView->setScene(ui->graphicsView->scene)




//In subClass "TestGV" "getImageFileName
QFileDialog dialog(this, "Upload");
dialog.setNameFilter(tr("Images (*.png *.jpg *.bmp *)"));
dialog.setViewMode(QFileDialog::Detail);
QString imageFileName1;
if(dialog.exec())
{
imageFileName1 = dialog.selectedFiles().first();
}

image1.load(imageFileName1);
image1 = image1.convertToFormat(QImage::Format_RGB32);

QRgb colour;
for (int f1=0; f1<image1.width(); f1++) {
for (int f2=0; f2<image1.height(); f2++) {
colour = image1.pixel(f1, f2);
image1.setPixel(f1, f2, QColor((qRed(colour) + qGreen(colour) + qBlue(colour))/3).rgb());
}
}
QPixmap pixmap;
pixmap.fromImage(image1);
scene = new QGraphicsScene(this);
newImage = scene->addPixmap(pixmap);


The above code does display Dialogbox & user can select image of choice
But nothing is uploaded
How do I set the scene from the subClass "testGV"
And do the uploading also from "TestGV"

Kindly provide me with example code :)

Kind Regards

Santosh Reddy
26th August 2013, 08:29
//In mainWindow Class -> PushButton "Upload Image"
ui->graphicsView->scene = new QGraphicsScene(this);
ui->graphicsView->setScene(ui->graphicsView->scene)
ui->graphicsView->getImageFileName(); // call function in subclass to get and set image




//In subClass "TestGV" "getImageFileName
QFileDialog dialog(this, "Upload");
dialog.setNameFilter(tr("Images (*.png *.jpg *.bmp *)"));
dialog.setViewMode(QFileDialog::Detail);
QString imageFileName1;
if(dialog.exec())
{
imageFileName1 = dialog.selectedFiles().first();
}

image1.load(imageFileName1);
image1 = image1.convertToFormat(QImage::Format_RGB32);

QRgb colour;
for (int f1=0; f1<image1.width(); f1++) {
for (int f2=0; f2<image1.height(); f2++) {
colour = image1.pixel(f1, f2);
image1.setPixel(f1, f2, QColor((qRed(colour) + qGreen(colour) + qBlue(colour))/3).rgb());
}
}

QPixmap pixmap;
pixmap.fromImage(image1);
//scene = new QGraphicsScene(this);
newImage = scene->addPixmap(pixmap);

2lights
26th August 2013, 08:46
Thanks,


Based on QDebug output on width & height of image
my QImage works
my QPixmap does not get initialised at all :confused:


pixmap.fromImage(image1);



Loading from QImage directly... works


newImage = scene->addPixmap(QPixmap::fromImage(image1));

not sure if i'll need Qpixmap later though
Would like to know why QPixmap is not working accordingly


Kind Regards
Thanks

Santosh Reddy
26th August 2013, 09:22
fromImage() is a static member function, you need to copy the converted pixmap into a variable (you might have ignored the compiler warning, or may be even disabled them). Anyways this will work


QPixmap pixmap;
pixmap = pixmap.fromImage(image1);

and even better is this


QPixmap pixmap;
pixmap = QPixmap::fromImage(image1);