QTextEdit:Tab 键更改项目符号列表缩进
QTextEdit: Tab key changes bullet list indentation
我正在使用 C++ 和 Qt6。
我有一个接受富文本的 QTextEdit。
当我按 Tab 键时,格式更改为:
我想要的结果是第三颗子弹,但我得到的是第二颗子弹。有什么想法吗?
我已经实现了一个槽,当我的“增加缩进”按钮被调用时它会被调用,它的功能是我想要按 Tab 键实现的:
void SummaryWindow::changeListIndentation(int increment)
{
QTextCursor cursor = ui->textEditor->textCursor();
QTextList *currentList = cursor.currentList();
if(currentList)
{
QTextListFormat listFormat;
QTextListFormat::Style currentStyle = currentList->format().style();
listFormat.setIndent(cursor.currentList()->format().indent() + increment);
if (currentStyle == QTextListFormat::ListDisc || currentStyle == QTextListFormat::ListCircle || currentStyle == QTextListFormat::ListSquare)
{
if (listFormat.indent() == 1){listFormat.setStyle(QTextListFormat::ListDisc);}
if (listFormat.indent() == 2){listFormat.setStyle(QTextListFormat::ListCircle);}
if (listFormat.indent() >= 3){listFormat.setStyle(QTextListFormat::ListSquare);}
}
if (currentStyle == QTextListFormat::ListDecimal || currentStyle == QTextListFormat::ListUpperAlpha || currentStyle == QTextListFormat::ListLowerAlpha)
{
if (listFormat.indent() == 1){listFormat.setStyle(QTextListFormat::ListDecimal);}
if (listFormat.indent() == 2){listFormat.setStyle(QTextListFormat::ListUpperAlpha);}
if (listFormat.indent() >= 3){listFormat.setStyle(QTextListFormat::ListLowerAlpha);}
}
currentList->setFormat(listFormat);
}
else
{
QTextBlockFormat blockFormat = cursor.block().blockFormat();
blockFormat.setIndent( increment == 1 ? blockFormat.indent() + 1 : (blockFormat.indent() == 0 ? 0 : blockFormat.indent() - 1));
cursor.setBlockFormat(blockFormat);
}
}
bool SummaryWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->textEditor && event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Tab)
{
this->changeListIndentation(1);
return true;
}
else if (keyEvent->key() == Qt::Key_Backtab)
{
this->changeListIndentation(-1);
return true;
}
}
return QMainWindow::eventFilter(obj, event);
}
我正在使用 C++ 和 Qt6。 我有一个接受富文本的 QTextEdit。 当我按 Tab 键时,格式更改为:
我想要的结果是第三颗子弹,但我得到的是第二颗子弹。有什么想法吗?
我已经实现了一个槽,当我的“增加缩进”按钮被调用时它会被调用,它的功能是我想要按 Tab 键实现的:
void SummaryWindow::changeListIndentation(int increment)
{
QTextCursor cursor = ui->textEditor->textCursor();
QTextList *currentList = cursor.currentList();
if(currentList)
{
QTextListFormat listFormat;
QTextListFormat::Style currentStyle = currentList->format().style();
listFormat.setIndent(cursor.currentList()->format().indent() + increment);
if (currentStyle == QTextListFormat::ListDisc || currentStyle == QTextListFormat::ListCircle || currentStyle == QTextListFormat::ListSquare)
{
if (listFormat.indent() == 1){listFormat.setStyle(QTextListFormat::ListDisc);}
if (listFormat.indent() == 2){listFormat.setStyle(QTextListFormat::ListCircle);}
if (listFormat.indent() >= 3){listFormat.setStyle(QTextListFormat::ListSquare);}
}
if (currentStyle == QTextListFormat::ListDecimal || currentStyle == QTextListFormat::ListUpperAlpha || currentStyle == QTextListFormat::ListLowerAlpha)
{
if (listFormat.indent() == 1){listFormat.setStyle(QTextListFormat::ListDecimal);}
if (listFormat.indent() == 2){listFormat.setStyle(QTextListFormat::ListUpperAlpha);}
if (listFormat.indent() >= 3){listFormat.setStyle(QTextListFormat::ListLowerAlpha);}
}
currentList->setFormat(listFormat);
}
else
{
QTextBlockFormat blockFormat = cursor.block().blockFormat();
blockFormat.setIndent( increment == 1 ? blockFormat.indent() + 1 : (blockFormat.indent() == 0 ? 0 : blockFormat.indent() - 1));
cursor.setBlockFormat(blockFormat);
}
}
bool SummaryWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->textEditor && event->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Tab)
{
this->changeListIndentation(1);
return true;
}
else if (keyEvent->key() == Qt::Key_Backtab)
{
this->changeListIndentation(-1);
return true;
}
}
return QMainWindow::eventFilter(obj, event);
}