2010-09-05 131 views
0

如何用Qt通過單擊按鈕在QTextEdit中創建項目符號或編號列表?此外,有必要列出通過單擊相同按鈕選擇的列表。當光標在列表中並且您單擊該按鈕時,列表項目將變爲非列表項目,而是一個簡單的段落。用兩個字我想爲我的文本編輯器2按鈕創建,其工作方式與(引導和編號按鈕是MS Word)相同。如何用Qt創建項目符號或編號列表?

回答

1

我已經使用這個代碼:

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

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

也此鏈接:Rich Text Processing可能會有所幫助

希望這會有所幫助,至於

+0

第二個變量是我想要的,但是是的incompleate。困難的部分是編寫已編寫的文本/編號。並且bulleted /編號的文本使unbulleted/unnumbered。實際上,它應該使用相同的可檢查按鈕或在菜單中進行操作。 – Narek 2010-09-06 17:23:47

相關問題