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?
Forum Update on Monday, May 27th 2025

How to pass string value to loader iem?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 3 Posters 1.6k 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.
  • M Offline
    M Offline
    Mr Pang
    wrote on 8 Jan 2016, 05:35 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 8 Jan 2016, 08:02
    0
    • M Offline
      M Offline
      Mr Pang
      wrote on 8 Jan 2016, 06:52 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
      • C Offline
        C Offline
        Charby
        wrote on 8 Jan 2016, 07:43 last edited by
        #3

        Hi, would you please show the code?

        1 Reply Last reply
        0
        • M Mr Pang
          8 Jan 2016, 05:35

          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 8 Jan 2016, 08:02 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 C 2 Replies Last reply 8 Jan 2016, 08:27
          0
          • D Devopia53
            8 Jan 2016, 08:02

            @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 8 Jan 2016, 08:27 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
              8 Jan 2016, 08:02

              @Mr-Pang

              Below the methods typically used:

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

              C Offline
              C Offline
              Charby
              wrote on 8 Jan 2016, 09:20 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

              1/6

              8 Jan 2016, 05:35

              • Login

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