对 QMainWindow 的大小调整做出反应以调整小部件的大小
我如何对 QMainWindow
的大小调整做出反应?我在 QScrollArea
中有 QTextBrowsers
,我在创建它们时将它们调整为内容的大小(唯一应该滚动的是 QScrollArea
)。
现在一切正常,但如果我调整 mainWindow
的大小,QTextBrowsers
的高度不会改变,因为不会触发回流函数。
您是否有更好的想法来调整 QTextBrowser
以适应其内容?我当前的代码是:
void RenderFrame::adjustTextBrowser(QTextBrowser* e) const {
e->document()->setTextWidth(e->parentWidget()->width());
e->setMinimumHeight(e->document()->size().toSize().height());
e->setMaximumHeight(e->minimumHeight());
}
parentWidget()
是必要的,因为在小部件本身上运行 width()
始终返回 100,无论实际大小如何。
How do I react on the resize of a QMainWindow
? I have QTextBrowsers
in a QScrollArea
and I adjust them to the size of the content on creating them (the only thing that should scroll is the QScrollArea
).
Everything works for now, but if I resize the mainWindow
, the height of the QTextBrowsers
isn't changed, because the reflow function isn't triggered.
Do you have any better idea to adjust a QTextBrowser
to it's content? My current code is:
void RenderFrame::adjustTextBrowser(QTextBrowser* e) const {
e->document()->setTextWidth(e->parentWidget()->width());
e->setMinimumHeight(e->document()->size().toSize().height());
e->setMaximumHeight(e->minimumHeight());
}
The parentWidget()
is necessary because running width()
on the widget itself returns always 100, regardless of the real size.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果只有文本或 html,您可以使用
QLabel
代替,因为它已经根据可用空间调整其大小。您必须使用:才能获得与 QTextBrowser 几乎相同的行为。
如果您确实想使用
QTextBrowser
,您可以尝试这样的操作(改编自QLabel
源代码):If there is only text or html, you could use
QLabel
instead, because it already adapts its size to the available space. You'll have to use:to have almost the same behavior as a
QTextBrowser
.If you really want to use a
QTextBrowser
, you can try something like this (adapted fromQLabel
source code):