Results 1 to 5 of 5

Thread: How to wrap the text in QPainter

  1. #1
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows Android

    Post How to wrap the text in QPainter

    Hi all,

    I want to draw text input from keyboard, and display like Japanese style, from right to left and the top to bottom.
    I had implemented paintEvent() method in my class to draw text from the top to bottom, my class is inherited from QTextEdit class:

    Qt Code:
    1. void MyText::paintEvent(QPaintEvent *)
    2. {
    3. QPainter p(viewport());
    4. p.translate(0, 0);
    5. p.rotate(90);
    6. for(int i=0; i< this->document()->toPlainText().size(); ++i) {
    7. QString letter = this->document()->toPlainText().mid(i,1);
    8. p.save();
    9. p.translate(i * 12, 0);
    10. p.rotate(-90);
    11. p.setLayoutDirection(Qt::LeftToRight);
    12. p.drawText(this->rect(), Qt::AlignRight|Qt::AlignTop|Qt::TextWordWrap|Qt::TextWrapAnywhere, letter);
    13. p.restore();
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    And result like image:
    image.PNG

    But I want to text when fit height, it auto word wrap to new column like below image:
    Result.PNG

    How to wrap the text like above image?
    Thanks so much!

    Best regards!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to wrap the text in QPainter

    Hmm... why do you inherit QTextEdit?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to wrap the text in QPainter

    Hi wysota,

    Thanks for your response.

    My function like "Text Box" function in Microsoft Word, so I create a class which inherit QTextEdit and add to QWidget, user input text, change font style, change size and display that text from the top to bottom, from the right to the left, like Japanese style.

    I inherit QTextEdit, because I can get characters which user input and draw again.

    Best regards,

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to wrap the text in QPainter

    So why not use QTextDocument and QAbstractTextDocumentLayout to do what you want?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    duck88 (29th July 2014)

  6. #5
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to wrap the text in QPainter

    Dear wysota,

    Thanks for your advice.

    Best Regards,

Similar Threads

  1. How to wrap text in a label?
    By sasi in forum Qt Programming
    Replies: 5
    Last Post: 22nd October 2016, 04:14
  2. QPainter::drawText() with word wrap
    By zgulser in forum Qt Programming
    Replies: 4
    Last Post: 18th December 2013, 11:18
  3. Replies: 2
    Last Post: 9th April 2013, 06:26
  4. Long text and word wrap in QPainter
    By TomASS in forum Qt Programming
    Replies: 2
    Last Post: 11th December 2009, 11:50
  5. Wrap Text in PUSHBUTTON
    By BalaQT in forum Qt Programming
    Replies: 2
    Last Post: 16th September 2009, 13:54

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.