16

Qt Creatorは、いくつかのテキストの周りに細いフレームを描画して、適切な書式設定操作を見つけます (ここで例として、addRow の周りのフレームを参照します。黄色の領域は、テキストを移動する前に、見つかった場所を囲んでいたテキスト検索操作の結果です。カーソル..)

ここに画像の説明を入力してください

QTextEdit でその効果を得る方法を見つけることができませんでした。Qt Creator のソースから読み込もうとしましたが、大きすぎて情報に基づいて検索できません...

編集

私は今、カスタム QTextCharAttribute を調べ始めました。

class framedTextAttr : public QTextObjectInterface {...}

編集

それは機能しています:以下の私の答えによると。

4

5 に答える 5

12

QTextEdit::setExtraSelections()ドキュメントの任意のセクションを強調表示するために使用します。QTextEdit::ExtraSelectionクラスは、各ハイライトを定義するために使用されるパブリック メンバー変数を持つ単純なクラスです。ハイライトを作成するには、

  1. から入手QTextCursorするQTextEdit
  2. カーソルを操作して、正しいテキストが選択範囲として含まれるようにする
  3. カーソルを保存QTextCharFormatし、ExtraSelectionsオブジェクトに必要です(値を割り当てるだけで、ポインタやは必要ありません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();
}
于 2013-10-16T13:27:47.227 に答える
7

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...

edit

Actually, it's feasible. 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.

于 2013-10-17T11:32:51.473 に答える
0

Qt Creator のような大規模なプロジェクトのコードの一部を使用するのは簡単なことではなく、独自のコードをゼロから作成するよりも多くの時間と労力がかかる可能性があります。

あなたの問題のために、Qtには、QSyntaxHighlighter構文パターンを継承して正規表現とルール(色、フォントの太さなど)として設定できるクールなクラスがあります。

したがって、あなたの場合、ユーザーが検索ボックスに入力するとき、または単語を選択するときに、構文パターンを動的に設定する必要があり、構文規則では背景色になります。

于 2013-10-08T09:40:33.140 に答える
0

Qt Code の syntaxhighlighter の例を確認できます。役に立つと思います。

于 2014-03-29T10:03:05.233 に答える