我遇到了这个问题,其中 a 的行ListView
并不总是正确排列。看起来它可能是一个错误,但我有一个解决方法,可以通过剪辑修复它。但这只是一个意外吗?
问题与模型中有大量行有关,但我所有的行都是整数高度并且彼此相同,所以这应该不是问题。
main.qml
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);
}
}
}
东西.qml
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"
}
}
}
我的模型.h
#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();
}
这是您向下滚动时看到的内容。请注意标题与蓝色框重叠。
为什么是这样?
如果将repeater
其更改为只是1
而不是10
消失,但我认为这只会降低可能性。
如果该clip:true
行被注释,它可以工作,但我不知道为什么。
这是项目文件的要点。 https://gist.github.com/anonymous/8c314426fe4f8764e22819f63e7f50fc
qt5.6/windows/mingw
感谢您提供任何信息。