Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Usage of QString in QML

Usage of QString in QML

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.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.
  • S Offline
    S Offline
    sush123
    wrote on last edited by
    #1

    Hi Everyone,

    I need to use QString in QML can anyone help me how can i use the same

    J.HilkJ ODБOïO 2 Replies Last reply
    0
    • S sush123

      Hi Everyone,

      I need to use QString in QML can anyone help me how can i use the same

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @sush123
      please some more information on what you want to actually do, and tell us what you already tried.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      S 1 Reply Last reply
      3
      • S sush123

        Hi Everyone,

        I need to use QString in QML can anyone help me how can i use the same

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by
        #3

        @sush123
        Qt Type 'QString' is converted to QML Basic Type 'string' automatically
        https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#basic-qt-data-types

        1 Reply Last reply
        2
        • J.HilkJ J.Hilk

          @sush123
          please some more information on what you want to actually do, and tell us what you already tried.

          S Offline
          S Offline
          sush123
          wrote on last edited by
          #4

          @J-Hilk

          i have a class ABC.cpp which has function called Test(QString index) looks like
          void ABC :: Test(QString index).

          In XYZ.qml i am trying to access above in qml like

          ABC.Test("Hello"); -> works fine but


          when i declare that in constants.qml like

          readonly property string TXT_HELLO: "Hello"

          and use as below
          ABC.Test("Constants.TXT_HELLO");
          it doesnot work where i am wrong.

          Pradeep P NP 1 Reply Last reply
          0
          • S sush123

            @J-Hilk

            i have a class ABC.cpp which has function called Test(QString index) looks like
            void ABC :: Test(QString index).

            In XYZ.qml i am trying to access above in qml like

            ABC.Test("Hello"); -> works fine but


            when i declare that in constants.qml like

            readonly property string TXT_HELLO: "Hello"

            and use as below
            ABC.Test("Constants.TXT_HELLO");
            it doesnot work where i am wrong.

            Pradeep P NP Offline
            Pradeep P NP Offline
            Pradeep P N
            wrote on last edited by Pradeep P N
            #5

            @sush123

            You already have the property so you can use it directly in your function without" "
            ABC.Test(TXT_HELLO);

            can you please tell us what is Constants you are referring to ?

            Below is some sample code...

            TestString.h :

            public slots:
                void printQmlString(const QString &str); 
            

            TestString.cpp:

            void TestString::printQmlString(const QString &str)
            {
                qDebug() << Q_FUNC_INFO << str << endl;
            }
            

            main.cpp:

            TestString ts;
            engine.rootContext()->setContextProperty("TS", &ts);
            

            QML:

            import QtQuick 2.6
            import QtQuick.Window 2.2
            
            Window {
                id: root
            
                visible: true
                width: 640
                height: 480
                title: qsTr("Hello World")
            
                readonly property string myString: 'Hallo Qt, :)'
            
                /*  ...*/
            
                Component.onCompleted: TS.printQmlString(root.myString)
            }
            
            

            Output:

            cd7fa0e6-3943-4a19-8ebb-60bc19d13df6-image.png

            All the best.

            Pradeep Nimbalkar.
            Upvote the answer(s) that helped you to solve the issue...
            Keep code clean.

            S 1 Reply Last reply
            3
            • Pradeep P NP Pradeep P N

              @sush123

              You already have the property so you can use it directly in your function without" "
              ABC.Test(TXT_HELLO);

              can you please tell us what is Constants you are referring to ?

              Below is some sample code...

              TestString.h :

              public slots:
                  void printQmlString(const QString &str); 
              

              TestString.cpp:

              void TestString::printQmlString(const QString &str)
              {
                  qDebug() << Q_FUNC_INFO << str << endl;
              }
              

              main.cpp:

              TestString ts;
              engine.rootContext()->setContextProperty("TS", &ts);
              

              QML:

              import QtQuick 2.6
              import QtQuick.Window 2.2
              
              Window {
                  id: root
              
                  visible: true
                  width: 640
                  height: 480
                  title: qsTr("Hello World")
              
                  readonly property string myString: 'Hallo Qt, :)'
              
                  /*  ...*/
              
                  Component.onCompleted: TS.printQmlString(root.myString)
              }
              
              

              Output:

              cd7fa0e6-3943-4a19-8ebb-60bc19d13df6-image.png

              All the best.

              S Offline
              S Offline
              sush123
              wrote on last edited by
              #6

              @Pradeep-P-N

              i am sorry i will be more clear now :

              ABC class has a function called toolbarButtonClick:
              void ABC::toolbarButtonClick(QString index)
              {

              }

              Constants.qml is a file where i am thinking to keep all the strings at one place
              looks like :

              import QtQuick 2.0
              pragma Singleton

              QtObject {
              readonly property string TXT_HELLO: "Hello"
              readonly property string TXT_WORLD: "World"
              }

              For ToolbarButton i need to have Buttonclick
              ToolbarButton {
              label: qsTr("toolbutton")

              onButtonClick: {
              ABC.toolbarButtonClick(Constants.TXT_HELLO)
              }
              onButtonLongPress: {
              ABC.toolbarLongPress(Constants.TXT_HELLO)
              }
              }

              When i use like above i dont get click event


              if i use directly strings like below it will take click event
              onButtonClick: {
              ABC.toolbarButtonClick("Hello")
              }
              onButtonLongPress: {
              ABC.toolbarLongPress("Hello")
              }

              Pradeep P NP 2 Replies Last reply
              0
              • S sush123

                @Pradeep-P-N

                i am sorry i will be more clear now :

                ABC class has a function called toolbarButtonClick:
                void ABC::toolbarButtonClick(QString index)
                {

                }

                Constants.qml is a file where i am thinking to keep all the strings at one place
                looks like :

                import QtQuick 2.0
                pragma Singleton

                QtObject {
                readonly property string TXT_HELLO: "Hello"
                readonly property string TXT_WORLD: "World"
                }

                For ToolbarButton i need to have Buttonclick
                ToolbarButton {
                label: qsTr("toolbutton")

                onButtonClick: {
                ABC.toolbarButtonClick(Constants.TXT_HELLO)
                }
                onButtonLongPress: {
                ABC.toolbarLongPress(Constants.TXT_HELLO)
                }
                }

                When i use like above i dont get click event


                if i use directly strings like below it will take click event
                onButtonClick: {
                ABC.toolbarButtonClick("Hello")
                }
                onButtonLongPress: {
                ABC.toolbarLongPress("Hello")
                }

                Pradeep P NP Offline
                Pradeep P NP Offline
                Pradeep P N
                wrote on last edited by
                #7

                @sush123 said in Usage of QString in QML:

                Constants

                Please tell me do you have any error on buttonClick() & buttonLongPress() ?

                something like below :
                ReferenceError: Constants is not defined

                Pradeep Nimbalkar.
                Upvote the answer(s) that helped you to solve the issue...
                Keep code clean.

                1 Reply Last reply
                0
                • S sush123

                  @Pradeep-P-N

                  i am sorry i will be more clear now :

                  ABC class has a function called toolbarButtonClick:
                  void ABC::toolbarButtonClick(QString index)
                  {

                  }

                  Constants.qml is a file where i am thinking to keep all the strings at one place
                  looks like :

                  import QtQuick 2.0
                  pragma Singleton

                  QtObject {
                  readonly property string TXT_HELLO: "Hello"
                  readonly property string TXT_WORLD: "World"
                  }

                  For ToolbarButton i need to have Buttonclick
                  ToolbarButton {
                  label: qsTr("toolbutton")

                  onButtonClick: {
                  ABC.toolbarButtonClick(Constants.TXT_HELLO)
                  }
                  onButtonLongPress: {
                  ABC.toolbarLongPress(Constants.TXT_HELLO)
                  }
                  }

                  When i use like above i dont get click event


                  if i use directly strings like below it will take click event
                  onButtonClick: {
                  ABC.toolbarButtonClick("Hello")
                  }
                  onButtonLongPress: {
                  ABC.toolbarLongPress("Hello")
                  }

                  Pradeep P NP Offline
                  Pradeep P NP Offline
                  Pradeep P N
                  wrote on last edited by Pradeep P N
                  #8

                  @sush123

                  Please register your Constants.qml singleton before you use it.

                  refer qmlRegisterSingletonType - QQmlEngine Class.

                  below is sample code for the solution:

                  main.cpp

                      qmlRegisterSingletonType(QUrl("qrc:/Constants.qml"), "singleton", 1, 0, "Consts");
                  

                  Constants.qml is same as you used

                  pragma Singleton
                  
                  import QtQuick 2.0
                  
                  QtObject {
                      readonly property string click: 'Mouse Click'
                      readonly property string pressHold: 'Mouse PressHold'
                  }
                  

                  and then as below example

                       MouseArea {
                          anchors.fill: parent
                  
                          onClicked: {
                              console.log("Inside QML Mouse Click")
                              TS.printQmlString(Consts.click)
                          }
                  
                          onPressAndHold: {
                              console.log("Inside QML Mouse Press&Hold")
                              TS.printQmlString(Consts.pressHold)
                          }
                      }
                  
                  

                  Output :

                  8037c541-c85c-4f81-a4f9-f70efb85ed81-image.png

                  Hope this helps :)
                  All the best.

                  Pradeep Nimbalkar.
                  Upvote the answer(s) that helped you to solve the issue...
                  Keep code clean.

                  S 1 Reply Last reply
                  4
                  • Pradeep P NP Pradeep P N

                    @sush123

                    Please register your Constants.qml singleton before you use it.

                    refer qmlRegisterSingletonType - QQmlEngine Class.

                    below is sample code for the solution:

                    main.cpp

                        qmlRegisterSingletonType(QUrl("qrc:/Constants.qml"), "singleton", 1, 0, "Consts");
                    

                    Constants.qml is same as you used

                    pragma Singleton
                    
                    import QtQuick 2.0
                    
                    QtObject {
                        readonly property string click: 'Mouse Click'
                        readonly property string pressHold: 'Mouse PressHold'
                    }
                    

                    and then as below example

                         MouseArea {
                            anchors.fill: parent
                    
                            onClicked: {
                                console.log("Inside QML Mouse Click")
                                TS.printQmlString(Consts.click)
                            }
                    
                            onPressAndHold: {
                                console.log("Inside QML Mouse Press&Hold")
                                TS.printQmlString(Consts.pressHold)
                            }
                        }
                    
                    

                    Output :

                    8037c541-c85c-4f81-a4f9-f70efb85ed81-image.png

                    Hope this helps :)
                    All the best.

                    S Offline
                    S Offline
                    sush123
                    wrote on last edited by
                    #9

                    @Pradeep-P-N

                    Thanks alot i registered using wrong type,

                    qmlRegisterSingletonType(QUrl("qrc:/Constants.qml"), "singleton", 1, 0, "Consts"); helped me

                    Pradeep P NP 1 Reply Last reply
                    3
                    • S sush123

                      @Pradeep-P-N

                      Thanks alot i registered using wrong type,

                      qmlRegisterSingletonType(QUrl("qrc:/Constants.qml"), "singleton", 1, 0, "Consts"); helped me

                      Pradeep P NP Offline
                      Pradeep P NP Offline
                      Pradeep P N
                      wrote on last edited by
                      #10

                      @sush123
                      You are welcome.

                      All the best.

                      Pradeep Nimbalkar.
                      Upvote the answer(s) that helped you to solve the issue...
                      Keep code clean.

                      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