QScrollArea :: ensureVisible()和QScrollArea :: setWidget() [英] QScrollArea::ensureVisible() and QScrollArea::setWidget()
问题描述
在我得到 QScrollArea 的另一个问题>前一个,这有点类似。
I encountered another problem with QScrollArea
, after I got help for the previous one, which was somewhat similar.
现在的问题是 ensureVisible()
之后 c> setWidget / code>:
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()
:
此示例说明了问题,只需替换 /path/to/some/image.png
在您的计算机上有一些真实的形象:
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();
如果 setPixmap()
strong> before setWidget()
, ensureVisible()
将工作。
If the setPixmap()
is called before setWidget()
, ensureVisible()
will work.
此外,即使我调用 setWidgetResizable()
甚至 setFixedSize()
。
Also, the problem is reproducible even though I call setWidgetResizable()
and even setFixedSize()
.
为什么会发生这种情况,并且可以使 ensureVisible()
setWidget()
和 setPixmap()
?
Why does this happen, and is it possible to make ensureVisible()
work without changing the order of setWidget()
and setPixmap()
?
推荐答案
当调用 ensureVisible(10000,10000);
scrollArea还没有调整窗口小部件的大小。这就是为什么它不工作。
When you call ensureVisible(10000, 10000);
the scrollArea hasn't adjusted the widget's size yet. That is why it won't work.
如果您创建一个调用 ensureVisible
并使用 QTimer :: singleShot
调用该插槽的超时设置为0(你也可以使用
QMetaObject :: invokeMethod
与排队连接),它将工作,即使你设置滚动区域的小部件,然后在标签上设置像素映射。
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.
如果在调用 show $后调用
ensureVisible
c $ c>。但这只有在你的 scrollArea
是顶层窗口时才有效。如果你将它嵌入到一个小部件,它将无法工作。
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.
这篇关于QScrollArea :: ensureVisible()和QScrollArea :: setWidget()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!