我有一個文本編輯器程式,我想提供一個可檢查的 QToolButton 以將當前選定的文本切換為洗掉線格式(可檢查性用于在游標位置顯示 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() 在 textEditDisplay 結束時顯式更改洗掉線狀態,但沒有成功。
uj5u.com熱心網友回復:
您需要設定文本編輯的默認字符格式。
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/534442.html
