2014-11-04 44 views
0

我在QT QTextEdit中实现了一个焦点模式,其中我突出显示了光标存在的单行。到目前为止,我可以启用对焦模式,但是当我禁用对焦模式时,我希望状态恢复到原来的状态。在插槽断开处清除额外选择Qt

调用连接和断开的功能是:

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

所以我能突出它的黄色,但如果(!用户界面 - > actionFocus_Mode->器isChecked()),即,如果菜单项(对焦模式)的勾选,那么我希望恢复到正常模式。我将如何实现BacktoNormal()函数。

我现在想的是我应该将lineColor设置为透明或使其恢复正常(如果可能的话)。 我找不到与此相关的任何内容。任何帮助都是有用的,因为我完全停留在这一点上。

回答

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

完美。谢谢。该死的!这非常简单,我正在想一个完全不同的方向。 – Bhavyanshu 2014-11-04 06:44:01

+1

@Bhavyanshu不客气。 :)这个地方是为此目的而创建的,有时候新鲜的另一个人可以节省很多时间。 – Chernobyl 2014-11-04 06:47:31