Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Usage of QString in QML



  • Hi Everyone,

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


  • Moderators

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



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



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



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



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



  • @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-P-N

    Thanks alot i registered using wrong type,

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



  • @sush123
    You are welcome.

    All the best.


Log in to reply