QTextEdit中的文本格式很好,就像Qt Creator一样

Cap*_*liC 15 qt

Qt Creator发现一个很好的格式化操作,在一些文本周围绘制一个薄框(这里的一个例子,我指的是addRow周围的框架,黄色区域是一个文本查找操作的结果,该操作也已经找到了找到的位置,然后我移动了光标..)

在此输入图像描述

我一直无法找到如何在QTextEdit中获得该效果.我试图从Qt Creator来源阅读,但它们对于不知情的搜索来说太大了......

编辑

我刚才开始研究自定义的QTextCharAttribute

class framedTextAttr : public QTextObjectInterface {...}
Run Code Online (Sandbox Code Playgroud)

编辑

这是有效的:根据我的答案如下.

hyd*_*yde 10

使用QTextEdit::setExtraSelections()以突出显示文档的任意部分.QTextEdit :: ExtraSelection是具有公共成员变量的简单类,用于定义每个突出显示.要创建一个亮点,

  1. 获得QTextCursorQTextEdit
  2. 操纵光标,使其包含正确的文本作为选择
  3. QTextCharFormatExtraSelections对象中存储光标和所需(仅指定值,不需要指针或new)
  4. ExtraSelections对象(作为值)存储在QList
  5. 重复上面所有你想要的亮点
  6. 调用setExtraSelections()方法

一些示例代码:

#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();
}
Run Code Online (Sandbox Code Playgroud)


Cap*_*liC 7

使用QTextObjectInterface,我得到文本对象周围的框架:

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);
}
Run Code Online (Sandbox Code Playgroud)

但是文本变成了单个对象,不再可编辑

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);
}
Run Code Online (Sandbox Code Playgroud)

结果(这里是图片):

在此输入图像描述

似乎很难概括.我不满意......

编辑

实际上,这是可行的.我已经用插图方法解决了一些问题,并且以可重复使用的方式折叠/展开文本似乎也是可行的.

在此输入图像描述

我把我的测试项目放在GitHub上.