Important: Please read the 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

    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

  • @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
    it doesnot work where i am wrong.

  • @sush123

    You already have the property so you can use it directly in your function without" "

    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); 


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


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


    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)



    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: {
    onButtonLongPress: {

    When i use like above i dont get click event

    if i use directly strings like below it will take click event
    onButtonClick: {
    onButtonLongPress: {

  • @sush123 said in Usage of QString in QML:


    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:


        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")
            onPressAndHold: {
                console.log("Inside QML Mouse Press&Hold")

    Output :


    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