2015-11-29 104 views
8

TL; DR:TextEdit只有當我點擊它時,纔會突出顯示文字。沒有什麼幫助無法突出顯示TextEdit的工作

我有一個ListViewQAbstractListModel模型與字符串屬性。 這些字符串屬性正在拼寫檢查,QSyntaxHighlighter用於顯示拼寫錯誤。我在TextEditComponent.onCompleted中創建QSyntaxHighlighter後裔。我仔細檢查突出顯示get的執行是否有正確的拼寫錯誤,並且熒光筆的setFormat()以正確的位置執行。問題在於,只有當我點擊TextEdit本身時,纔會以紅色繪製文本(無效)。

TextEdit住在Flickable(跟蹤光標)和Flickable住在Rectangle(有很好的背景和邊框)。綁定到某些信號並調用TextEdit的update()不會有幫助。

拼寫檢查完成後,我發出創建的SyntaxHighlighter的rehighlight()信號。

Rectangle { 
    id: descriptionRect 
    height: 30 
    border.width: descriptionTextInput.activeFocus ? 1 : 0 
    clip: true 

    Flickable { 
     id: descriptionFlick 
     contentWidth: descriptionTextInput.paintedWidth 
     contentHeight: descriptionTextInput.paintedHeight 
     anchors.fill: parent 
     interactive: false 
     flickableDirection: Flickable.HorizontalFlick 
     height: 30 
     clip: true 
     focus: false 

     function ensureVisible(r) { 
      if (contentX >= r.x) 
       contentX = r.x; 
      else if (contentX+width <= r.x+r.width) 
       contentX = r.x+r.width-width; 
     } 

     TextEdit { 
      id: descriptionTextInput 
      width: descriptionFlick.width 
      height: descriptionFlick.height 
      text: description 
      onTextChanged: model.editdescription = text 

      Component.onCompleted: { 
       globalModel.initDescriptionHighlighting(index, descriptionTextInput.textDocument) 
      } 

      onCursorRectangleChanged: descriptionFlick.ensureVisible(cursorRectangle) 
     } 
    } 
} 

這裏是如何它不直到你工作示範工程的小樣本點擊文本https://bitbucket.org/ribtoks/rehighlighdemo/src

任何想法我怎麼能解決這個問題?

+1

我建立並執行你的來源,但該程序可以作爲你的預期。單擊拼寫檢查按鈕後,單詞突出顯示。我在OSX上使用Qt 5.5.1 – DenimPowell

+0

一切正常,你可以嘗試使用make disclean來重建項目&& qmake && make' – swex

+0

它肯定不適用於Qt 5.4。無論是否進行清潔和調整。它可能在5.5.1上工作。但是我需要保持我的產品兼容性達到Qt 5.2,這需要一些破解以及5.4 – Ribtoks

回答

0

該問題可能是由於在Qt 5.5中修復了QTBUG-44765引起的。

鑑於該錯誤的級別較低,我不認爲它實際上是解決它的。

您可以解決這通過附加一個空字符串的文本編輯,當你用語法高亮

TextEdit { 
    id: captionTextEdit 
    width: wrapperFlick.width 
    height: wrapperFlick.height 
    text: display 
    readOnly: true 

    Component.onCompleted: { 
     itemsModel.initHighlighter(index, captionTextEdit.textDocument) 
    } 

    Connections { 
     target: itemsModel 
     onUpdateTextEdit: { 
      console.log("Update element at index: " + indexToUpdate) 

      if (indexToUpdate == index) 
      { 
       console.log("Update me!") 
       captionTextEdit.append("") 
      } 
     } 
    } 

    onCursorRectangleChanged: wrapperFlick.ensureVisible(cursorRectangle) 
} 

其中updateTextEdit(indexToUpdate)是一個新的信號,你itemsModel已經發出實現。

itemsmodel.h

signals: 
    void updateTextEdit(int indexToUpdate); 

itemsmodel.cpp

void ItemsModel::initHighlighter(int index, QQuickTextDocument *document) { 
    // Signal mapper could be avoided if lamda slot are available (Qt5 and C++11) 
    QSignalMapper* signalMapper = new QSignalMapper(this); 

    if (0 <= index && index < m_ItemsList.length()) { 
     SingleItem *item = m_ItemsList.at(index); 
     SpellCheckHighlighter *highlighter = new SpellCheckHighlighter(document->textDocument(), item); 
     QObject::connect(item, SIGNAL(spellCheckResultsReady()), 
         highlighter, SLOT(rehighlight())); 

     // TODO: Don't connect this slot for Qt 5.5+ to avoid performance overhead 
     QObject::connect(item, SIGNAL(spellCheckResultsReady()), 
         signalMapper, SLOT(map())); 
     signalMapper->setMapping(item, index); 
    } 

    connect(signalMapper, SIGNAL(mapped(int)), 
      this, SIGNAL(updateTextEdit(int))); 
} 

的完整代碼可以在這裏:https://bitbucket.org/swarta/rehighlighdemo/branch/workaround#diff

+0

嗨Simon。我最終採取了相同的解決方法。只是不使用'append(「」)',而是使用方法'deselect()'來代替。實際上,我使用的是Qt 5.5.1,在我複雜的應用程序中,這仍然不起作用(需要解決方法),而對於演示,它在5.5.1中起作用。無論如何,我仍然沒有打算用我的方法回答這個問題,所以我會接受你的答案。 – Ribtoks

+0

好的很酷。所以我們有一個很好的解決方案,讓其他人找到並提升這個。您的演示項目爲希望開始使用QML中的語法高亮顯示的每個人提供了很多價值,因此保持活躍狀態​​將很好。 –