views:

64

answers:

2

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

+1  A: 

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

serge_gubenko
The second variant is what I want, but is's incompleate. The difficult part is to make already written text buletted/numbered. And bulleted/numbered text make unbulleted/unnumbered. Actually it should be done with the same checkable button or and action in a menu.
Narek
A: 

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