2012-08-07 13 views
6

我使用一些QTextEdit小部件創建窗體。像QTextEdit這樣的QWidget,它自動將其高度自動包裹到其內容中?

中的QTextEdit的默認高度超過單個文本行和內容高度超過的QTextEdit的高度,它會創建一個滾動條滾動的內容。

我想重寫此行爲,以創建一個QTextEdit寧願包裹它的高度它的內容。這意味着默認高度將是一行,並且在包裝或輸入新行時,QTextEdit會自動增加其高度。每當內容高度超過QTextEdit的高度時,後者不應創建滾動條,而只需增加高度。

我該如何去做這件事?謝謝。

回答

6

這幾乎是完全一樣的問題,我回答了一天大約做一個QTextEdit調整其效應初探高度含量的變化:PySide Qt: Auto vertical growth for TextEdit Widget

我回答,而不是標誌着一個重複的,因爲我懷疑其可能你想有一個這個變化。讓我知道如果你要我擴大這個答案:

另一個問題有多個部分。這裏是增長的高度widget的摘錄:

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) 
+0

這正是我所需要的。雖然沒有遇到過我的搜索字詞。謝謝! – Benjamin 2012-08-08 19:30:19

+0

也(我看不出在你的其他職位)我不得不連接下列以包裹的QTextEdit的高度,它的文檔時,前者被調整:'self.document()documentLayout()documentSizeChanged.connect。 (self.wrapHeightToContents)'。 – Benjamin 2012-08-09 04:37:01

+0

如果您在我的示例中看到我正在使用更高的不同信號。我沒有需要您連接到在C++中 – jdi 2012-08-09 04:44:34

4

以下代碼設置一個QTextEdit控件內容的高度:

# 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() 

# now that we have a document we can use it's size to set the QTextEdit's size 
# also we add the margins 
text_edit.setFixedHeight(text_edit.document().size().height() + text_edit.contentsMargins().top()*2) 

# finally we add the QTextEdit to our layout 
grid.addWidget(text_edit) 

我希望這有助於。

+1

這是一個非常有趣的技巧,但我認爲一定有其他方法。但是我的另一部分意識到這可能是實踐中最好的方式,因爲您不必計算幀寬度等等,並將其全部計算出來。 – neuronet 2015-12-31 21:15:54

+0

技術上'contentsMargins()。頂部()+ contentsMargins()。底部()'比'更正確contentsMargins()。頂部()* 2'儘管這值通常是相同的。 – 2016-07-04 19:26:49

相關問題