0

As a beginner in Qbs/QML I want to get information about what instance that imported our module. For example, given this project:

.
├── a
│   └── imported.qbs
└── b
    └── main.qbs

with a/imported.qbs like this:

Product{
    name:{
        console.info("I was imported from" + XXX);
        return "imported"
    }
}

and b/main.qbs like this:

import "../a/imported.qbs" as Imported
Imported {}

When I run main.qbs, how could I get the instance that imported imported.qbs (main.qbs)?

Or in other words, what should I susbstitute for XXX to print main.qbs.

Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95

2 Answers2

0

I don't think you can get the file name of the instantiating item, nor should it be relevant.

  • I don't really need the filename, but rather a reference to the instance. I want to look at the variables inside it. – Mr. Developerdude Feb 02 '23 at 03:30
  • The instance of what exactly? You *are* in the instance of the Product. – Christian Kandeler Feb 02 '23 at 09:50
  • True... and yet the runtime knows that the "Product" was invoked when it read a file called "b/main.qbs", it is in the memory of the computer running qbs, just not available to us from the script. – Mr. Developerdude Feb 03 '23 at 20:26
  • @LennartRolland inheritance information is simply not available to the JS engine when Qbs evaluates property expressions. Imports are resolved very early in the parsing process and when the first parser stage is done, inheritance information is thrown away. The resulting project/product item tree contains flat items where each item consists of a map of properties with property names as the keys and JS expressions as values. The parser only knows in which .qbs file an expression was defined, but even that information is not available to the expressions, but only to the engine. – Richard W Feb 08 '23 at 20:53
0

I solved this myself, and thought maybe someone else might have use from my findings. I solved it like this:

In b/main.qbs I created property name2 & path2 to hold the local name & path like so:

import "../a/imported.qbs" as Imported
Imported {
    property string path2:path
    property string name2:name
}

and in a/imported.qbs I use the properties like this:

Product{
    name:{
        console.info("I was imported from" + name2 + " (" + path2 + ")");
        return "imported"
    }
}
Mr. Developerdude
  • 9,118
  • 10
  • 57
  • 95