PDA

View Full Version : QTextEdit Justify align making work



dec0ding
12th January 2006, 17:20
If I post on wrong forum, just move the post ;) couldn't find better one (no cathegory for snipplets?)

I have bad experience with QTextEdit's align Justify features. The Align methods for putting Justified align are not working, although the three other Qt:: nodes do work, like Qt::AlignCenter, Qt::AlignRight, Qt::AlignLeft. However Qt::AlignJustify doesn't, and it makes the text in QTextEdit object goes nuts. However Trolltech put warning at Qt refference about this flag:


Qt::AlignJustify - Justifies the text in the available space. Does not work for everything and may be interpreted as AlignAuto in some cases.


So here is a way for making your text in QTextEdit Object be Aligned as 'Justify':


QTextEdit *mainTextEdit=new QTextEdit(this); // the object where the text is displayed
…
connect(mainTextEdit, SIGNAL(cursorPositionChanged( int , int )), this, SLOT(toggleIfStyle(int, int)) );
…
// then in the toggleIfStyle(int,int) method we have:

void class::toggleIfStyle(int a, int b)
{
QString textParagragh(mainTextEdit->text(a)); // the text of the current paragraph

mainTextEdit->insertAt(”<p align=justify>”, a, 0); // QTextEdit reads HTML tags, so we enter the align=justify tag
mainTextEdit->insertAt(”</p>”, a, mainTextEdit->paragraphLength(a)); // close the tag

mainTextEdit->setText(mainTextEdit->text()); // the text has to be re-displayed
}

Hope sometimes, to someone will help
:)

Bye

Nemo
13th January 2006, 09:44
Hey ,

Thanks. This is good. I have also notice that if you change a textEdit Content with setHtml() it cause alignment to get lost. Have you faced this ?

dec0ding
13th January 2006, 13:02
Hey ,

Thanks. This is good. I have also notice that if you change a textEdit Content with setHtml() it cause alignment to get lost. Have you faced this ?

yes, just tested, Qt 3.3.5 and it does destroy the alignment.

Howver, we are lucky Text Edit's that parse HTML code ;)