C++ Qt 5 QPlainTextEdit高亮显示当前行在“之后失败”;撤销;

C++ Qt 5 QPlainTextEdit高亮显示当前行在“之后失败”;撤销;,c++,qt,C++,Qt,我正在使用Qt5开发一个简单的文本编辑器程序。我的编辑器组件是QPlainTextEdit的一个子类,对于一些基本功能,我从中窃取了一些代码。似乎相互干扰的两项功能是突出显示编辑器当前行的代码(与文本编辑的cursorPositionChanged()信号相连,如演示所示): QList-es; QTextEdit::外部选择; selection.format.setBackground(currentLineHighlight); selection.format.setProperty(Q

我正在使用Qt5开发一个简单的文本编辑器程序。我的编辑器组件是
QPlainTextEdit
的一个子类,对于一些基本功能,我从中窃取了一些代码。似乎相互干扰的两项功能是突出显示编辑器当前行的代码(与文本编辑的
cursorPositionChanged()
信号相连,如演示所示):

QList-es;
QTextEdit::外部选择;
selection.format.setBackground(currentLineHighlight);
selection.format.setProperty(QTextFormat::FullWidthSelection,true);
selection.cursor=textCursor();
selection.cursor.clearSelection();
附加(选择);
设置外部选择(es);
我写的代码是为了做一个非常常见的“当你点击tab键时,同时选择多行时,缩进所有行”的事情:

QTextCursor curs=textCursor();
如果(!curs.hasSelection())
返回;
//获取要缩进的第一行数和行数。
int spos=curs.anchor(),epos=curs.position();
如果(spos>epos)
{
int hold=spos;
spos=epos;
epos=保持;
}
curs.setPosition(spos,QTextCursor::MoveAnchor);
int sblock=curs.block().blockNumber();
curs.setPosition(epos,QTextCursor::MoveAnchor);
int eblock=curs.block().blockNumber();
//做缩进。
curs.setPosition(spos,QTextCursor::MoveAnchor);
curs.beginEditBlock();

对于(int i=0;i这实际上是Qt中的一个bug,不是OP中的代码问题。请参阅。

这实际上是Qt中的bug,不是OP中的代码问题。请参阅

QList<QTextEdit::ExtraSelection> es;
QTextEdit::ExtraSelection selection;

selection.format.setBackground(currentLineHighlight);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);

selection.cursor = textCursor();
selection.cursor.clearSelection();

es.append(selection);
setExtraSelections(es);
QTextCursor curs = textCursor();

if(!curs.hasSelection())
    return;

// Get the first and count of lines to indent.

int spos = curs.anchor(), epos = curs.position();

if(spos > epos)
{
    int hold = spos;
    spos = epos;
    epos = hold;
}

curs.setPosition(spos, QTextCursor::MoveAnchor);
int sblock = curs.block().blockNumber();

curs.setPosition(epos, QTextCursor::MoveAnchor);
int eblock = curs.block().blockNumber();

// Do the indent.

curs.setPosition(spos, QTextCursor::MoveAnchor);

curs.beginEditBlock();

for(int i = 0; i <= (eblock - sblock); ++i)
{
    curs.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);

    curs.insertText("\t");

    curs.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor);
}

curs.endEditBlock();

// Set our cursor's selection to span all of the involved lines.

curs.setPosition(spos, QTextCursor::MoveAnchor);
curs.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);

while(curs.block().blockNumber() < eblock)
{
    curs.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor);
}

curs.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);

setTextCursor(curs);