PyQt:当它们的内容改变时如何处理小部件的自动调整大小 [英] PyQt: how to handle auto-resize of widgets when their content changes

查看:892
本文介绍了PyQt:当它们的内容改变时如何处理小部件的自动调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我会用两个简单的场景来说明我的问题:



情况1:



我有一个QLineEdit小部件。有时,当我使用QLineEdit.setText()更改其内容时,单行字符串不适合在当前大小的小部件。



情景2:


。我必须选择小部件并使用箭头键双向滚动字符串才能查看。

我有一个QTextEdit小部件。有时,当我使用QTextEdit.setHtml()更改其内容时,呈现的HTML内容不适合在当前大小的小部件。小部件开始显示水平和/或垂直滚动​​条,我可以使用它们滚动HTML内容。



在这种情况下,我想要的是有一些逻辑,决定在内容改变之后,新内容将不再适合小部件,并自动增加小部件大小,以便一切适合。



如何处理这些情况?
我使用的是PyQt4。



编辑:在读取注释和第一个答案(提到在窗口小部件中输入内容)后,我浏览了问题一次。我很不高兴地发现一个可怕的错字。我的意思是QTextBrowser当我写QTextEdit,我的道歉,误导你。这是:我有一个小部件,呈现HTML代码,我改变,我希望小部件增长到足以显示一切没有滚动条。



至于QLineEdit而不是QLabel - 我去了QLineEdit,因为我注意到,我不能选择文本从QLabel与鼠标复制它。使用QLineEdit是可能的。

解决方案

我在C ++中回答,因为这是我最熟悉的,你的问题不是特定于PyQt。



通常,你只需要调用 QWidget :: updateGeometry() sizeHint()可能已经改变时,就像你需要调用 QWidget :: update()



但是,您的问题是,当文本是文本时, sizeHint()添加到 QLineEdit QTextEdit 。原因:人们不希望他们的对话增长,因为他们的类型:)



也就是说,如果你真的想成长为你的类型在你需要从它们继承的小部件中的行为,并重新实现 sizeHint() minimumSizeHint() ,并且可能 setText() append()等调用 updateGeometry / code>所以大小提示的变化被注意到。



sizehint计算将不是微不足道,并且对于 QLineEdit QTextEdit (这是秘密地一个 QAbstractScrollArea ),但你可以看看 sizeHint() minimumSizeHint() / code>,它有一个模式来做你想要的: QComboBox :: AdjustToContents



编辑:你的两个usecases(QTextBrowser w / o滚动条和QLineEdit而不是QLabel只是为了选择其中的文本)可以通过使用一个QLabel和一个最近足够的Qt解决。 QLabel在Qt 4.2中获得了链接点击通知和所谓的文本交互标志(其中之一是TextSelectableByMouse)。我能够证明的唯一的区别是,加载新的内容不是自动的,没有历史,没有微焦点提示(即从链接到链接)在QLabel。


I am having some issues with the size of qt4 widgets when their content changes.

I will illustrate my problems with two simple scenarios:

Scenario 1:

I have a QLineEdit widget. Sometimes, when I'm changing its content using QLineEdit.setText(), the one-line string doesn't fit into the widget at its current size anymore. I must select the widget and use the arrow keys to scroll the string in both directions in order to see it all.

Scenario 2:

I have a QTextEdit widget. Sometimes, when I'm changing its content using QTextEdit.setHtml(), the rendered HTML content doesn't fit into the widget at its current size anymore. The widget starts displaying horizontal and/or vertical scroll bars and I can use them to scroll the HTML content.

What I would want in such scenarios is to have some logic that decides if after a content change, the new content won't fit anymore into the widget and automatically increase the widget size so everything would fit.

How are these scenarios handled? I'm using PyQt4.

Edit: after reading both the comment and the first answer (which mentions typing content into the widget), I went over the question one more time. I was unpleasantly surprised to find out a horrible typo. I meant QTextBrowser when I wrote QTextEdit, my apologies for misleading you. That is: I have a widget which renders HTML code that I'm changing and I would want the widget to grow enough to display everything without having scrollbars.

As for QLineEdit instead of QLabel - I went for QLineEdit since I've noticed I can't select text from a QLabel with the mouse for copying it. With QLineEdit it is possible.

解决方案

I'm answering in C++ here, since that's what I'm most familiar with, and your problem isn't specific to PyQt.

Normally, you just need to call QWidget::updateGeometry() when the sizeHint() may have changed, just like you need to call QWidget::update() when the contents may have changed.

Your problem, however, is that the sizeHint() doesn't change when text is added to QLineEdit and QTextEdit. For a reason: People don't expect their dialogs to grow-as-they-type :)

That said, if you really want grow-as-you-type behaviour in those widgets you need to inherit from them and reimplement sizeHint() and minimumSizeHint() to return the larger size, and potentially setText(), append() etc. to call updateGeometry() so the sizehint change is noticed.

The sizehint calculation won't be entirely trivial, and will be way easier for QLineEdit than for QTextEdit (which is secretly a QAbstractScrollArea), but you can look at the sizeHint() and minimumSizeHint() implementations for inspiration (also the one for QComboBox, which has a mode to do exactly what you want: QComboBox::AdjustToContents.

EDIT: Your two usecases (QTextBrowser w/o scrollbars and QLineEdit instead of QLabel just for selecting the text in there) can be solved by using a QLabel and a recent enough Qt. QLabel has gained both link-clicking notification and so-called "text-interaction flags" (one of which is TextSelectableByMouse) in Qt 4.2. The only difference that I was able to make out is that loading new content isn't automatic, there's no history, and there's no micro focus hinting (ie. tabbing from link to link) in QLabel.

这篇关于PyQt:当它们的内容改变时如何处理小部件的自动调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆