PDA

View Full Version : How to wrap the text in QPainter



duck88
29th July 2014, 04:49
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:


void MyText::paintEvent(QPaintEvent *)
{
QPainter p(viewport());
p.translate(0, 0);
p.rotate(90);
for(int i=0; i< this->document()->toPlainText().size(); ++i) {
QString letter = this->document()->toPlainText().mid(i,1);
p.save();
p.translate(i * 12, 0);
p.rotate(-90);
p.setLayoutDirection(Qt::LeftToRight);
p.drawText(this->rect(), Qt::AlignRight|Qt::AlignTop|Qt::TextWordWrap|Qt::T extWrapAnywhere, letter);
p.restore();
}
}
And result like image:
10531

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

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

Best regards!

wysota
29th July 2014, 12:09
Hmm... why do you inherit QTextEdit?

duck88
29th July 2014, 12:48
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,

wysota
29th July 2014, 13:53
So why not use QTextDocument and QAbstractTextDocumentLayout to do what you want?

duck88
29th July 2014, 15:25
Dear wysota,

Thanks for your advice.

Best Regards,