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. How to pass string value to loader iem?
Qt 6.11 is out! See what's new in the release blog

How to pass string value to loader iem?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 2.2k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Mr Pang
    wrote on last edited by
    #1

    I have a child.qml which will be loaded by a Loader.
    child.qml has a Text
    I want to pass a string to Text Dynamically.
    How to do it?
    Thanks for your guidance!

    D 1 Reply Last reply
    0
    • M Offline
      M Offline
      Mr Pang
      wrote on last edited by
      #2

      I define a property string origName in parent Loader,
      child.qml uses parent.origName to reference it.
      When I changed the source property of Loader, I got "TypeError: Cannot read property 'origName' of null"
      Is this a serious problem?

      1 Reply Last reply
      0
      • CharbyC Offline
        CharbyC Offline
        Charby
        wrote on last edited by
        #3

        Hi, would you please show the code?

        1 Reply Last reply
        0
        • M Mr Pang

          I have a child.qml which will be loaded by a Loader.
          child.qml has a Text
          I want to pass a string to Text Dynamically.
          How to do it?
          Thanks for your guidance!

          D Offline
          D Offline
          Devopia53
          wrote on last edited by
          #4

          @Mr-Pang

          Below the methods typically used:

          yourLoader.setSource("child.qml", {"text": "yourMessages"}); // "text" is property name on child.qml item

          M CharbyC 2 Replies Last reply
          0
          • D Devopia53

            @Mr-Pang

            Below the methods typically used:

            yourLoader.setSource("child.qml", {"text": "yourMessages"}); // "text" is property name on child.qml item

            M Offline
            M Offline
            Mr Pang
            wrote on last edited by
            #5

            @Devopia53
            Thank you very much.
            I am too careless to find out this method.

            1 Reply Last reply
            0
            • D Devopia53

              @Mr-Pang

              Below the methods typically used:

              yourLoader.setSource("child.qml", {"text": "yourMessages"}); // "text" is property name on child.qml item

              CharbyC Offline
              CharbyC Offline
              Charby
              wrote on last edited by
              #6

              I think it would have help to have a complete working code as there many ways to use a loader and passing a string...

              Here is a sample with 4 ways :
              main.qml

              import QtQuick 2.2
              import QtQuick.Window 2.1
              import QtQuick.Controls 1.2
              import QtQml 2.2
              
              Window{
                  id:page1
                  visible:true;    width:640;    height:480
              
                  Loader{
                      id:yourLoader
                      property string origName : "orig"
                      source:"child.qml"
                      anchors.top : button.bottom
                  }
              
                  Button{
                      id:button
                      text:"change"
              
                      onClicked: {
                          var curTime = new Date().toTimeString();
                          yourLoader.setSource("child.qml", {"text": "text:"+curTime});
                          yourLoader.item.text2 = "text2:"+curTime;
                          yourLoader.origName = "text3 and text4:"+curTime;
                      }
                  }
              }
              

              and child.qml

              import QtQuick 2.0
              
              Item {
                  id: root
                  property string text : "init 1"
                  property string text2 : "init 2"
                  property string text3 : yourLoader.origName
                  property string text4 : parent ? parent.origName : ""
              
                  Text{
                      text : parent.text + "\n" + parent.text2 + "\n" + parent.text3 + "\n" + parent.text4
                  }
              }
              

              as you can see the 3 first ways are working fine without error whereas the 4th requires to check the validity of parent before accessing its property.

              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