I have a text editor program, and I wanted to provide a check-able QToolButton to switch currently selected text to strikeout format (checkability is used to show isStrikeout at cursor location). When selecting any text and pressing the button it works fine: the text changes from non strikeout to strikeout and vice versa. However, when clicking the button while having no text selected future written text does not output in strikeout format.
The source of toolButton's strikethrough toggled signal:
void MainWindow::on_toolButtonStrikethrough_toggled(bool checked)
{
QTextCursor cursor = ui->textEditDisplay->textCursor();
QTextCharFormat format = ui->textEditDisplay->currentCharFormat();
format.setFontStrikeOut(!format.fontStrikeOut());
cursor.setCharFormat(format);
}
I've tried to use cursor.atEnd() and cursor.atBlockEnd() to explicitly change the strikeout state while at the end of textEditDisplay to no success.
CodePudding user response:
You need to set the default char format of the text edit.
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);
}