Domanda

I have a QSpinBox that changes the coloring of an QImage on a scene. Everything works just fine. The color updates correctly. If i hold down the arrow on my QSpinBox everything works fine. I do have a problem when I hold down the the arrow on my QSpinBox for a very long time. When I hold it for about a minute or so, my application eventually stops responding and sometimes the image disappears. I was wondering if anyone knew what could be potentially causing this. Is it possible that my application gets too bogged down with signals? if so, how do i fix this?

Thanks for your help!

Here is a snippet of code. I haven't included the stuff for setting each pixel value. that I know I'm doing correctly. The changeMinColor is one of the slots for the signal of a spinbox.

void binFileDialog::changeMinColor(double value)
{
    lowColorValue = value;
    ui->maxColorSpin->setMinimum(lowColorValue + .0001);
    setBinScene();
}
void binFileDialog::setBinScene()
{
    float lowValue = lowColorValue;
    float highValue = highColorValue;
    QImage img = QImage(bFile.ncols, bFile.nrows, QImage::Format_RGB32);
    // go through and set  call img.setPixel with rgb values based on contents of bFile
    // and the min and max colors lowValue and highValue.
    QPixmap pm = QPixmap::fromImage(img);
    QGraphicsScene *scene = new QGraphichsScene;
    ui->graphicsView->setSceneRect(0,0, bFile.ncols, bFile.nrows);
    scene->addPixmap(pm);
    ui->graphicsView->setScene(scene);
}

changeMinColor is connected to the QSpinBox's valueChanged signal:

connect(ui->minColorSpin, SIGNAL(valueChanged(double)),
                          SLOT(changeMinColor(double))); 

I have also noticed that as I hold down the spinbox my memory increases. This has to be wrong. What am I forgetting? Thanks again for the help.

È stato utile?

Soluzione

setBinScene() creates a new QGraphicsScene each time which is never deleted. As each value change of the spinbox calls setBinScene(), your code piles up leaked QGraphicsScene objects. I'd suggest to avoid recreating the scene all together and just update a QGraphicsPixmapItem instead:

Initialize the scene (once):

QGraphicsScene *scene = new QGraphicsScene(this);
m_pixmapItem = new QGraphicsPixmapItem;
scene->addItem(m_pixmapItem);
ui->graphicsView->setScene(scene);

to set/update the image:

m_pixmapItem->setPixmap(pm);
ui->graphicsView->setSceneRect(0,0, bFile.ncols, bFile.nrows); //might want to avoid this one if the dimensions do not change
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top