0

i have an application which sets properties of Qml from C++, so far i was able to set property of any type. I am able to create QQmlComponent in C++ and set it to property.

But first i need to initialize component with its properties. I can do that with create/beginCreate and set properties. But i cant change created QObject back to QQmlComponent, therefor i cant set it to givent Component type property.

Is there some way how to conver QObject to QQmlComponent ? Or is there any other way to initialize QQmlComponent properties ?

Example, of what i want to achiev in C++, how it looks in qml

import QtQuick 2.3

Loader {
    // property of type Component
    sourceComponent: Text {
    
        text: "property i want to intialize, this component can be in file"
    }
}

Edit: I will clarify what im after. Suppose i have something like JSON given by user. e.g:

{
   "type":"Loader",
   "sourceComponent":{
      "type":"Text",
      "text":"some property of property of type QQmlComponentProperty"
   }
}

Then from C++ i create given items, problem is how to create QQmlComponent with user given properties.

Kwan
  • 23
  • 4
  • I think you don't understand it quite right. A component is kind of template you create an item from. item - it is an instance, component is a template. you cannot convert on to another and back. I guess you have to clarify your question. What exactly do you want to archive? – folibis Nov 16 '20 at 11:05
  • i have file defined by user, which has properties of some qml object (e.g Rectangle etc). in this file can be properties of type QQmlComponent and i dont know how to assign their properties. I will give example in question. – Kwan Nov 16 '20 at 12:22
  • Added another example hope its is clearer now :) – Kwan Nov 16 '20 at 12:33
  • Ok, when you create an object, I guess using [QQmlComponent::create](https://doc.qt.io/qt-5/qqmlcomponent.html#create) you get a pointer to QObject, and so you can set/get a property you want using the [Qt property system](https://doc.qt.io/qt-5/properties.html#reading-and-writing-properties-with-the-meta-object-system), for example `ptr->setProperty("text","some property of property of type QQmlComponentProperty")`. I mean you set a property to _instance_, not to _component_. – folibis Nov 16 '20 at 12:40
  • yes thats what im doing, but you cannot then set sourceComponent with created instance because its different type. Thats what im after :) – Kwan Nov 16 '20 at 12:46
  • do you mean Loader? what a sense to use Loader for already created component? Loader _instantiates_ item from component. If you already have an instance there is no need to use Loader. – folibis Nov 16 '20 at 12:50
  • loader is just example. Other example would be CircularGauge, which has style Property. – Kwan Nov 16 '20 at 12:52
  • It doesn't matter, the principle is the same. You set property to an instance, not to a Component. Component has no properties except its own. It doesn't even exist. You can change an instance of item, not the template / type. The example in C++ - `int i = 10` ... you cannot change the type - `int`, but you can change the instance - `i`. a class would be more appropriate as an example but it's the same ... – folibis Nov 16 '20 at 12:59
  • yeah i know that i cant cant change type but intsnace of component after create() is QObject, i cant set QObject to QQmlComponent. Or maybe i dont understand you :) – Kwan Nov 16 '20 at 13:08
  • why you want to set QObject to QQmlComponent? – folibis Nov 16 '20 at 13:09
  • Because its what i get after i create an instance of QQmlComponent (becuase i dont have way to assign its initial properties) with QQmlComponent.create() – Kwan Nov 16 '20 at 13:12
  • I really don't understand. You have the pointer of type `QObject *` to the instance of your component. Now you can set/change any property you want. Why do you want to assign it to `QQmlComponent`? Looks like XY problem ... – folibis Nov 16 '20 at 13:32
  • because created instance should be a property value (but instance is QObject and type of property im assingnig it to is QQmlComponent see example). – Kwan Nov 16 '20 at 13:47
  • property of what? please provide example – folibis Nov 16 '20 at 13:56
  • Rather than describing your C++ code, please show what you have tried. – JarMan Nov 16 '20 at 14:13

0 Answers0