当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - ListView in QML - empty rows

问题描述:

I see the list, it has right amount of the rows(acording to the elements amount in the QStringList), but rows are empty.

What am i doing wrong?

in c++ my code is:

QStringList s;

s.append("1");

s.append("2");

s.append("2");

s.append("2");

s.append("2");

s.append("2");

QObject * pList = pQmlObject->findChild<QObject*>("devicesList");

pList->setProperty("model",s);

and in QML i do:

 ListView {

id:devicesList

objectName:"devicesList"

width: 100; height: 100

model: myModel

delegate: Rectangle {

height: 50

width: 100

}

}

Thanks a lot for the help!

网友答案:

You need to set the text for that ListView. In your case:

ListView {
            id:devicesList
            objectName:"devicesList"
            width: 100; height: 100
            model: myModel
            delegate: Rectangle {
                height: 50
                width: 100
                Text { text: modelData }
            }
        }

According to the documentation,

A model may be a simple QStringList, which provides the contents of the list via the modelData role.

Edit #1:

I get the warning ReferenceError: myModel is not defined when I am working with your C++ code.

Maybe it's my fault because I don't have the whole code, but in any case here you have a different way to interact with QML Objects:

#include <QGuiApplication>
#include <QStringList>

#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>

int main(int argc, char ** argv)
{
    QGuiApplication app(argc, argv);

    QStringList s;
    s.append("1");
    s.append("2");
    s.append("2");
    s.append("2");
    s.append("2");
    s.append("3");

    QQuickView view;

    QQmlContext *ctxt = view.rootContext();
    ctxt->setContextProperty("myModel", QVariant::fromValue(s));

    view.setSource(QUrl("qrc:view.qml"));
    view.show();

    return app.exec();
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: