繁体   English   中英

插槽断开时清除ExtraSelections Qt

[英]Clear ExtraSelections Qt on slot disconnect

我正在QT QTextEdit中实现聚焦模式,在该模式中我突出显示存在光标的单行。 到目前为止,我可以启用聚焦模式,但是当我禁用聚焦模式时,我希望状态恢复到原来的状态。

调用连接和断开连接的函数是:

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);
}

因此,我可以将其突出显示为黄色,但可以使用if(!ui-> actionFocus_Mode-> isChecked())突出显示,即,如果未选中菜单项(焦点模式),那么我希望恢复为正常模式。 我将如何实现BacktoNormal()函数。

我现在想的是,我应该将lineColor设置为transparent或某种方式使其恢复正常(如果可能的话)。 我找不到与此相关的任何内容。 任何帮助都将是有用的,因为我现在完全陷入困境。

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM