14

I have a QTextEdit which act as "displayer" (editable to false). The text it displays is wordwrapped. Now I do wish to set the height of this textbox so that the text fits exactly (while also respecting a maximum height).

Basically the widget (in the same vertical layout) below the layout should get as much space as possible.

How can this be achieved most easily?

feedc0de
  • 3,646
  • 8
  • 30
  • 55
paul23
  • 8,799
  • 12
  • 66
  • 149
  • 1
    cant find qtextbox in QT library – Dmitriy Kachko Feb 29 '12 at 21:19
  • meant qtextedit, fixed (with link) – paul23 Feb 29 '12 at 21:24
  • If you put the QTextEdit inside another QScrollArea (to set the maximum height), you could use the same code I gave there: http://stackoverflow.com/questions/7301785/react-on-the-resizing-of-a-qmainwindow-for-adjust-widgets-size – alexisdm Mar 01 '12 at 01:40
  • 1
    @paul23 you might want to check my answe on a similar request [here](http://stackoverflow.com/questions/11851020/a-qwidget-like-qtextedit-that-wraps-its-height-automatically-to-its-contents/13599165#13599165). – laurasia Nov 28 '12 at 06:39

5 Answers5

11

I found a pretty stable, easy solution using 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_()

(Forgive me, I know your question is about C++, and I'm using Python, but in Qt they're pretty much the same thing anyway).

Jingjie Yang
  • 605
  • 2
  • 10
  • 22
Tiberius
  • 111
  • 1
  • 5
2

Current size of the underlying text can be available via

QTextEdit::document()->size();

and I believe that using this we could resize the widget accordingly.

#include <QTextEdit>
#include <QApplication>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTextEdit te ("blah blah blah blah blah blah blah blah blah blah blah blah");
    te.show();
    cout << te.document()->size().height() << endl;
    cout << te.document()->size().width() << endl;
    cout <<  te.size().height() << endl;
    cout <<  te.size().width() << endl;
// and you can resize then how do you like, e.g. :
    te.resize(te.document()->size().width(), 
              te.document()->size().height() + 10);
    return a.exec();    
}
Dmitriy Kachko
  • 2,804
  • 1
  • 19
  • 21
  • Isn't the word-wrapping lost when converting to a "document"? – paul23 Feb 29 '12 at 22:41
  • try to compile the code that I put in answer, you'll see difference between size of the widget and size of the contained document. Word-wrapping is not lost. But you need to show the widget first. – Dmitriy Kachko Feb 29 '12 at 22:54
  • Well this doesn't really work out - as the size isn't set when using `setText()` command. – paul23 Mar 01 '12 at 21:54
  • You need to show the widget first to form the document. Well this is a bad style in fact, because a size of a widget in the QT is actually relying on the outer layout and size policies of the widget, but anyway you can use that idea. – Dmitriy Kachko Mar 02 '12 at 01:57
2

Unless there is something particular to the capabilities of a QTextEdit that you need, a QLabel with word wrap turned on will do exactly what you want.

Arnold Spence
  • 21,942
  • 7
  • 74
  • 67
  • 5
    Well qlabel only wordwraps on word boundaries - nor does it show scrollbars when the data gets too large. I started with using a label, but I need both feature sets it seems... – paul23 Feb 29 '12 at 22:40
1

In my case, I put my QLabel inside a QScrollArea. And if you are keen, you combine both and make your own widget.

mhtrinh
  • 21
  • 2
0

Speaking of Python, I actually found .setFixedWidth( your_width_integer ) and .setFixedSize( your_width, your_height ) quite useful. Not sure if C has similar widget attributes.

lsheng
  • 3,539
  • 10
  • 33
  • 43