[英]QML prevent ListView delegate to update all the time
I'm relatively new to QML/QtQuick and still learning.我对 QML/QtQuick 比较陌生,并且还在学习。 I have a little performane issue with a very small private project.我对一个非常小的私人项目有一点性能问题。 I just tryed to implement a filter function to my ListView, because >15.000 objects are a lot to search manually.我只是尝试在我的 ListView 中实现一个过滤器 function,因为 > 15.000 个对象需要手动搜索很多。 I just want to update the ListView when I finished the editing of my search field or pressing "return".当我完成搜索字段的编辑或按“返回”时,我只想更新 ListView。 But instead it's refreshing every time I insert or delete a character from this textfield which needs sometimes a few seconds.但是,每次我从这个文本字段中插入或删除一个字符时,它都会刷新,这有时需要几秒钟。
Anyone have an idea how to prevent the list to be refreshed permanently or reducing theese performance issues?任何人都知道如何防止列表永久刷新或减少这些性能问题?
Thanks a lot非常感谢
import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.XmlListModel 2.12
import Anime_initialiser 1.0
import "."
Page {
TextField{
id: searchField
width: parent.width
z: 1
/*onEditingFinished: {
XL.animeListModel.reload()
}*/
}
ListView {
z: 0
ScrollBar.vertical: ScrollBar { active: true }
id: listView
width: parent.width
height: parent.height
model: XL.animeListModel
y: searchField.height
Anime_initialiser {
id: initialiser
onShowAnimeDetails: {
xmlDataString = xmlString
swipeView.currentIndex = swipeView.currentIndex+1
}
}
delegate: ItemDelegate {
visible: {
if (searchField.length > 0)
return (main_title.toLowerCase().match(searchField.text.toLowerCase()) || de_title.toLowerCase().match(searchField.text.toLowerCase())) ? true : false
else
return true
}
height: visible ? Button.height : 0
width: parent ? parent.width : 0
Button {
anchors.fill: parent
onClicked: {
anime_id = aid
initialiser.buttonClicked(anime_id)
}
Text {
width: parent.width
height: parent.height
font.pointSize: 100
minimumPointSize: 12
fontSizeMode: Text.Fit
text: aid + ": " + main_title + (de_title ? "\nDE: " + de_title : "")
}
}
}
}
}
Rather than toggling the visible flag of all of your delegates, you should use a QSortFilterProxyModel .而不是切换所有代表的可见标志,您应该使用QSortFilterProxyModel 。 The idea is that the proxy model would use your XL.animeListModel
as a source model, and then you can give the proxy a regular expression telling it which ones to filter out.这个想法是代理 model 将使用您的XL.animeListModel
作为源 model,然后您可以给代理一个正则表达式,告诉它要过滤掉哪些。 Depending on how you want it to filter, you could just call setFilterRole()
to tell it which property to compare against your regex, or you could do a custom filter by overriding the filterAcceptsRow()
function.根据您希望它如何过滤,您可以调用setFilterRole()
来告诉它要与您的正则表达式比较哪个属性,或者您可以通过覆盖filterAcceptsRow()
function 来执行自定义过滤器。
EDIT :编辑:
If you don't want to use a proxy, you can still prevent the constant updates by not binding on the visible
property directly to the search field.如果您不想使用代理,您仍然可以通过不将visible
属性直接绑定到搜索字段来防止不断更新。 You were on the right track with your onEditingFinished
code.您的onEditingFinished
代码走在了正确的轨道上。 You could create a separate text string that just holds the completed search text.您可以创建一个单独的文本字符串来保存完整的搜索文本。
property string searchText: ""
Then update that string when you are done typing your search text.然后在您输入完搜索文本后更新该字符串。
onEditingFinished: {
searchText = searchField.text.toLowerCase();
}
And finally, bind your visible
property to this new string.最后,将您的visible
属性绑定到这个新字符串。
visible: {
if (searchText.length > 0)
return (main_title.toLowerCase().match(searchText) || de_title.toLowerCase().match(searchText)) ? true : false
else
return true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.