1

I encountered another problem with QScrollArea, after I got help for the previous one, which was somewhat similar.

The problem now is that ensureVisible() does nothing if you create a scroll area and a label, set the label to be the scroll area's widget, and then load an image into the label - after setWidget():

This example illustrates the problem, just replace /path/to/some/image.png with some real image on your computer:

QScrollArea *scrollArea = new QScrollArea;
QLabel *label = new QLabel(scrollArea);
scrollArea->setWidgetResizable(true);
scrollArea->setWidget(label);
label->setPixmap(QPixmap("/path/to/some/image.png"));
label->setFixedSize(label->pixmap()->size());
scrollArea->ensureVisible(10000, 10000);
scrollArea->show();

If the setPixmap() is called before setWidget(), ensureVisible() will work.

Also, the problem is reproducible even though I call setWidgetResizable() and even setFixedSize().

Why does this happen, and is it possible to make ensureVisible() work without changing the order of setWidget() and setPixmap()?

Community
  • 1
  • 1
sashoalm
  • 75,001
  • 122
  • 434
  • 781

1 Answers1

1

When you call ensureVisible(10000, 10000); the scrollArea hasn't adjusted the widget's size yet. That is why it won't work.

If you create a slot that calls ensureVisible and use QTimer::singleShot to call that slot with the timeout set to 0 (you can also use QMetaObject::invokeMethod with queued connection), it will work, even if you set the scroll area's widget before you set the pixmap on the label.

What also works is, if you call ensureVisible after you call show. But this only works if your scrollArea is a top level window. If you embed it to a widget, it will not work.

thuga
  • 12,601
  • 42
  • 52
  • Then why does it work if `setPixmap()` is before `setWidget()`? Besides, the widget is resized properly (thanks to your previous answer), only `ensureVisible()` doesn't work. – sashoalm Mar 26 '14 at 14:04
  • @sashoalm It doesn't seem to work properly even if you set the pixmap before calling `setWidget`. – thuga Mar 26 '14 at 14:24
  • Sorry for replying so late, but I had other things and had forgotten about the question. When I swap the `label->setPixmap` and `scrollArea->setWidget` lines, the image is shown initially scrolled on my computer. Is your image big enough? It might be that there's no need to scroll, and that's why it doesn't. – sashoalm Mar 27 '14 at 13:52
  • @sashoalm My image was 600x600. The window size was well below that. The scroll bars were visible. I tried to make it scroll to the bottom right corner, but it only scrolled maybe 50 pixels to the right and down. But when I used `QTimer::singleShot` it scrolled to the bottom right corner properly. The order of `label->setPixmap` and `scrollArea->setWidget` seemed to make no difference. – thuga Mar 27 '14 at 14:37
  • Can you try with a bigger image, like 1000x1000? It still might be because of that, because in my case, [the scroll bars moved, but not all the way](http://imgur.com/1OXI25A) (don't know why). The image was about 1500x780. – sashoalm Mar 27 '14 at 14:48
  • @sashoalm It seems to scroll close to the bottom right corner but not quite. I'm not sure what's with that. – thuga Mar 28 '14 at 06:57