PDA

View Full Version : Dynamic font size in QTextEdit



amitm02
27th April 2017, 12:12
I have a document in HTML format. The fonts are specified in 'pt' units (e.g 14pt).
I also have page width specification (e.g 400pt), so when the text line is longer than 400, it will wrap to the next line.

I would like the appeared font size in QTextEdit to match the window widget size. Such that if I make the window smaller, the fonts will get smaller. If i make the window bigger, the appeared fonts will get bigger. In any case, the line will break at the exact same place.
Similar to the editing experience in Pages or Word.

What is the best way to accomplish that?

Santosh Reddy
3rd May 2017, 13:18
Something like this...



class TexEdit : public QTextEdit
{
public:
explicit TexEdit(QWidget * parent = 0) : QTextEdit(parent) { }

protected:
void resizeEvent(QResizeEvent * event)
{
QTextEdit::resizeEvent(event);

const int pointSize = event->size().width() / 12;
qDebug() << "New Size" << event->size() << pointSize;

selectAll();
setFontPointSize(pointSize);
}
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

TexEdit widget;
widget.setPlainText("Hello World");
widget.show();

return app.exec();
}