pyside2 qtextedit在使用包装时不会适应自己的内容。 (制作聊天窗口)
pyside2 qtextedit在放置文本时不会更改其自身的大小。
我正在尝试创建诸如聊天窗口的内容,每个消息 - qtextedit在仅在读模式下。所有放置在QSCrollarea中的“消息”。主要目标是让消息框(下面的屏幕上的消息盒子样)调整其尺寸至内容。 错误的工作示例
已被复制了很多次。但这没有我想要的。它创建了固定的,没有可重大的QTEXTEDIT消息框。
例如,我的实际意思是,如果我们有一个单词消息,则QTEXTEDIT小部件必须成为一个单程盒,并具有消息的宽度。如果我们有一个多句子消息,则QTEXTEDIT小部件必须成为一个多冲程框(已经扩展了高度,而无需在内部滚动),并具有最大的恒定长度(我会选择)。
接下来是显示正确显示的示例 (很好的例子)
The PySide2 QTextEdit doesn't change it's own size when the text is placed in.
I'm trying to create something like chat window, where every message - QTextEdit in OnlyRead mode. All the 'messages' placed in QScrollArea. The main goal is to let message-boxes (message-boxeslike on the screen below) adjust their size to content.
wrong working example
I tried this code
https://ru.stackoverflow.com/questions/1408239/Как-сделать-двухсторонний-чат-в-qt-pyqt/1408264#1408264
which has been copy-pasted lots of times. But it doesn't do what i want. It creates a fixed, no resizable QTextEdit message-boxes.
As example what i actually mean, if we have a single-word message, QTextEdit widget must become a single stroke box, with width of the message. If we have a multi-sentences message, QTextEdit widget must become a multi-stroke box (already expanded in height, without the need to scroll it inside), with maximum constant length(which i ll choose).
Next is the example with correct messages displaying
(good example)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了实施自调整的消息,需要一些预防措施。
正如我在答案中解释的那样,对相关帖子,您必须考虑布局及其尺寸之间的复杂而微妙的关系要求,这变得更加复杂,因为A 文本布局没有固定比率。
基于文本设置宽度的主要问题是它可以更改显示它所需的高度,这可能会导致递归,并且由于大小基于 current viewport大小,因此结果是
MinimumSizeHint()
将始终在一定量的递归调用后返回最小的大小。考虑到上述内容,我们必须对我的原始代码进行以下更改:
MINVIMMIMSIZEHINT()
至:;与链接的修改代码有所不同:最重要的是,重写样式表并没有很大的意义,设置保证金会在
> frame> framewidth()
的值中创建一个问题(这就是为什么他们从文档高度中减去100个;这当然不是一个不错的选择,因为余量应设置在布局内。In order to implement a self-adjusting message, some precautions are required.
As explained in my answer and comments to the related post, you must consider the complex and delicate relation between the dimensions of a layout and its requirement, which becomes even more complex as a text layout doesn't have a fixed ratio.
The main problem with setting the width based on the text is that it can change the height required to display it, which can cause a recursion, and since the size is based on the current viewport size, the result is that the
minimumSizeHint()
will always return the smallest possible size after a certain amount of recursive calls.Considering the above, we must do the following changes to my original code:
minimumSizeHint()
must be changed to:idealWidth()
based on the maximum size of the widget;Note that there are a couple of differences from the modified code of your link: most importantly, rewriting the stylesheet doesn't make a lot of sense, and setting the margin creates an issue with the value returned by
frameWidth()
(that's why they subtracted 100 from the document height); that is certainly not a good choice, as the margin should be set within the layout.