Loading image from subclass QGraphicsview
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:
Code:
definitions in public "TesGv Class"
Code:
//In mainWindow Class -> PushButton "Upload Image"
ui->graphicsView->getImageFileName(); // call function in subclass to get and set image
ui->graphicsView->setScene(ui->graphicsView->scene)
Code:
//In subClass "TestGV" "getImageFileName
dialog.setNameFilter(tr("Images (*.png *.jpg *.bmp *)"));
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());
}
}
pixmap.fromImage(image1);
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
Re: Loading image from subclass QGraphicsview
Code:
//In mainWindow Class -> PushButton "Upload Image"
ui->graphicsView->setScene(ui->graphicsView->scene)
ui->graphicsView->getImageFileName(); // call function in subclass to get and set image
Code:
//In subClass "TestGV" "getImageFileName
dialog.setNameFilter(tr("Images (*.png *.jpg *.bmp *)"));
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());
}
}
pixmap.fromImage(image1);
//scene = new QGraphicsScene(this);
newImage = scene->addPixmap(pixmap);
Re: Loading image from subclass QGraphicsview
Thanks,
Based on QDebug output on width & height of image
my QImage works
my QPixmap does not get initialised at all :confused:
Code:
pixmap.fromImage(image1);
Loading from QImage directly... works
Code:
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
Re: Loading image from subclass QGraphicsview
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
Code:
pixmap = pixmap.fromImage(image1);
and even better is this
Code:
pixmap
= QPixmap::fromImage(image1
);