2013-11-01 62 views
0

我正在使用Qt5編寫一個簡單的文本編輯器程序。我的編輯器組件是QPlainTextEdit的子類,對於我從this Qt demo program盜取了一些代碼的一些基本功能。功能性的兩件,似乎相互干擾是突出編輯器的當前行的代碼(連接到文本編輯的cursorPositionChanged()信號,就像演示所示):Qt 5 QPlainTextEdit突出顯示當前行在「撤消」後失敗

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

這兩種功能都很好 - 至少大部分的時間。似乎有涉及這兩個奇怪的錯誤,當我做到以下幾點:

  1. 選擇了幾行,並按下Tab鍵,它縮進他們都如我所料。
  2. 撤消該操作。

此時,在縮進的線條集合的最後一行上,線條突出顯示不像編輯器那樣一直延伸到整個編輯器 - 它只延伸到行的末尾。如果我將光標移動到該行的末尾,然後點擊「Enter」,則可以解決問題。

我試了幾件事來嘗試診斷這個問題,包括嘗試移動光標和/或錨點,而不是在高亮函數中調用clearSelection(),並嘗試檢查/遍歷通過QTextBlock s編輯的文件,以嘗試找到一些差異,但在這一點上,我感到不知所措。我簡直不能讓這些代碼像我期望的那樣行事。

我發現現在渲染不正確的那一行可以是「固定的」,但是可以將任何字符添加到該行或調整窗口大小。

此外,如果我在縮進函數結束時刪除setTextCursor調用,仍會發生此錯誤。

這兩件事使我相信,這個錯誤已無關,與任何文本光標或文檔中的內容,所以在這一點上,我傾向於認爲它額外選擇的Qt的渲染錯誤。

有沒有人看到我出錯了?

回答