PDA

View Full Version : (SOLVED) QTextEdit subscripted text



giowck
11th February 2011, 10:51
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:



ui->htmlTextEdit->currentCharFormat().setVerticalAlignment(QTextChar Format::AlignSubScript);

Nothing happens... Anyone?

BTW. i also tried with this:


QString selected_text = ui->htmlTextEdit->textCursor().selectedText();
ui->htmlTextEdit->textCursor().removeSelectedText();
ui->htmlTextEdit->insertHtml(QString("<sub>%1</sub>").arg(selected_text));

The last one works, except that i can use it only when text is selected (QTextEdit has selection).

Thanks! ;)

wysota
11th February 2011, 13:37
This works for me:

#include <QtGui>

int main(int argc, char **argv){
QApplication app(argc, argv);
QTextEdit edit;
QTextDocument *doc = edit.document();
QTextCursor cursor(doc);
cursor.insertText("abc");
QTextCharFormat fmt;
fmt.setVerticalAlignment(QTextCharFormat::AlignSub Script);
cursor.insertText("xyz", fmt);
edit.show();
return app.exec();
};

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.

giowck
11th February 2011, 15:20
Oh thank you!

I thought it were a reference xD

Now it works!