Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. qml: I can not import component in this way
QtWS25 Last Chance

qml: I can not import component in this way

Scheduled Pinned Locked Moved Solved QML and Qt Quick
7 Posts 3 Posters 1.4k Views
  • 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 LeonciniD Offline
    Dante LeonciniD Offline
    Dante Leoncini
    wrote on last edited by
    #1

    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
    0
    • L Offline
      L Offline
      LScott
      wrote on last edited by
      #2

      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
      0
      • Dante LeonciniD Offline
        Dante LeonciniD Offline
        Dante Leoncini
        wrote on last edited by
        #3

        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
        0
        • L Offline
          L Offline
          LScott
          wrote on last edited by
          #4

          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
          1
          • Dante LeonciniD Offline
            Dante LeonciniD Offline
            Dante Leoncini
            wrote on last edited by
            #5

            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
            0
            • Dante LeonciniD Offline
              Dante LeonciniD Offline
              Dante Leoncini
              wrote on last edited by
              #6

              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
              0
              • A Offline
                A Offline
                AntonyB
                Banned
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved