Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved qml: I can not import component in this way

    QML and Qt Quick
    3
    7
    1009
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dante Leoncini
      Dante Leoncini last edited by

      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)

      1 Reply Last reply Reply Quote 0
      • L
        LScott last edited by

        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
        '''

        1 Reply Last reply Reply Quote 0
        • Dante Leoncini
          Dante Leoncini last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • L
            LScott last edited by

            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 :)

            1 Reply Last reply Reply Quote 1
            • Dante Leoncini
              Dante Leoncini last edited by

              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!

              1 Reply Last reply Reply Quote 0
              • Dante Leoncini
                Dante Leoncini last edited by

                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());
                    }
                }
                
                1 Reply Last reply Reply Quote 0
                • A
                  AntonyB Banned last edited by

                  This post is deleted!
                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post