相关文章推荐
风度翩翩的钢笔  ·  pytorch ...·  6 月前    · 
知识渊博的哑铃  ·  一个有趣的问题, ...·  11 月前    · 
长情的回锅肉  ·  How to import my dbc ...·  1 年前    · 
瘦瘦的小摩托  ·  iphone6S ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to create a component of an existing imported type. While this task seems important enough (say when creating components of types found in your own installed QML plugins) it does not seem to be documented. For example, when trying to create a component of MyObject , the workaround is to create a MyObjectComponent.qml file in the application as follows:

import MyPackage 1.0
MyObject{}

Then a component from this object can be created with Qt.createComponent("MyObjectComponent.qml"), but this method seems redundant. Is there a more concise way? I would expect Qt.createComponent("MyObject") to work, but it doesn't.

What is significant difference between Qt.createComponent("MyObjectComponent.qml") and Qt.createComponent("MyObjectComponent") in your vision? Also you can directly create component, using Component.createObject. – folibis Jun 26, 2018 at 11:39 @folibis: No, Component.createObject creates an object from a component, not a component. – derM Jun 26, 2018 at 15:27 @OP: What do you want to do? I guess you have some problems distinguishing between Types (Qml-Files, registered Types etc.), Components and Objects. As to my knowledge, it is impossible to create Components (prototypes) again out of Objects? – derM Jun 26, 2018 at 15:30 @derM imagine I want to do something like import QtQuick 2.11 ... Qt.createComponent("Item");. I guess the correct term is indeed Type here. – Ayberk Özgür Jun 27, 2018 at 7:15

Qt.createComponent is specified to take a URL as argument, not a type name, so no, there is no way to use Qt.createComponent(Type). But I still don't get, what could be any benefit of this.

  • It gives you no flexibility, since there is no QML-Type to pass around types as values.
  • It gives you no performance benefit, since Qt.createComponent(URL) also uses the engines component cache.
  • Also there are only few use cases where explicit JS component creation with Qt.createComponent is the right way to go, since QML is a declarative language and most things can be done declarative.

    Consider these other ways to create components:

    If the a type of a property is Component, you can use standard syntax to create objects. Automatically the object creation stops after the createComponent-step:

    property Component someProperty: Item {
        // This Item is not instantiated. Instead only a prototpye/component is created
        // and bound to the property 'someProperty'
    

    Wrap the Object you don't want to fully create yet, in a Component:

    Component {
        id: myComponent // Use this to reference the Component later.
        Item {
            // This Item is not instantiated. Instead only a prototpye/component is created
            // You can reference it by the *Component's id*
    

    This can also be used in property assignment:

    property var someProperty: Component {
        Item {
      

    TL;DR
    No - you can't pass a type to a function in QML, so you can't do it with Qt.createComponent either - especially not, when it is specified to take a URL.
    If you still have the feeling that it would be necessary, and any of the other ways to create Components seem to not fulfill your needs, please ask again, and specify what you try to do, and why you think it would be necessary to do so, and we might find a way to solve your real problem.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.