Important: Please read the Qt Code of Conduct -

how to call qml function and get back value from it using C++

  • Hey guys i want to get the HTML from this file ( Mywebview.qml ) to my C++ (dbmanager.cpp )code

    what i want to do is : when a user clicks on save page button it would call parsing() from qml file and send the result to C++ code . i want to call it in add() function . Is it possible to do that ?

    "in short : i want to call a qml function , function executes return some value . I want that value in C++ , so that i can save it in a file( html) "

    Please look at the code here (FULL CODE ) :

    Pasting here all the code will look like a mess , so i am just posting Mywebview.qml code and dbmanager.cpp


    #include "dbmanager.h"
    #include <QtSql>
    #include <QSqlDatabase>
    #include <QSqlDriver>
    dbmanager::dbmanager(QObject *parent) : QObject(parent)
    void dbmanager::add()
        QDir databasePath;
        QString path = databasePath.currentPath()+"WTL.db";
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");//not dbConnection
            qDebug() <<"error in opening DB";
            qDebug() <<"connected to DB" ;
        QSqlQuery query;
        if( query.exec(""))
            qDebug() << "PAGE ADDED successfully";
         qDebug() <<query.lastError();
    void dbmanager::del()
        qDebug() <<"DELETION CODE GOES HERE";


    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtWebEngine 1.1
    import QtWebKit 3.0
    Window {
        visible: true
        width: 600
        height: 400
        function parsing() {
            var http = new XMLHttpRequest();
            var json , parse , text , rev_id;
            http.onreadystatechange = function(){
                if(http.readyState == 4 && http.status == 200)
                { json = http.responseText;
                    parse = JSON.parse(json);
                    rev_id = parse.parse.revid;
                    text = parse.parse.text["*"];
                     // <-- STRIP ME (o.O)
                    text = text.replace(/&#39;\/index.php/, "");
                    text = text.replace(/&amp;/,"&");
                    text = text.replace(/MathShowImage&amp;/, "MathShowImage&")
                    text = text.replace(/mode=mathml&#39;/, "mode=mathml\"");
                    text = text.replace(/<meta class="mwe-math-fallback-image-inline" aria-hidden="true" style="background-image: url\(/ ,"<img style=\"background-repeat: no-repeat; background-size: 100% 100%; vertical-align: -0.838ex;height: 2.843ex; \" src=\"");
                    text = text.replace(/<meta class="mwe-math-fallback-image-display" aria-hidden="true" style="background-image: url\(/ ,"<img style=\"background-repeat: no-repeat; background-size: 100% 100%; vertical-align: -0.838ex;height: 2.843ex; \" src=\"");
                    text = text.replace(/&amp;mode=mathml\"/ , "&mode=mathml>\"");
                    console.log(text); // after strip :p .
            id: webEngineView
            anchors.fill: parent
        Component.onCompleted: parsing()

  • I took the information from this stackoverflow thread and modified it to fit your needs. I can't guarantee that it'll work since i didn't tried, but i still want to provide you the opportunity to try it yourself:

    • Make your function invokeable:
    class DbManager {
        Q_INVOKABLE void add();
    • and then you can call this somewhere in your code. Afaik it has to be executed before you execute or start your QML, so main.cpp would be a shot.
    qmlRegisterType<DbManager >("com.myproject", 1, 0, "DbManager ");
    • and in QML Side you can do like that:
    import com.myproject 1.0
    DBManager {
         id: dbmanager
    function parsing() {

    You can also add Parameters and pass them like you call a function. Return values should be no problem too.

    There might be another option, but i haven't worked with qml for some time so im not sure if its possible:

    • Create a signal in QML
    signal addMyStuff()
    • Connect the Signal to a c++ function
    QQuickView view;
    QObject *object = view.rootObject();
    QObject::connect(object, SIGNAL(addMyStuff()), myDatabaseObject, SLOT(add()));

Log in to reply