0

ボタンをクリックして Qt を使用して QTextEdit で箇条書きまたは番号付きリストを作成する方法は? また、同じボタンをクリックして選択した段落をリストにする必要があります。カーソルがリスト内にあるときにボタンをクリックすると、リスト項目はリスト項目ではなく単純な段落になります。2 つの言葉で、テキスト エディター用に 2 つのボタンを作成したいと考えています。これは、(箇条書きと番号付けのボタンは MS Word です) と同じように機能します。

4

2 に答える 2

5

QTextEdit は HTML テキストの書式設定をサポートする必要があるため、以下のボタン クリック ハンドラはテキスト編集コントロールに 2 つのリストを挿入する必要があります。

void MainWindow::on_pushButton_clicked()
{
    // will insert a bulleted list
    ui->textEdit->insertHtml("<ul><li>text 1</li><li>text 2</li><li>text 3</li></ul> <br />");
    // will insert a numbered list
    ui->textEdit->insertHtml("<ol><li>text 1</li><li>text 2</li><li>text 3</li></ol>");
}

または、 QTextDocumentおよびQTextCursorメンバーを使用して textedit コンテンツを操作できます。以下に例を示します。

void MainWindow::on_pushButton_2_clicked()
{
    QTextDocument* document = ui->textEdit->document();
    QTextCursor* cursor = new QTextCursor(document);

    QTextListFormat listFormat;
    listFormat.setStyle(QTextListFormat::ListDecimal);
    cursor->insertList(listFormat);

    cursor->insertText("one");
    cursor->insertText("\ntwo");
    cursor->insertText("\nthree");
}

また、このリンク:リッチテキスト処理が役立つ場合があります

これが役に立てば幸いです、よろしく

于 2010-09-05T18:39:36.527 に答える
2

私はこのコードを使用しました:

 void TextEdit::textStyle(int styleIndex)
 {
     QTextCursor cursor = textEdit->textCursor();

     if (styleIndex != 0) {
         QTextListFormat::Style style = QTextListFormat::ListDisc;

         switch (styleIndex) {
             default:
             case 1:
                 style = QTextListFormat::ListDisc;
                 break;
             case 2:
                 style = QTextListFormat::ListCircle;
                 break;
             case 3:
                 style = QTextListFormat::ListSquare;
                 break;
             case 4:
                 style = QTextListFormat::ListDecimal;
                 break;
             case 5:
                 style = QTextListFormat::ListLowerAlpha;
                 break;
             case 6:
                 style = QTextListFormat::ListUpperAlpha;
                 break;
             case 7:
                 style = QTextListFormat::ListLowerRoman;
                 break;
             case 8:
                 style = QTextListFormat::ListUpperRoman;
                 break;
         }

         cursor.beginEditBlock();

         QTextBlockFormat blockFmt = cursor.blockFormat();

         QTextListFormat listFmt;

         if (cursor.currentList()) {
             listFmt = cursor.currentList()->format();
         } else {
             listFmt.setIndent(blockFmt.indent() + 1);
             blockFmt.setIndent(0);
             cursor.setBlockFormat(blockFmt);
         }

         listFmt.setStyle(style);

         cursor.createList(listFmt);

         cursor.endEditBlock();
     } else {
         // ####
         QTextBlockFormat bfmt;
         bfmt.setObjectIndex(-1);
         cursor.mergeBlockFormat(bfmt);
     }
 }

このソースから。

私だけが変わった

 } else {
     // ####
     QTextBlockFormat bfmt;
     bfmt.setObjectIndex(-1);
     cursor.mergeBlockFormat(bfmt);
 }

次のコードに:

 } else {
     // ####
QTextBlockFormat bfmt;
bfmt.setObjectIndex(0);
cursor.mergeBlockFormat(bfmt);
setTextCursor(cursor);
}
于 2010-10-20T10:35:38.647 に答える