3

In the 4.8 docs, it is specified that:

When using files with relative paths, the path should be relative to the file whereQt.createComponent() is executed.

So I use the following to create an object:

Qt.createComponent("./foo/bar.qml").createObject(_this)

But I get the error:

QQmlComponent: Component is not ready

I have also tried (without luck) "foo/bar.qml". Changing to "foobar.qml" works fine, with the directory structure:

./
 +--foo/
 |     `--bar.qml
 `--foobar.qml

I notice however that there is no mention of relative URLs in the version 5 docs, so is there a new way to do this that I'm missing?

OJFord
  • 10,522
  • 8
  • 64
  • 98

1 Answers1

0

I doubt this is directly related to relative paths. The "Component is not ready" error means that you're calling createObject() before the component is ready. Instead, you need to listen for the component's statusChanged signal and create the object only when the status changed to Component.Ready.

Take a look at Dynamic QML Object Creation from JavaScript for a complete example and more detail.

MrEricSir
  • 8,044
  • 4
  • 30
  • 35
  • I also receive that error when I attempt to create from a nonexistent file though; hence my assumption. – OJFord Oct 19 '14 at 02:15
  • @OllieFord If that's the case, I'd double check to make sure you're running the executable from the same path that the QML files are located in. – MrEricSir Oct 19 '14 at 03:30
  • I must be, I'm using Qt Creator with the (incomplete) project structure as in OP. Opening from project root works fine, but from subdirectories does not. – OJFord Oct 19 '14 at 13:38
  • Also, I can verify that it never becomes ready, by doing `while(cmpnt !== Component.Ready){};cmpnt.createObject(_this)`, which just hangs. – OJFord Oct 19 '14 at 13:42