【发布时间】:2017-02-07 21:08:38
【问题描述】:
我有以下带有列表视图的滚动视图:
ScrollView{
anchors.fill: parent
ListView{
id: lvCommitsBranch
model: git.getCommitsBranch();
clip: true
delegate: Rectangle {
height: 100
width: parent.width
Text {
anchors.left: parent.left
font.bold: true
text:model.author
id:txtName
}
Text{
anchors.left: parent.left
anchors.top:txtName.bottom
font.pixelSize: 10
text:model.email
id: txtEmail
}
Text {
anchors.left: parent.left
anchors.top:txtEmail.bottom
text: model.message + ' ' + model.hash
id: txtMsg
}
MouseArea{
anchors.fill: parent
onClicked: {
lvCommitsBranch.currentIndex = index;
console.log('Msg: ' + model.message);
console.log('Hash: ' + model.hash);
}
acceptedButtons: Qt.LeftButton | Qt.RightButton
}
}
}
}
问题是,当我滚动时,一些项目会消失(每次随机滚动,有时我必须快速滚动,但并非总是如此)。
当我点击没有消失的项目时,我会在所有模型的属性上得到undefined。当 Mousearea 的 onclick 被触发时,它会打印以下内容:
qml: 消息:未定义
qml: 哈希:未定义
我从我的git 自定义组件返回的方法 (QAbstractListModel) 中获取模型信息。
这是我的 QAbstractListModel:
标题:
class CommitsBranch : public QAbstractListModel
{
Q_OBJECT
public:
enum Roles {
AuthorRole,
EMailRole,
MsgRole,
DateRole,
HashRole
};
explicit CommitsBranch(QObject *parent = 0);
CommitsBranch(Repository *repo);
public:
virtual int rowCount(const QModelIndex &parent) const override;
virtual QVariant data(const QModelIndex &index, int role) const override;
protected:
// return the roles mapping to be used by QML
virtual QHash<int, QByteArray> roleNames() const override;
private:
QList<Commit> m_data;
QHash<int, QByteArray> m_roleNames;
};
Cpp:
CommitsBranch::CommitsBranch(QObject *parent)
: QAbstractListModel(parent)
{
}
CommitsBranch::CommitsBranch(Repository *repo)
{
m_roleNames[AuthorRole] = "author";
m_roleNames[EMailRole] = "email";
m_roleNames[MsgRole] = "message";
m_roleNames[DateRole] = "date";
m_roleNames[HashRole] = "hash";
/*
here we append the m_data (QList) Items using libgit2 methods
*/
}
int CommitsBranch::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_data.count();
}
QVariant CommitsBranch::data(const QModelIndex &index, int role) const
{
// this function returns the required data
}
QHash<int, QByteArray> CommitsBranch::roleNames() const
{
return m_roleNames;
}
而git只是一个继承自QObject的类,它有如下方法:
Q_INVOKABLE QObject* getCommitsBranch();
QObject *Git::getCommitsBranch()
{
CommitsBranch* files = new CommitsBranch(repo.data());
return files;
}
没有滚动视图我会得到相同的行为。
编辑: 如果我使用一个包含大量提交的存储库(更多行到列表视图),即使增加 cacheBuffer 也无济于事,如果我滚动得快一点,所有项目都会消失。
【问题讨论】:
-
问题可能源于滚动时视图自动创建和销毁委托。一个肮脏的快速解决方案是增加视图的
cacheBuffer- 这是要预加载的像素数量。 QML 有时被称为losing track of its sheep,可以这么说,检查滚动是否不会意外删除实际的模型项。 -
我尝试将
cacheBuffer增加到100,但问题仍然存在。我再次将其更改为 1000,现在项目不会消失,但有时我仍然在项目的属性中未定义(还有一些奇怪的视觉错误,如文本移动 o.o) -
尝试在您的委托中将
Item包裹在Rectangle周围。喜欢delegate: Item { Rectangle { ... } } -
@DuKes0mE 结果完全一样,没有任何变化:s
-
嗯,它应该避免你的矩形被删除。
标签: c++ qt qml qtquick2 qtquickcontrols