相关文章推荐
眉毛粗的乌冬面  ·  组件使用 v-model -- 非兼容 · ...·  5 月前    · 
精明的蚂蚁  ·  v-model 调用函数怎么传参 - CSDN文库·  5 月前    · 
完美的皮带  ·  pyspark保存文件遇坑及出坑过程-阿里云 ...·  1 年前    · 
酷酷的牙膏  ·  oracle数据库java.sql.SQLE ...·  1 年前    · 
Code  ›  像QTextEdit这样的QWidget会自动将其高度换行到内容中吗?开发者社区
scroll
https://cloud.tencent.com/developer/ask/sof/102398653
飘逸的热带鱼
11 月前
首页
学习
活动
专区
工具
TVP 最新优惠活动
返回腾讯云官网
提问

问 像QTextEdit这样的QWidget会自动将其高度换行到内容中吗?

Stack Overflow用户
提问于 2012-08-08 01:24:13
EN

我正在创建一个带有一些QTextEdit小部件的表单。

QTextEdit的默认高度超过了一行文本,当内容的高度超过QTextEdit的高度时,它会创建一个滚动条来滚动内容。

我想重写这个行为来创建一个QTextEdit,它更愿意将它的高度包装到它的内容中。这意味着默认高度为一行,换行或输入新行时,QTextEdit将自动增加其高度。当内容高度超过QTextEdit的高度时,QTextEdit不应该创建滚动条,而只是增加高度。

我该怎么做呢?谢谢。

2 10.7K 0 票数 9
EN
python
qt4
pyqt4
qtextedit

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-08 14:25:57

这几乎和我前几天回答的关于让QTextEdit调整高度以响应内容更改的问题一模一样: PySide Qt: Auto vertical growth for TextEdit Widget

我正在回答,而不是标记副本,因为我怀疑你可能想要这个的变体。如果你想让我扩展这个答案,请告诉我:

另一个问题有多个部分。下面是growing height小部件的摘录:

代码语言: javascript
复制
class Window(QtGui.QDialog):
    def __init__(self):
        super(Window, self).__init__()
        self.resize(600,400)
        self.mainLayout = QtGui.QVBoxLayout(self)
        self.mainLayout.setMargin(10)
        self.scroll = QtGui.QScrollArea()
        self.scroll.setWidgetResizable(True)
        self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.mainLayout.addWidget(self.scroll)
        scrollContents = QtGui.QWidget()
        self.scroll.setWidget(scrollContents)
        self.textLayout = QtGui.QVBoxLayout(scrollContents)
        self.textLayout.setMargin(10)
        for _ in xrange(5):
            text = GrowingTextEdit()
            text.setMinimumHeight(50)
            self.textLayout.addWidget(text)
class GrowingTextEdit(QtGui.QTextEdit):
    def __init__(self, *args, **kwargs):
        super(GrowingTextEdit, self).__init__(*args, **kwargs)  
        self.document().contentsChanged.connect(self.sizeChange)
        self.heightMin = 0
        self.heightMax = 65000
    def sizeChange(self):
        docHeight = self.document().size().height()
        if self.heightMin <= docHeight <= self.heightMax:
            self.setMinimumHeight(docHeight)
票数 6
EN

Stack Overflow用户

发布于 2012-11-28 14:33:32

下面的代码将QTextEdit构件设置为内容的高度:

代码语言: javascript
复制
# using QVBoxLayout in this example
grid = QVBoxLayout()
text_edit = QTextEdit('Some content. I make this a little bit longer as I want to see the effect on a widget with more than one line.')
# read-only
text_edit.setReadOnly(True)
# no scroll bars in this example
text_edit.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) 
text_edit.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) 
text_edit.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
# you can set the width to a specific value
# text_edit.setFixedWidth(400)
# this is the trick, we nee to show the widget without making it visible.
# only then the document is created and the size calculated.
# Qt.WA_DontShowOnScreen = 103, PyQt does not have this mapping?!
text_edit.setAttribute(103)
text_edit.show()
 
推荐文章
眉毛粗的乌冬面  ·  组件使用 v-model -- 非兼容 · vue3.x -- 系列学习 · 看云
5 月前
精明的蚂蚁  ·  v-model 调用函数怎么传参 - CSDN文库
5 月前
完美的皮带  ·  pyspark保存文件遇坑及出坑过程-阿里云开发者社区
1 年前
酷酷的牙膏  ·  oracle数据库java.sql.SQLException: 结果集已耗尽,总是跳不出while(rs.next())循环,请求高手帮忙解决!
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号