Results 1 to 3 of 3

Thread: (SOLVED) QTextEdit subscripted text

  1. #1
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default (SOLVED) QTextEdit subscripted text

    Hi,
    I'm writing a simple text editor (rich text with html as engine), so i use the QTextEdit class wich is really great... So far bold, italic, underline, alignments etc... work great.

    Now I'm trying to allow subscript and superscript text.
    The problem is that it doesn't work:

    Qt Code:
    1. ui->htmlTextEdit->currentCharFormat().setVerticalAlignment(QTextCharFormat::AlignSubScript);
    To copy to clipboard, switch view to plain text mode 
    Nothing happens... Anyone?

    BTW. i also tried with this:
    Qt Code:
    1. QString selected_text = ui->htmlTextEdit->textCursor().selectedText();
    2. ui->htmlTextEdit->textCursor().removeSelectedText();
    3. ui->htmlTextEdit->insertHtml(QString("<sub>%1</sub>").arg(selected_text));
    To copy to clipboard, switch view to plain text mode 
    The last one works, except that i can use it only when text is selected (QTextEdit has selection).

    Thanks!
    Last edited by giowck; 11th February 2011 at 15:21.

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

    Default Re: QTextEdit subscripted text

    This works for me:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char **argv){
    4. QApplication app(argc, argv);
    5. QTextEdit edit;
    6. QTextDocument *doc = edit.document();
    7. QTextCursor cursor(doc);
    8. cursor.insertText("abc");
    9. fmt.setVerticalAlignment(QTextCharFormat::AlignSubScript);
    10. cursor.insertText("xyz", fmt);
    11. edit.show();
    12. return app.exec();
    13. };
    To copy to clipboard, switch view to plain text mode 

    I think you are modifying a copy of the format. currentTextCharFormat() returns a copy and not a reference so your changes are not applied to the cursor.
    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. The following user says thank you to wysota for this useful post:

    giowck (11th February 2011)

  4. #3
    Join Date
    Aug 2009
    Posts
    44
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextEdit subscripted text

    Oh thank you!

    I thought it were a reference xD

    Now it works!

Similar Threads

  1. how to get the text from QTextEdit
    By qt_user in forum Qt Programming
    Replies: 5
    Last Post: 22nd August 2011, 00:22
  2. QTextEdit::text()
    By hazardpeter in forum Newbie
    Replies: 2
    Last Post: 5th August 2009, 21:13
  3. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 16:45
  4. QTextEdit -> add Text
    By ape in forum Newbie
    Replies: 16
    Last Post: 19th December 2007, 14:59
  5. QTextEdit and mark Text
    By J-jayz-Z in forum Qt Programming
    Replies: 12
    Last Post: 28th September 2007, 00:14

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.