且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PyQt:当它们的内容改变时如何处理小部件的自动调整大小

更新时间:2023-02-05 23:11:54

我在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.