Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Component setData for loading a bunch of qml files from memory.
Forum Updated to NodeBB v4.3 + New Features

Component setData for loading a bunch of qml files from memory.

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 2 Posters 4.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nizeguy
    wrote on last edited by
    #1

    I want to load qml files from memory using Component.setData .

    The syntax is:
    void QQmlComponent::setData(const QByteArray & data, const QUrl & url) [slot]

    What i would like to know if it is possible to load alot of components to memory and work from there, given the QUrl as their name+relative_location.

    execute like ./qmltest tutorial1.qml

    my current example returns this error:

    QQmlComponent: Component is not ready
    "file:///tmp/hello.qml:4:1: NiButton is not a type"

    my current example follows:

    @
    #include <QtCore>
    #include <QtQuick/QtQuick>
    #include <QtQml/qqml.h>
    #include <QDebug>
    #include <Qt/qapplication.h>

    QQuickItem * createComponent(QQmlEngine * in_engine, QQmlContext * in_context, QByteArray in_data, QUrl in_url=QUrl(), QQuickItem * in_parent=0) {
    QQmlComponent *component = new QQmlComponent (in_engine);
    component->setData(in_data, in_url);
    QQuickItem *rect = qobject_cast<QQuickItem *> ( component->create(in_context) );

    if (!component->errors().isEmpty()) {
        foreach (const QQmlError &error, component->errors())
        qWarning() << error.toString();
        return 0;
    }
    if (in_parent)
        rect->setParentItem(qobject_cast<QQuickItem *>(in_parent));
    return rect;
    

    }

    int main(int argc, char ** argv)
    {
    QApplication app(argc, argv);
    QQuickView *qxView = new QQuickView();

    QString pluginImportPath;
    QQmlEngine* engine = qxView->engine();
    if (pluginImportPath.isEmpty()) {
        QDir cur = QDir::current();
        cur.cd(pluginImportPath);
        pluginImportPath = cur.absolutePath();
    

    Q(pluginImportPath );
    QDir::setCurrent(pluginImportPath);
    engine->addImportPath(pluginImportPath);
    }

    // QQmlContext * tmp_context = new QQmlContext( qxView->rootContext());
    QQmlContext * tmp_context = qxView->rootContext();

    app.arguments().size() ? qxView->setSource(app.arguments()[1]) : qxView->setSource(QUrl("hello.qml"));
    
    QUrl url1 = QUrl::fromLocalFile&#40;pluginImportPath+"/"+"hello.qml"&#41;;
    QByteArray data1("import QtQuick 2.0\n import \".\"\n Rectangle{ width:200; height:200; color:\"red\";\nNiButton{}}");
    
    QUrl url2 = QUrl::fromLocalFile&#40;pluginImportPath+"/"+"NiButton.qml"&#41;;
    QByteArray data2("import QtQuick 2.0\n Rectangle{ width:200; height:250; color:\"green\";}");
    
    QQuickItem * parent1 = app.arguments().size() ? qxView->rootObject() : 0 ;
    
    QQuickItem * item2   = createComponent(engine,tmp_context,data2,url2);
    
    QQuickItem * item1   = createComponent(engine,tmp_context,data1,url1,parent1);
    
    
    QObject::connect(qxView->engine(), SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
    qxView->show();
    return app.exec();
    

    }
    @

    And a qml file to serve as qml root called tutorial1.qml:
    @
    import QtQuick 2.0

    Rectangle {
    id: page
    width: 320; height: 480
    color: "lightgray"
    Text {
    id: helloText
    text: "Hello world!"
    y: 30
    anchors.horizontalCenter: page.horizontalCenter
    font.pointSize: 24; font.bold: true
    }
    }
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tolszak
      wrote on last edited by
      #2

      Problem is with NiButton. Why do you create it dynamically as Item2 since you declaring it in item1?
      This two files (hello.qml and NiButton.qml) are in the same directory but i think that "import "."" part is problem. Try "import ""+pluginImportPath+""\n". I suppose that import "." is not reference to hello.qml file directory.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nizeguy
        wrote on last edited by
        #3

        Thanks for the reply.
        Could you try if it works for you then.
        I made all combinations of paths(executable dir, etc) and couldn't get it to work.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tolszak
          wrote on last edited by
          #4

          Could You provide tarball of above code with examplary qml files ?

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved