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

How to control onClicked in qml with C ++ code ?



  • i want to control onClicked in qml with c++ code, without touch the display.
    but, i don't know how.
    please tell me....

    ex)
    int main() {
    ...
    callclick();
    ...
    }

    if i do this, call onClick in QML code.


  • Moderators

    hi @LeeK and welcome

    first of, you do not provide enough information, for any good help.

    Let me ask this, do you know how to send signals from a c++ class to your qml file?
    If not, you may want to read through this first.
    https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html



  • hi @J-Hilk .
    First, thanks for the answers.
    I will provide additional information.

    my sample qml code :

    Rectangle {
         id: rectsampleid
         height: 500
         width: 500
         visible: true
    
        function myQmlFunction(msg) {
            console.log("Got message:", msg)
            return "some return value"
        }
    
        MouseArea {
            anchors.fill: parent
            onClicked: {
                Qt.quit();
            }
        }
    
        Text {
            text: qsTr("Hello World")
            anchors.centerIn: parent
        }
    }
    

    and my sample main.cpp :

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlComponent>
    #include <QDebug>
    #define QARG(object,returnedValue, msg) QMetaObject::invokeMethod(object, "myQmlFunction", Q_RETURN_ARG(QVariant, returnedValue), Q_ARG(QVariant, msg))
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        QQmlComponent component(&engine, QUrl(QLatin1String("qrc:/main.qml")));
        //engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        QObject *mainPage = component.create();
        QObject* item = mainPage->findChild<QObject *>("buttonTest");
    
        QVariant returnedValue;
        QVariant msg = "Hello from C++";
        QARG(mainPage, returnedValue, msg);
        qDebug() << "QML function returned:" << returnedValue.toString();
    
        return app.exec();
    }
    

    The code is roughly the sample code.
    i want to execute the onClicked in the MouseArea in the QML without touch to display.

    I want to do any method call that gave the signal.
    Otherwise, i want to control the Qt sublayer to achieve the desired result.
    but, i don't know how.....
    And i have already seen the tutorial website you provided, but I couldn't get the information I needed.

    I keep searching and studying, but I can't think of any way.
    Please help me...


  • Moderators

    @LeeK said in How to control onClicked in qml with C ++ code ?:

    hi @J-Hilk .
    First, thanks for the answers.
    I will provide additional information.

    thanks

    And i have already seen the tutorial website you provided, but I couldn't get the information I needed.

    I doubt that, there is exactly an example of what you need:
    https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods

    and here's a working program for you:

    //main.cpp
    #include <QApplication>
    #include <QQmlApplicationEngine>
    
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        QObject *rootElement = engine.rootObjects().first();
        QVariant returnedValue;
        QVariant msg = "Hello from C++";
        QMetaObject::invokeMethod(rootElement, "myQmlFunction",
                Q_RETURN_ARG(QVariant, returnedValue),
                Q_ARG(QVariant, msg));
    
        qDebug() << "QML function returned:" << returnedValue.toString();
    
        return app.exec();
    }
    
    //main.qml
    import QtQuick 2.11
    import QtQuick.Window 2.2
    
    Window
    {
        id:rootWindow
        width: 400
        height: 250
    
        visible:  true
    
        function myQmlFunction(msg) {
                console.log("Got message:", msg)
                return "some return value"
            }
    }
    


  • okay, i know.
    The tutorial you provided is what I saw in the past, and I wrote the sample code through it.
    However, what I want to ask is whether i can control "onClicked" in qml with C ++ code based on that sample code, not that the sample code is wrong.


  • Moderators

    @LeeK ah, I got you now ;-)

    The answer is yes and no

    you can't directly access the onClicked function, but you could create a standalone function that is called/executed by onClicked and can also be called from c++

    Or you could potentially emit the clicked signal manually, I'm unsure about that as I haven' tried that one yet.



  • @J-Hilk okay.
    "you can't directly access the onClicked function."
    maybe, this is answer for my questions.
    thanks for the replies.


Log in to reply