我正在QT QTextEdit中实现一个焦点模式,其中我突出显示光标所在的单行。到目前为止,我可以启用焦点模式,但是当我禁用焦点模式时,我希望状态恢复到原来的状态。
调用connect和disconnect的函数是:
void MainWindow::onFocus_Mode_triggered()
{
QTextEdit *texed = qobject_cast<QTextEdit*>(ui->tabWidget->currentWidget());
if(ui->actionFocus_Mode->isChecked()){
connect(texed, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine()));
}
else {
disconnect(texed, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); //First disconnect and then call method to clear ExtraSelections
BacktoNormal(); //Help needed in implementing this
}
}
现在,当选中菜单项actionFocus_Mode时,光标当前所在的行将通过下面给出的函数以黄色突出显示。
void MainWindow::highlightCurrentLine() {
QTextEdit *texed = qobject_cast<QTextEdit*>(ui->tabWidget->currentWidget());
QList<QTextEdit::ExtraSelection> extraSelections;
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = texed->textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
texed->setExtraSelections(extraSelections);
}
所以我可以用黄色突出显示它,但是如果(!ui-> actionFocus_Mode-&gt; isChecked()),即如果未选中菜单项(焦点模式),我希望恢复到正常模式。我将如何实现BacktoNormal()函数。
我现在想的是我应该将lineColor设置为透明或者某些东西以使其恢复正常(如果可能的话)。 我无法找到与此相关的任何内容。任何帮助都会有用,因为我完全陷入了困境。
答案 0 :(得分:1)
在BackNormal
尝试将任何内容都设置为额外选择。
QTextEdit *texed = qobject_cast<QTextEdit*>(ui->textEdit);
QList<QTextEdit::ExtraSelection> extraSelections;
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(Qt::yellow).lighter(160);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = texed->textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
extraSelections.clear();//nothing
texed->setExtraSelections(extraSelections);
我什么时候在计算机上尝试此操作(使用其他代码),此选项已成功删除。
较小的版本:
QTextEdit *texed = qobject_cast<QTextEdit*>(ui->textEdit);
QList<QTextEdit::ExtraSelection> extraSelections;//empty list
texed->setExtraSelections(extraSelections);