Javier Urrestarazu
Javier Urrestarazu

Reputation: 83

QGraphicsView additem() getting bigger

I have a signal that triggers the following slot on QT:

void MainWindow::stream_frame_changed(bool change, const QImage& image)
{
    if (stream == true && change == true)
    {
        QPixmap aux = QPixmap::fromImage(image);
        item->setPixmap(aux);
        ui->graphicsView->scene()->addItem(item);
    }
}

The signal is emmited every 100 milliseconds. I am noticing that there is a memory leak, and it makes sense, because I am adding many items to the scene without ever deleting them.

I do not know how to delete the previous items. If i execute the clear() function, the window closes, so it is not an option apparently.

Upvotes: 0

Views: 77

Answers (1)

eyllanesc
eyllanesc

Reputation: 244369

Instead of creating new items you must reuse the existing item:

*.cpp

on constructor:

ui->graphicsView->scene()->addItem(item);

and

void MainWindow::stream_frame_changed(bool change, const QImage& image)
{
    if (stream == true && change == true)
    {
        QPixmap aux = QPixmap::fromImage(image);
        item->setPixmap(aux);
    }
}

Upvotes: 1

Related Questions