如何用Qt创建项目符号或编号列表?
问题描述:
如何用Qt通过单击按钮在QTextEdit中创建项目符号或编号列表?此外,有必要列出通过单击相同按钮选择的列表。当光标在列表中并且您单击该按钮时,列表项目将变为非列表项目,而是一个简单的段落。用两个字我想为我的文本编辑器2按钮创建,其工作方式与(引导和编号按钮是MS Word)相同。如何用Qt创建项目符号或编号列表?
答
我已经使用这个代码:
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);
}
答
的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>");
}
或者你可以操纵使用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可能会有所帮助
希望这会有所帮助,至于
第二个变量是我想要的,但是是的incompleate。困难的部分是编写已编写的文本/编号。并且bulleted /编号的文本使unbulleted/unnumbered。实际上,它应该使用相同的可检查按钮或在菜单中进行操作。 – Narek 2010-09-06 17:23:47