Qt 为什么ListView中的repeater需要剪切?
我遇到了这个问题,Qt 为什么ListView中的repeater需要剪切?,qt,qml,qt5,qt5.6,Qt,Qml,Qt5,Qt5.6,我遇到了这个问题,ListView的行并不总是正确排列。看起来这可能是一个bug,但我有一个解决方法,可以通过剪辑来修复它。但这只是一个意外吗 问题与模型中有大量行有关,但我所有的行都是整数高度,并且彼此相同,因此这不应该是问题 main.qml Thing.qml mymodel.h #包括 #包括 #包括 #包括 #包括 类MyModel:公共QabStretchListModel { Q_对象 公众: 整数行数; 整数行高; MyModel() { _行数=1000000; _行高=100
ListView
的行并不总是正确排列。看起来这可能是一个bug,但我有一个解决方法,可以通过剪辑来修复它。但这只是一个意外吗
问题与模型中有大量行有关,但我所有的行都是整数高度,并且彼此相同,因此这不应该是问题
main.qml
Thing.qml
mymodel.h
#包括
#包括
#包括
#包括
#包括
类MyModel:公共QabStretchListModel
{
Q_对象
公众:
整数行数;
整数行高;
MyModel()
{
_行数=1000000;
_行高=100;
}
int rowCount(常量QModelIndex&parent=QModelIndex())常量重写
{
返回行计数;
}
QVariant数据(常量QModelIndex&index,int角色)常量覆盖
{
int ix=index.row();
char-buf[128];
sprintf(buf,“方框%d”,ix);
返回buf;
}
QHash roleNames()常量重写
{
QHash角色;
角色[Qt::UserRole+1]=“名称”;
返回角色;
}
Q_可调用整数行高()
{
返回高度;
}
};
main.cpp
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“mymodel.h”
int main(int argc,char*argv[])
{
QGUI应用程序应用程序(argc、argv);
qqmlaplicationengine;
MyModel模型;
QQmlContext*ctxt=engine.rootContext();
ctxt->setContextProperty(“myModel”和&model);
engine.addImportPath(“:/”);
engine.load(QUrl(QStringLiteral(“qrc:/main.qml”));
返回app.exec();
}
下面是您向下滚动一点时看到的内容。请注意,标题由蓝色框重叠
这是为什么?
如果将中继器
更改为仅1
而不是10
,它就会消失,但我认为这只是降低了可能性
如果对clip:true
行进行了注释,它会工作,但我不知道为什么
以下是项目文件的要点。
qt5.6/windows/mingw
谢谢你提供的任何信息。看起来像。通过快速查看,
ListView
将可见项的索引乘以代理高度,因此可能存在一些大型号计数的精度问题。谢谢。没有看到这个错误报告。是的,看起来是同一个问题。出于某种原因,它似乎受到每个列表项中项目数的影响。我希望他们能解决这个问题,至少对于固定尺寸的物品。
import QtQuick 2.5
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.2
ApplicationWindow
{
visible: true
width: 640
height: 800
property int rowHeaderHeight: 24
// make a list of rows each with the model row height + header height
ListView
{
id: boxview
anchors.fill: parent
// calculate content height (line can be omitted without problem)
contentHeight: (myModel.rowHeight()+rowHeaderHeight)*myModel.rowCount()
model: myModel
delegate: Thing1 { name: model.name; headerHeight: rowHeaderHeight }
Component.onCompleted:
{
// go right to the middle
boxview.positionViewAtIndex(500000, ListView.Beginning);
}
}
}
import QtQuick 2.5
import QtQuick.Controls 1.4
Item
{
// thing is just a box with a header label
width: parent.width
height: myModel.rowHeight() + headerHeight
property string name
property int headerHeight
// label is the header height
Label
{
width: parent.width
height: headerHeight
verticalAlignment: Text.AlignBottom
text: name
x: 8
}
// then the box
Repeater
{
// but wait! there are 10 of them in the same place
// yes, it works for 1, but goes wrong the more there are
// 10 makes it look obviousl
model: 10
delegate: Rectangle
{
//clip: true // uncomment this to fix the problem, but why?
y: headerHeight
width: parent.width;
height: myModel.rowHeight()
color: "blue"
}
}
}
#include <QAbstractListModel>
#include <QQmlApplicationEngine>
#include <QObject>
#include <QString>
#include <QDebug>
class MyModel : public QAbstractListModel
{
Q_OBJECT
public:
int _rowCount;
int _rowHeight;
MyModel()
{
_rowCount = 1000000;
_rowHeight = 100;
}
int rowCount(const QModelIndex& parent = QModelIndex()) const override
{
return _rowCount;
}
QVariant data(const QModelIndex &index, int role) const override
{
int ix = index.row();
char buf[128];
sprintf(buf, "Box %d", ix);
return buf;
}
QHash<int, QByteArray> roleNames() const override
{
QHash<int, QByteArray> roles;
roles[Qt::UserRole+1] = "name";
return roles;
}
Q_INVOKABLE int rowHeight()
{
return _rowHeight;
}
};
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <qqmlcontext.h>
#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
#include "mymodel.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
MyModel model;
QQmlContext* ctxt = engine.rootContext();
ctxt->setContextProperty("myModel", &model);
engine.addImportPath(":/.");
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}