如何使用 Qt 创建项目符号或编号列表?

发布于 2024-09-17 10:50:23 字数 156 浏览 11 评论 0 原文

如何通过单击按钮在 QTextEdit 中使用 Qt 创建项目符号或编号列表?此外,还需要列出通过单击同一按钮选择的段落。当光标位于列表中并单击按钮时,列表项变为非列表项,而是一个简单的段落。简而言之,我想为我的文本编辑器创建 2 个按钮,其工作方式与(项目符号和编号按钮是 MS Word)相同。

How to create a bulleted or numbered list in QTextEdit with Qt by clicking a button? Also it is necessary that make a list the paragraphes which are selected by clicking the same button. And when the cursor is in the list and you click the button, the the list item becomes not-list item, but a simple paragraph. In two words I want to create for my text editer 2 buttons, that work in the same way as (buletting and numbering button is MS Word).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此岸叶落 2024-09-24 10:50:50

我使用了这段代码:

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

I have used this code:

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

from this source.

Only I have changed

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

to the following code:

 } else {
     // ####
QTextBlockFormat bfmt;
bfmt.setObjectIndex(0);
cursor.mergeBlockFormat(bfmt);
setTextCursor(cursor);
}
你不是我要的菜∠ 2024-09-24 10:50:46

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

或者您可以使用 QTextDocumentQTextCursor 成员。下面是一个示例:

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

还有此链接: 富文本处理 可能会有所帮助

,希望如此帮助,问候

QTextEdit should support html text formatting so button click handler below should insert 2 lists into the text edit control:

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

alternatively you can manipulate textedit content using QTextDocument and QTextCursor members. Below is an example:

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

also this link: Rich Text Processing might be helpful

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文