How to show image on QGraphicsView
-
I have opencv Mat image and want to show it on QGraphicsView. According to I read from internet, I did:
header:QGraphicsScene *scene = new QGraphicsScene;
source(drawing slot):
cv::cvtColor(img, img, CV_BGR2RGB); QPixmap pixmap(QPixmap::fromImage(QImage(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888))); scene->addPixmap(pixmap); ui->graphicsView->setScene(scene);
When a signal emitted, this slot runs. When I run program, after emitting the signal the program crash after immediately showing image on graphicsview. What can be possible issue? If there is much better ui element for images, I can use it also.
-
As always - a function is used but the documentation is not read.
QImage ctor: "The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed."So make a deep copy of your QImage or make sure the buffer remains valid until the QImage gets destroyed.
-
@Christian-Ehrlicher So, How to do it?
-
@masa4 said in How to show image on QGraphicsView:
So, How to do it?
So make a deep copy of your QImage or make sure the buffer remains valid until the QImage gets destroyed.
See e.g. QImage::copy()
-
@Christian-Ehrlicher
header:QImage *qimg;
source:
qimg = new QImage(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888); QPixmap pixmap(QPixmap::fromImage(qimg->copy(0,0,img.cols, img.rows)));
with this still not work.
-
QPixmap pixmap(QPixmap::fromImage(QImage(img.data, img.cols, img.rows, img.step, QImage::Format_RGB888)).copy());
if this does not work than your incoming image data is not correct. You can easily look what's inside the QImage by saving it to a file.