今天我想和QtQuick2一起玩一点.所以我开始将非常简单的Qt Quick1应用程序移植到Quick2.这个应用程序使用一些模型 在Qt5中,模型无法正常工作:我无法使用角色访问数据.
这是我的QML代码:
import QtQuick 2.0
Rectangle {
width: 800
height: 360
ListView {
model: mainModel
spacing: 5
anchors.fill: parent
orientation: ListView.Vertical
delegate: Text {
text: "1"
Component.onCompleted: {
console.log(mainModel);
console.log(mainModel.roles() );
console.log(model);
console.log(model.homm); // `homm` is my roleName
console.log(homm);
}
}
Run Code Online (Sandbox Code Playgroud)
在Qt4.8中,我能够使用roleName语法获取数据(在此QML中我的roleName = homm)但在Qt5中我不能.那是在控制台中写的:
MainModel(0x7fff08beff80)
homm,wtf
QQuickVDMAbstractItemModelData(0x23c96e0)
undefined
file:///media/disk/kakadu/prog/qt/quick2test/qml/quick2test/main.qml:20: ReferenceError: homm is not defined
Run Code Online (Sandbox Code Playgroud)
这就是Quick 1.1
MainModel(0x7fffe58182f0)
undefined
QDeclarativeVisualDataModelData(0x2372ea0)
QVariant(MiniModel*)
QVariant(MiniModel*)
Run Code Online (Sandbox Code Playgroud)
您可以看到使用角色访问数据按预期工作.我为您创建了测试应用程序: Qt5和Qt 4.8.我希望你能帮助我找到物质的核心.
PS我在Qt5版本上做了一些改动.在Qt5中,方法setRoleNames()已过时,roleNames()建议覆盖.我做了这个压倒一切.
PPS我的代码示例应该可以在GNU/Linux x64上编译
我已经创建了这个 QML 窗口,但是当我用鼠标抓住它的边框并调整它的大小时,窗口的内容重新绘制非常缓慢。你可以克隆我的repo并自己测试。任何想法如何提高性能?
import QtQuick 2.0
Rectangle {
id: root
width: 600
height: 600
color: "lightgrey"
ListView {
model: mainModel
spacing: 5
width: parent.width
orientation: ListView.Horizontal
delegate: Rectangle {
//width: parent.desiredWidth / mainModel.dataLen()//+ " " + model.sort
width: root.width / mainModel.dataLen() - 10
//width: 200
ListView {
id: lv1
ScrollBar {
flickable: lv1
vertical: true
hideScrollBarsWhenStopped: false
scrollbarWidth: 5
}
model: homm
spacing: 5
width: parent.width
height: 150
orientation: ListView.Vertical
delegate:
Rectangle {
radius: 5
anchors.rightMargin: 5
anchors.leftMargin: …Run Code Online (Sandbox Code Playgroud) 这是这个问题的更具体版本。
我想向视图报告我将向我的模型插入一些行。有两种调用方式beginInsertRows:
beginInsertRows(QModelIndex(), first, last)
Run Code Online (Sandbox Code Playgroud)
和
beginInsertRows(createIndex(-1,-1), first, last)
Run Code Online (Sandbox Code Playgroud)
有一个区别:在第二个变体中,QModelIndex将在其内部存储指向我们模型的指针。在 Qml 内部:
void QQuickVisualDataModel::_q_rowsInserted(const QModelIndex &parent, int begin, int end)
{
Q_D(QQuickVisualDataModel);
qDebug() << "d->m_adaptorModel.rootIndex = " << d->m_adaptorModel.rootIndex;
if (parent == d->m_adaptorModel.rootIndex)
_q_itemsInserted(begin, end - begin + 1);
}
Run Code Online (Sandbox Code Playgroud)
语句主体if不会被执行,因为它似乎rootIndex总是不存储指向模型的指针(即它存储 NULL)
我已经创建了测试 Qt5 应用程序
所以,我的问题是: 这是错误还是功能?有人在我之前尝试过这个错误的调用吗?