代码之家  ›  专栏  ›  技术社区  ›  Narek

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

  •  0
  • Narek  · 技术社区  · 15 年前

    如何通过单击按钮在QTextEdit和Qt中创建带项目符号或编号的列表?此外,有必要列出通过单击同一按钮选择的段落。当光标在列表中并单击按钮时,列表项就不再是列表项,而是一个简单的段落。总之,我想为我的文本编辑器创建两个按钮,它们的工作方式与(buleting和numbering按钮是msword)相同。

    2 回复  |  直到 15 年前
        1
  •  5
  •   serge_gubenko    15 年前

    QTextEdit应支持html文本格式,因此下面的按钮单击处理程序应在文本编辑控件中插入两个列表:

    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 成员。下面是一个例子:

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

    还有这个链接: Rich Text Processing 可能会有帮助

    希望这有帮助,问候

        2
  •  2
  •   Narek    14 年前

    我使用了以下代码:

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

    source .

    只有我变了

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

     } else {
         // ####
    QTextBlockFormat bfmt;
    bfmt.setObjectIndex(0);
    cursor.mergeBlockFormat(bfmt);
    setTextCursor(cursor);
    }