Results 1 to 3 of 3

Thread: QTextEdit Justify align making work

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Default QTextEdit Justify align - making it work [a tip]

    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':

    Qt Code:
    1. QTextEdit *mainTextEdit=new QTextEdit(this); // the object where the text is displayed
    2. …
    3. connect(mainTextEdit, SIGNAL(cursorPositionChanged( int , int )), this, SLOT(toggleIfStyle(int, int)) );
    4. …
    5. // then in the toggleIfStyle(int,int) method we have:
    7. void class::toggleIfStyle(int a, int b)
    8. {
    9. QString textParagragh(mainTextEdit->text(a)); // the text of the current paragraph
    11. mainTextEdit->insertAt(”<p align=justify>”, a, 0); // QTextEdit reads HTML tags, so we enter the align=justify tag
    12. mainTextEdit->insertAt(”</p>”, a, mainTextEdit->paragraphLength(a)); // close the tag
    14. mainTextEdit->setText(mainTextEdit->text()); // the text has to be re-displayed
    15. }
    To copy to clipboard, switch view to plain text mode 

    Hope sometimes, to someone will help

    Last edited by high_flyer; 12th January 2006 at 17:23.


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.