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..)
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.
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.
open(QFile::ReadOnly | QFile::Text); QTextStream ReadFile(&file); while (! ReadFile. atEnd()) { QString line = ReadFile. readLine(); ui->Output->append(line); } file.
The QTextEdit class provides a widget that is used to edit and display both plain and rich text.
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.
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,
QTextCursor
from the QTextEdit
QTextCharFormat
in an ExtraSelections
object (just assign values, no need for pointers or new
)ExtraSelections
object (as value) in QList
setExtraSelections()
methodSome 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With