Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QQmlComponent: Component is not ready
QtWS25 Last Chance

QQmlComponent: Component is not ready

Scheduled Pinned Locked Moved Solved General and Desktop
qqmlcomponentqqmlengineqqmlcontext
4 Posts 2 Posters 5.2k Views
  • 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.
  • D Offline
    D Offline
    devDawg
    wrote on 28 Jun 2018, 14:49 last edited by devDawg
    #1

    Hi all,

    So before you flame me for not doing my research, I have looked through the Qt Forums & Stack Overflow to see how other people have dealt with this issue. Unfortunately, my situation seems to be slightly more complicated.

    Like the post title says, I am getting a "QQmlComponent: Component is not ready" error.

    main.cpp:

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QFile>
    #include <QDebug>
    #include <QQmlComponent>
    #include <QUrl>
    #include <QStringLiteral>
    #include "datamodel.h"
    #include "datamodule.h"
    #include "datapiece.h"
    
    int main(int argc, char *argv[])
    {
    
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QApplication app(argc, argv);
    
        qmlRegisterType<DataPiece>("DataPackage",1,0,"DataPiece");    
        qmlRegisterType<DataModule>("DataPackage",1,0,"DataModule");
    
        QQmlApplicationEngine engine;
    
    
        //    qDebug() << comp.errorString();
        //    qDebug() << comp.status();
        //    qDebug() << comp.progress();
    
        QQmlContext * context = engine.rootContext();
        DataModel model("C:\\Users\\AMcFarlane\\Qt\\projects\\UiTest\\mock_loader_test");
        context->setContextProperty("dataModel",&model);
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        QQmlComponent comp(&engine,QUrl("C:\\Users\\AMcFarlane\\Qt\\projects\\UiTest\\Display0.qml"));
        QFile file("C:\\Users\\AMcFarlane\\Qt\\projects\\UiTest\\Display0.qml");
        qDebug() << "path correctness: " <<file.exists();
        qDebug() << "status: "<<comp.status()<<endl<<"progress: "<<comp.progress();
    
        QObject *summaryPage = comp.create(context);
    
    
    
        return app.exec();
    
    }
    

    The output from those qDebug statements near the bottom looks like this:

    path correctness:  true
    status:  QQmlComponent::Status(Loading) 
    progress:  0
    QQmlComponent: Component is not ready
    

    So the filepath is correct. According to the compiler, my component is "Loading", but as you can see, progress is at 0%.

    I have also tried calling comp.create() with & without the context argument.

    That being said, I have 2 suspicions.

    I am thinking that there could be a problem with my execution flow. I tried rearranging things, putting the QQmlComponent instantiation before & after loading the engine and setting my context property, to no avail.

    Secondly, do I need to manually tell my program to wait until the QQmlComponent has loaded? I don't foresee this being true, as I feel like it really should not take long at all.

    I would really appreciate any ideas or possible suggestions. Thank you!

    EDIT: I included a while loop that IF the component is "loading", it waits until the component has loaded, regularly reporting progress. This resulted in a console full of zeros, and a crashed program.

    Anything worthwhile is never achieved easily.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KillerSmath
      wrote on 28 Jun 2018, 15:15 last edited by
      #2

      An old post with same problem:
      https://forum.qt.io/topic/54517/solved-qqmlcomponent-component-is-not-ready/9

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      D 1 Reply Last reply 28 Jun 2018, 15:33
      2
      • K KillerSmath
        28 Jun 2018, 15:15

        An old post with same problem:
        https://forum.qt.io/topic/54517/solved-qqmlcomponent-component-is-not-ready/9

        D Offline
        D Offline
        devDawg
        wrote on 28 Jun 2018, 15:33 last edited by
        #3

        @KillerSmath That was one of the first posts I came across in troubleshooting this.

        So, I did just solve it, but in a very peculiar manner.

        Initially when I tried to test the file path with QFile::exists(), it returned false when I instantiated the file with "qrc:/filename.qml" and return true when I instantiated with the full path instead.

        This led me to rely on using the full path when trying to instantiate the QQmlComponent, which turned out to be my problem.

        changing QQmlComponent comp(&engine,QUrl("C:\\Users\\AMcFarlane\\Qt\\projects\\UiTest\\Display0.qml"));

        to QQmlComponent comp(&engine,QUrl(QStringLiteral("qrc:/Display0.qml")));

        That seemed to fix the issue. So in retrospect, QFile & QUrl have slightly different tastes when it comes to file paths.

        NOTE: You can pass a QString to the QUrl, just as easily as a QStringLiteral. Both work.

        Anything worthwhile is never achieved easily.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KillerSmath
          wrote on 28 Jun 2018, 16:28 last edited by KillerSmath
          #4

          @devDawg

          In memory, resources are represented by a tree of resource objects. The tree is automatically built at startup and used by QFile for resolving paths to resources. You can use a QDir initialized with ":/" to navigate through the resource tree from the root.

          So, you could use:

          QQmlComponent comp(&engine,QUrl("qrc:/Display0.qml"));
          QFile file(":/Display0.qml"); // acessing by resource tree
          

          NOTE: QStringLiteral is a optimization case, you can use QStringLiteral to avoid the implicit conversion between char* to QString and then copy the data.

          References:
          Qt Resource Documentation - Using resource in the application
          QStringLiteral Explanation

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          1 Reply Last reply
          3

          3/4

          28 Jun 2018, 15:33

          • Login

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