¡Encontré una solución bastante estable y fácil usando QFontMetrics
!
from PyQt4 import QtGui
text = ("The answer is QFontMetrics\n."
"\n"
"The layout system messes with the width that QTextEdit thinks it\n"
"needs to be. Instead, let's ignore the GUI entirely by using\n"
"QFontMetrics. This can tell us the size of our text\n"
"given a certain font, regardless of the GUI it which that text will be displayed.")
app = QtGui.QApplication([])
textEdit = QtGui.QPlainTextEdit()
textEdit.setPlainText(text)
textEdit.setLineWrapMode(True) # not necessary, but proves the example
font = textEdit.document().defaultFont() # or another font if you change it
fontMetrics = QtGui.QFontMetrics(font) # a QFontMetrics based on our font
textSize = fontMetrics.size(0, text)
textWidth = textSize.width() + 30 # constant may need to be tweaked
textHeight = textSize.height() + 30 # constant may need to be tweaked
textEdit.setMinimumSize(textWidth, textHeight) # good if you want to insert this into a layout
textEdit.resize(textWidth, textHeight) # good if you want this to be standalone
textEdit.show()
app.exec_()
(Perdóname, sé que tu pregunta es acerca de C++, y estoy usando Python, pero en Qt
Son más o menos lo mismo que de todos modos).
no puede encontrar en la biblioteca qtextbox QT –
QTextEdit significaba, fijo (con enlace) – paul23
Si ponemos la QTextEdit dentro de otro QScrollArea (para ajustar la altura máxima), se puede usar el mismo código que di allí: http: // stackoverflow .com/questions/7301785/react-on-the-resizing-of-a-qmainwindow-for-adjust-widgets-size – alexisdm