Results 1 to 2 of 2

Thread: Dynamic font size in QTextEdit

  1. #1
    Join Date
    Apr 2017
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Dynamic font size in QTextEdit

    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?
    Last edited by amitm02; 27th April 2017 at 12:13. Reason: reformatted to look better

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Dynamic font size in QTextEdit

    Something like this...

    Qt Code:
    1. class TexEdit : public QTextEdit
    2. {
    3. public:
    4. explicit TexEdit(QWidget * parent = 0) : QTextEdit(parent) { }
    5.  
    6. protected:
    7. void resizeEvent(QResizeEvent * event)
    8. {
    9. QTextEdit::resizeEvent(event);
    10.  
    11. const int pointSize = event->size().width() / 12;
    12. qDebug() << "New Size" << event->size() << pointSize;
    13.  
    14. selectAll();
    15. setFontPointSize(pointSize);
    16. }
    17. };
    18.  
    19. int main(int argc, char *argv[])
    20. {
    21. QApplication app(argc, argv);
    22.  
    23. TexEdit widget;
    24. widget.setPlainText("Hello World");
    25. widget.show();
    26.  
    27. return app.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Not able to increase font size for QTextEdit
    By shyamsundar1982 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 3rd March 2015, 08:38
  2. Replies: 1
    Last Post: 30th March 2012, 17:46
  3. Replies: 0
    Last Post: 26th October 2010, 18:59
  4. Font Height and width based on font size
    By Ghufran in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2010, 09:02
  5. change font size and button size of QMessageBox
    By nass in forum Qt Programming
    Replies: 6
    Last Post: 13th September 2006, 20:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.