使用QQMLContext访问嵌套ListView

Accessing nested ListView with QQmlContext

本文关键字:ListView 嵌套 访问 QQMLContext 使用      更新时间:2023-10-16

我正在尝试使用继承qabstractlistmodel的类填充QML ListView。到目前为止,我设法在此处使用QT文档来创建它,在" QABSTRACTITEMMODEL子类"部分下:

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "gamemodel.h"  
int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    GameModel model;    //A class similar to AnimalModel in Qt Documentation.
                        //It contains a QList of Objects, each having 2 QString
                        //members (title and genre).
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    model.readFile("c:/somePath/XML_G.xml"); //Initializing GameModel QList member 
                                             //using an XML file
    QQmlContext *ctxt = engine.rootContext();
    ctxt->setContextProperty("myModel", &model);
    return app.exec();
}

main.qml

import QtQuick 2.5
import QtQuick.Window 2.2
Window
{
    id: win
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    ListView
    {
        id: myList
        width: parent.width
        height: 50
        clip: true
        spacing: 5
        orientation: ListView.Horizontal
        model: myModel
        delegate:
        Rectangle
        {
            width: 150
            height: 20
            color: "#2255ff"
            Text
            {
                text: gameTitle + " " + genre
            }
        }
    }
} 

到目前为止,我的代码有效。但是,如果我尝试更改我的 main.qml 文件:

import QtQuick 2.5
import QtQuick.Window 2.2
Window
{
    id: win
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Item    //ListView is now nested in this Item
    {
        ListView
        {
            id: myList
            width: parent.width
            height: 50
            clip: true
            spacing: 5
            orientation: ListView.Horizontal
            model: myModel
            delegate:
            Rectangle
            {
                width: 150
                height: 20
                color: "#2255ff"
                Text
                {
                    text: gameTitle + " " + genre
                }
            }
        }
    }
} 

我最终无法使用 ctxt-> setContextProperty(" mymodel",&amp; model);。从QT文档中我几乎无法收集的东西(尽管我很可能是错误的),QQMLContext的作用就像QML文件的范围。想到,我尝试更改它:

QQmlContext *ctxt = engine.rootContext();

QQmlContext *ctxt = engine.rootContext()->findChild<QQmlContext*>("list");

以及将我的项目的 objectName 属性设置为"列表"。显然,这失败了,这也导致了崩溃。由于我在QML的经验仅限于QT文档,因此我在找到解决方法方面没有成功。使用QQMLContext的解决方案是否可能是可能的,还是我必须使用Qobject?如果是这样, ctxt-> setContextProperty(" mymodel",&amp; model) be be be

的qobject等效等效于什么。

setContextProperty()调用的第一个参数基本上是对象的"标识符",就像QML侧的id属性一样。

您需要在QML中访问它之前将其设置,否则在使用时未知。

因此,您不需要任何其他电话,但是您需要在加载需要它的QML之前进行此操作。

只需在main.cpp

中的engine.load(...)行之前移动它

好吧,显然我的问题在我的QML文件中。在我的代码中,我设置了这样的listView:

width: parent.width

但是,当我添加一个项目作为我的listView的父时,我忘了为项目设置初始宽度,从而将ListView的宽度转换为0。。