[Interest] QQmlComponent setData usage

mark diener rpzrpzrpz at gmail.com
Thu May 28 23:10:49 CEST 2015


Hello:

I have a small sample program to clarify how to use QQmlComponent::setData(
).

Of course, the QML engine cannot load a component due to unknown Myrect type

Can anyone comment on how to use setData( ) so that Myrect is visible as a
component.

Application output:

Debugging starts

QML debugging is enabled. Only use this in a safe environment.

QML Debugger: Waiting for connection on port 51293...

Qml Component ready.........

QQmlApplicationEngine failed to load component

:6 Myrect is not a type


Here is the small sample code:

main.cpp
##############

#include <QGuiApplication>

#include <QQmlApplicationEngine>

#include <QQmlComponent>

#include <QQmlEngine>

#include <QThread>

#include <QDebug>


int main(int argc, char *argv[])

{

    QGuiApplication gapp(argc, argv);

    QQmlApplicationEngine geng;


    //Below line WORKS - commented out for testing

    //QByteArray gmainqml("import QtQuick 2.4\n import QtQuick.Window
2.2\n Window { visible: true\n width: 300\n height: 500\n Rectangle {
color:\"blue\"\n height:100\n width:100\n }\n Text { anchors.centerIn:
parent\n text: \"setData\"\n }\n}") ;


    //This byte array has a reference to Myrect component

    QByteArray gmainqml("import QtQuick 2.4\n import QtQuick.Window
2.2\n Window { visible: true\n width: 300\n height: 500\n Myrect {
height:100\n width:100\n }\n Text { anchors.centerIn: parent\n text:
\"setData\"\n }\n}") ;


    //A QML Component stored in a ByteArray

    QByteArray gcompqml("import QtQuick 2.4\n Rectangle { color: \"blue\" }");


    QQmlComponent gcomp(&geng) ;


    gcomp.setData(gcompqml,QUrl("Myrect"));


     if (gcomp.isReady() == false)

    {

        //Give it one whole second to load, that should be enough time

        QThread::sleep(1) ;

    }

    if (gcomp.isReady() == false)

    {

        qDebug() << "Qml Component not ready" ;

        return (0);

    }

    qDebug() << "Qml Component ready........." ;



    //Nothing on the register type area other than to define your QML
in a file or C++ class    //qmlRegisterType("Rpz",1,0,"Myrect.qml");

    geng.loadData(gmainqml) ;


    return gapp.exec();

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150528/c838fd69/attachment.html>


More information about the Interest mailing list