QToolButton不切换QTextCharFormat::setFontStrikeOut()为未来的书面文本,而没



我有一个文本编辑器程序,我想提供一个可检查的QToolButton来将当前选择的文本切换为strikeout格式(可检查性用于在光标位置显示isStrikeout)。当选择任何文本并按下按钮时,它工作得很好:文本从非三棱变为三棱,反之亦然。但是,在没有选择文本的情况下单击按钮时,将来写入的文本不会以删除格式输出。

toolButton的划线切换信号来源:

void MainWindow::on_toolButtonStrikethrough_toggled(bool checked)
{
QTextCursor cursor = ui->textEditDisplay->textCursor();
QTextCharFormat format = ui->textEditDisplay->currentCharFormat();
format.setFontStrikeOut(!format.fontStrikeOut());
cursor.setCharFormat(format);
}

我已经尝试使用cursor.atEnd()和cursor.atBlockEnd()来显式地改变剔除状态,而在textditdisplay结束时没有成功。

您需要设置文本编辑的默认字符格式。

void MainWindow::on_toolButtonStrikethrough_toggled(bool checked)
{
QTextCursor cursor = ui->textEditDisplay->textCursor();
QTextCharFormat format = ui->textEditDisplay->currentCharFormat();
format.setFontStrikeOut(!format.fontStrikeOut());
cursor.setCharFormat(format);
ui->textEditDisplay->setCurrentCharFormat(format);
}

相关内容

最新更新