I am writing a code to show a video in a graphicsView QObject. It goes like this:
void MainWindow::UpdateVideo()
{
cap >> frame;
cvtColor(frame, frame,CV_BGR2RGB);
QImage Qframe = QImage((uchar*) frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
QPointer<QGraphicsScene> VideoScene = new QGraphicsScene;
VideoScene->addPixmap(QPixmap::fromImage(Qframe));
ui->VideoView->setScene(VideoScene);
ui->VideoView->show();
}
void MainWindow::on_pushButton_2_clicked()
{
cap.open("test1.mpg");
if(!cap.isOpened())
cout<<"CANNOT OPEN FILE"<<endl; //or you can put some error message
QPointer<QTimer> UpdateVideo = new QTimer;
connect(UpdateVideo, SIGNAL(timeout()), this, SLOT(UpdateVideo()));
UpdateVideo->start(10);
}
As you can see, the slot on_pushButton_2_clicked() will call slot UpdateVideo() with the timer every 10ms after the first click. I want to get the video to displays flawlessly but after a few seconds the following error appears on the application output of qt creator:
Qimage: out of memory, returning null image.
And then the graphicsView frame goes blank. Can you tell me where is the memory leak?
I don't think that QPointer does what you think. You are constantly allocating new
QGraphicScene
s. That's why you are running out of resources.QGraphicsPixmapItem*
returned byaddPixmap
should be remebered.setPixmap (const QPixmap &pixmap )
method ofQGraphicsPixmapItem
should be called to update the image.For example:
Edit:
You are also constantly allocating new
QTimer
s. A single timer should be a member of the window as it needs to survive the end of the method.