qml: I can not import component in this way



  • I'm trying to import a qml file this way

    import "../../temas/" + X + "/ui/ScrollBar"
    

    but it does not work

    the idea is that the component is different depending on the value of X (x is a string)



  • I don't believe that this can be done, as the string concatenation here requires the evaluation of a javascript expression.

    What you can do, however, if you have an application using a C++ backend, is to register a type pointing to the place you want:

    '''
    QString str("../../temas/" + X + "/ui/ScrollBar");
    qmlRegisterType(QUrl(str), "MyScrollbar", 1, 0, "qmlName");
    '''

    Then import with:

    '''
    import MyScrollbar 1.0
    '''



  • if I change the value "x" also changes the "qmlRegisterType"?

    the idea is to change the value x. that the components loaded with the old value are destroyed, and that they load the new ones that are in another location.

    For example, I have a text editor with a scroll bar. when the value x changes, the bar is destroyed and the new bar is loaded.



  • No, qmlRegisterType doesn't use a binding, but I think I understand better what you want to do.

    Look up the Loader qml type. You can use this to load another qml component in place 'on the fly', and you can set the location of the component that it loads with a binding on the 'sourceComponent' property.

    I'm on my phone right now but if you want I can generate an example for you when I have access to my computer :)



  • I was thinking about the same, I was looking for a simpler solution so that someone who sees the code understands it easier and can copy it.

    I leave these images so you can see what I'm doing
    instagram.com/p/BgdLUOWg4XF/
    https://www.instagram.com/p/BgowF7Zjv-K/

    I'm going to try to make it work that way
    Thank you!



  • already worked, the code stayed like this:

    file.qml

    import "nd_ui.js" as Ui
    
     Item { //Block de notas
    
      Component.onCompleted: {
        textInput.forceActiveFocus()
        Ui.scrollBar(tema, bloc, flipi, Qt.Vertical)
        parent.parent.nombre = archivon(archivo) + ": Bloc de notas"
      }
    
       Item {id: bloc}
       Flickable {id: flipi}
    }
    

    nd_ui.js

    var component;
    var sprite;
    
    function scrollBar(tema, padre, scrollArea, orientation) {
        component = Qt.createComponent("../../temas/"+tema+"/ui/ScrollBar/ScrollBar.qml");
        if (component.status == Component.Ready)
            crearScrollbar(padre, scrollArea, orientation);
        else
            component.statusChanged.connect(crearScrollbar);
    }
    
    function crearScrollbar(padre, scrollArea, orientation) {
        if (component.status == Component.Ready) {
            sprite = component.createObject(padre, {
                                                "scrollArea": scrollArea,
                                                "orientation" : orientation
                                            });
    
            if (sprite == null) {
                // Manejo de error
                console.log("Error al crear objeto");
            }
        } else if (component.status == Component.Error) {
            // Error Handling
            console.log("Error loading component:", component.errorString());
        }
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.