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).
How to create a bulleted or numbered list with Qt?
7.3k views Asked by Narek At
2
There are 2 answers
1
On
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
I have used this code:
from this source.
Only I have changed
to the following code: