derM
derM

Reputation: 13711

Accessing a Components Property prior to the Instantiation

I have the following problem: I have a list of Components, that contain Rectangles with a width of either 200 or 800. I'd like to filter this list, and only create objects of the Rectangles with a width of 200 as I work on a small screen.

Preferably I do not want to create all objects, check their width, and destroy those with the wrong width again. For obvious reasons I really only want to create those with a width of 200.

To do this I would need to aquire knowledge of the width, before I instantiate them. As far as I have seen, there is no publicly available and documented way of introspecting/reflecting the Component prior to it's instantiation.

My question is: Is there a non-public way to gain knowledge about what is packaged inside of my Component? Might it be possible with C++?

Or would it at least be possible to find out what kind of Object is encapsulated? Whether it will be a CustomComponent1, a Button, a RedRectangle...

Upvotes: 0

Views: 71

Answers (1)

Romain Pokrzywka
Romain Pokrzywka

Reputation: 210

Unfortunately not. You can't even predict it, since the Component could even point to a qml file that hasn't even been downloaded yet, if it was fetched from the network.

There are a couple of things you can try though, if you have room to approach the problem from another angle:

What you can do is pass properties from outside the component into it as it gets created. Assuming you control the code within the Component, you can then adjust how the internal elements get created based on the value of the property(ies) that was(were) set from outside.

If that's not good enough, say your Component provides multiple elements and you only want to create the ones that match your criteria (possibly a combination of many), then you can introduce a second Component layer within the first Component, and have that second Component either create the actual element if it matches your criteria, or an empty Item{} if it doesn't, which is as close as it gets to not creating anything.

I hope that helps!

Upvotes: 2

Related Questions