Qt 无法在TextEdit上获得突出显示
TL;DR:Qt 无法在TextEdit上获得突出显示,qt,qml,syntax-highlighting,Qt,Qml,Syntax Highlighting,TL;DR:text编辑仅当我单击高亮显示的文本时才绘制它。无济于事 我有一个带有字符串属性的QAbstractListModel模型的ListView。 正在对这些字符串属性进行拼写检查,QSyntaxHighlighter用于显示拼写错误。我在组件中创建QSyntaxHighlighter子体。我仔细检查了高亮显示get的执行是否有正确的拼写错误,并且高亮显示的setFormat()的执行位置是否正确。问题是,只有当我点击textdedit本身时,它才会以红色绘制文本(无效) TextEdi
text编辑
仅当我单击高亮显示的文本时才绘制它。无济于事
我有一个带有字符串属性的QAbstractListModel
模型的ListView
。
正在对这些字符串属性进行拼写检查,QSyntaxHighlighter
用于显示拼写错误。我在组件中创建QSyntaxHighlighter
子体。我仔细检查了高亮显示get的执行是否有正确的拼写错误,并且高亮显示的setFormat()
的执行位置是否正确。问题是,只有当我点击textdedit
本身时,它才会以红色绘制文本(无效)
TextEdit
位于Flickable
中(用于跟踪光标),而Flickable
位于矩形中(具有良好的背景和边框)。绑定到某些信号并调用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)
}
}
}
矩形{
id:descriptionRect
身高:30
border.width:descriptionTestInput.activeFocus?1:0
剪辑:对
轻快的{
id:descriptionFlick
contentWidth:DescriptionTestInput.paintedWidth
contentHeight:DescriptionTestInput.paintedHeight
锚定。填充:父级
交互:错误
flickableDirection:Flickable.HorizontalFlick
身高:30
剪辑:对
焦点:错误
函数可修改(r){
如果(contentX>=r.x)
contentX=r.x;
否则,如果(contentX+width问题可能是由引起的,在Qt 5.5中修复
考虑到bug的级别很低,我认为解决它实际上是不现实的
在完成语法突出显示后,可以通过向TextEdit追加一个空字符串来解决此问题
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)
}
其中,updateExtendit(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)));
}
void ItemsModel::initHighlighter(int索引,qquicktextcument*document){
//如果lamda插槽可用(Qt5和C++11),则可以避免使用信号映射器
QSignalMapper*signalMapper=新的QSignalMapper(此);
如果(0 textDocument(),项);
QObject::connect(项目,信号(spellCheckResultsReady()),
高亮度灯,插槽(再高亮度());
//TODO:不要为Qt 5.5+连接此插槽以避免性能开销
QObject::connect(项目,信号(spellCheckResultsReady()),
信号映射器,插槽(map());
信号映射器->设置映射(项目、索引);
}
连接(信号映射器,信号(映射(int)),
这是一个信号(updateExtendit(int));
}
此处提供完整代码:刚刚在5.11.2上遇到此问题,并找到了以下修复程序,该程序允许更新单个块,而无需突出显示/取消选择整个文本区域
rehighlightBlock(newBlock);
Q_EMIT document()->documentLayout()->updateBlock(newBlock);
我构建并执行了您的源代码,但程序的工作方式与您预期的一样。在我单击“检查拼写”按钮后,单词会立即突出显示。我在OSXe上使用了Qt 5.5.1。一切正常。请尝试使用确保disclean&&qmake&&make重新生成项目,以确保它不适用于Qt 5.4。有无清洁和qmaking。它可能是工作的king on 5.5.1。但我需要在我的产品中保持Qt 5.2之前的兼容性,这需要一些黑客和5.4Hi Simon。我最终得到了相同的解决方法。只是不使用append(“”
),而是使用方法deselect()
。实际上,我使用的是Qt 5.5.1,在我复杂的应用程序中,这仍然不起作用(需要解决办法)虽然演示在5.5.1中有效。无论如何,我仍然没有用我的方法来回答这个问题,所以我会接受你的答案。好的,很酷。所以我们有一个很好的解决方案,可以让其他人找到并改进这个问题。你的演示项目为每个想在QML中开始语法突出显示的人提供了很多价值,所以它将是nice使其保持活力。您好。非常感谢此更新。您是否尝试过修补示例中的小代码文件?(以确认其工作正常)