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
Forum Updated to NodeBB v4.3 + New Features

Usage of QString in QML

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.0k 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 12 Feb 2020, 10:37 last edited by
    #1

    Hi Everyone,

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

    J O 2 Replies Last reply 12 Feb 2020, 10:38
    0
    • S sush123
      12 Feb 2020, 10:37

      Hi Everyone,

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

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 12 Feb 2020, 10:38 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 13 Feb 2020, 04:54
      3
      • S sush123
        12 Feb 2020, 10:37

        Hi Everyone,

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

        O Offline
        O Offline
        ODБOï
        wrote on 12 Feb 2020, 10:51 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 J.Hilk
          12 Feb 2020, 10:38

          @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 13 Feb 2020, 04:54 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.

          P 1 Reply Last reply 13 Feb 2020, 05:04
          0
          • S sush123
            13 Feb 2020, 04:54

            @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.

            P Offline
            P Offline
            Pradeep P N
            wrote on 13 Feb 2020, 05:04 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 13 Feb 2020, 07:01
            3
            • P Pradeep P N
              13 Feb 2020, 05:04

              @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 13 Feb 2020, 07:01 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")
              }

              P 2 Replies Last reply 13 Feb 2020, 07:12
              0
              • S sush123
                13 Feb 2020, 07:01

                @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")
                }

                P Offline
                P Offline
                Pradeep P N
                wrote on 13 Feb 2020, 07:12 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
                  13 Feb 2020, 07:01

                  @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")
                  }

                  P Offline
                  P Offline
                  Pradeep P N
                  wrote on 13 Feb 2020, 07:24 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 13 Feb 2020, 07:43
                  4
                  • P Pradeep P N
                    13 Feb 2020, 07:24

                    @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 13 Feb 2020, 07:43 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

                    P 1 Reply Last reply 14 Feb 2020, 13:27
                    3
                    • S sush123
                      13 Feb 2020, 07:43

                      @Pradeep-P-N

                      Thanks alot i registered using wrong type,

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

                      P Offline
                      P Offline
                      Pradeep P N
                      wrote on 14 Feb 2020, 13:27 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

                      9/10

                      13 Feb 2020, 07:43

                      • Login

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