How to create a bulleted or numbered list with Qt?

advertisements

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


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