Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nice text formatting in QTextEdit, like Qt Creator does

Tags:

qt

Qt Creator spots a nice formatting operation, drawing a thin frame around some text (here an example, I refer to the frames around addRow, the yellow areas were the result a text find operation that also had framed the locations found, before I moved the cursor..)

Enter image description here

I've been unable to find how to get that effect in QTextEdit. I tried to read from Qt Creator sources, but they are too large for uninformed search...

edit

I started just now to look into custom QTextCharAttribute, via

class framedTextAttr : public QTextObjectInterface {...}

edit

It's working: as per my answer below.

like image 543
CapelliC Avatar asked Oct 07 '13 19:10

CapelliC


People also ask

How do I read text from QTextEdit?

QTextEdit does not have any text() method, if you want to get the text you must use toPlainText() , if you want to clean the text it is better to use clear() since it makes it more readable.

How do I display a text file in Qt?

open(QFile::ReadOnly | QFile::Text); QTextStream ReadFile(&file); while (! ReadFile. atEnd()) { QString line = ReadFile. readLine(); ui->Output->append(line); } file.

What is text Edit in Qt Designer?

The QTextEdit class provides a widget that is used to edit and display both plain and rich text.

How do you use text browser QT?

If you want to provide your users with an editable rich text editor, use QTextEdit. If you want a text browser without hypertext navigation use QTextEdit, and use QTextEdit::setReadOnly() to disable editing. If you just need to display a small piece of rich text use QLabel.


1 Answers

Use QTextEdit::setExtraSelections() to highlight any arbitrary sections of the document. QTextEdit::ExtraSelection class is simple class with public member variables, used to define each highlight. To create a highlight,

  1. get QTextCursor from the QTextEdit
  2. manipulate the cursor so that it contains the right text as selection
  3. store cursor and desired QTextCharFormat in an ExtraSelections object (just assign values, no need for pointers or new)
  4. store the ExtraSelections object (as value) in QList
  5. repeat above for all highlights you want
  6. call the setExtraSelections() method

Some example code:

#include <QtGui>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // create QTextEdit, with lot of text in it
    QTextEdit w("Alice and Bob (and Eve). ");
    for(int i=0;i<10;++i) w.setText(w.toPlainText() + w.toPlainText());

    // prepare variables for highlights
    QTextCharFormat fmt;
    fmt.setUnderlineStyle(QTextCharFormat::SingleUnderline);
    fmt.setUnderlineColor(QColor(200,200,200));
    fmt.setBackground(QBrush(QColor(230,230,230)));

    // highlight all text in parenthesis
    QTextCursor cursor = w.textCursor();
    while( !(cursor = w.document()->find(QRegExp("\\([^)]*\\)"), cursor)).isNull()) {
        QTextEdit::ExtraSelection sel = { cursor, fmt };
        selections.append(sel);
    }

    // set, show, go!
    w.setExtraSelections(selections);
    w.show();
    return a.exec();
}
like image 123
hyde Avatar answered Sep 19 '22 18:09

hyde