Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, February 3, 2016

Nice text formatting in QTextEdit, like Qt Creator does

Nice text formatting in QTextEdit, like Qt Creator does


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 QtCreator sources, but they are too large for uninformed search...

I started just now to look into custom QTextCharAttribute, via

class framedTextAttr : public QTextObjectInterface {...}  

It's working: as per my answer below.

Answer by Mahmoud Hassan for Nice text formatting in QTextEdit, like Qt Creator does


Well, using part of code of a big projects like Qt Creator is not easy at all, and it could take more time and effort than creating your own code from scratch.

For your problem, Qt has a cool class QSyntaxHighlighter that you can inherit and set your syntax patterns as regular expressions and rules (color, font weight, ...)

So for your case, you need to set the syntax patterns dynamically when user types in the find box or when selecting a word, and for the syntax rule it will be background color.

Answer by hyde for Nice text formatting in QTextEdit, like Qt Creator does


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     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();  }  

Answer by CapelliC for Nice text formatting in QTextEdit, like Qt Creator does


With QTextObjectInterface I get the frame around the text object:

QSizeF framedTextAttr::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)  {      Q_ASSERT(format.type() == format.CharFormat);      const QTextCharFormat &tf = *(const QTextCharFormat*)(&format);      QString s = format.property(prop()).toString();      QFont fn = tf.font();      QFontMetrics fm(fn);      return fm.boundingRect(s).size();  }    void framedTextAttr::drawObject(QPainter *painter, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)  {      Q_ASSERT(format.type() == format.CharFormat);      QString s = format.property(prop()).toString();      painter->drawText(rect, s);      painter->drawRoundedRect(rect, 2, 2);  }  

But the text becomes a single object, no more editable

MainWindow::MainWindow(QWidget *parent)      : QMainWindow(parent)  {      setCentralWidget(new QTextEdit);        framedTextAttr *fa = new framedTextAttr;      editor()->document()->documentLayout()->registerHandler(framedTextAttr::type(), fa);        editor()->setPlainText("hic sunt\n leones !");        QTextCharFormat f;      f.setObjectType(fa->type());        QTextCursor c = editor()->document()->find("leones");      f.setProperty(fa->prop(), c.selectedText());        c.insertText(QString(QChar::ObjectReplacementCharacter), f);  }  

And the result (here a picture):

Enter image description here

It seems it's difficult to generalize. I'm not satisfied...

I've worked out some of the problems with the illustrated approach, and it seems to be viable also for folding/unfolding text in reusable fashion.

Enter image description here

I've put my test project on GitHub.

Answer by mavroprovato for Nice text formatting in QTextEdit, like Qt Creator does


Is using QTextEdit a requirement? If not, you can use Scintilla as the text editing component, which has a Qt binding. Scintilla has the Indicator feature which does exactly what you want.

Answer by QtUser for Nice text formatting in QTextEdit, like Qt Creator does


You can check out syntaxhighlighter example for Qt Code.I guess it will be useful.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.