0

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.

Kara
  • 6,115
  • 16
  • 50
  • 57
user1216527
  • 145
  • 1
  • 1
  • 11
  • I'm guessing that it is a piling on of updates. It's not the wrong signal/ or slot function because it works fine until it has been held down for a long time. I wanted to know if there is a way to prevent a piling on of updates, like slowing down the spinner. – user1216527 May 01 '13 at 19:32
  • Well, I've added a bit of code. I'm not sure what to include. Because it works fine until it has been bogged down by signals. – user1216527 May 01 '13 at 19:57
  • connect(ui->minColorSpin, SIGNAL(valueChanged(double)),SLOT(changeMinColor(double))); – user1216527 May 01 '13 at 20:01
  • turns out I just had to fix a couple of lines. thanks anyways! – user1216527 May 01 '13 at 22:13

1 Answers1

2

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
Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • This is great! Thank you. It worked! I did things a little differently. I initialize the scene once, and I make sure I clear it before I set it. Thanks! – user1216527 May 01 '13 at 22:11
  • Creating a custom QGraphicsPixmapItem, overloading `paint`, and only rebuilding the pixmap when necessary from the paint method (or painting directly) might be even more efficient. – Mat May 02 '13 at 04:22