Unsolved 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 ) : https://github.com/hackertron/W2L/tree/sub/storage
Pasting here all the code will look like a mess , so i am just posting Mywebview.qml code and dbmanager.cpp
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 db.setDatabaseName(path); if(!db.open()) { qDebug() <<"error in opening DB"; } else { qDebug() <<"connected to DB" ; } QSqlQuery query; if( query.exec("")) { qDebug() << "PAGE ADDED successfully"; } else { qDebug() <<query.lastError(); } } void dbmanager::del() { qDebug() <<"DELETION CODE GOES HERE"; }
Mywebview.qml
import QtQuick 2.5 import QtQuick.Window 2.2 import QtWebEngine 1.1 import QtWebKit 3.0 Window { id:webview 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; console.log(rev_id); text = parse.parse.text["*"]; //console.log(text); // <-- STRIP ME (o.O) while(text.match(/'\/index.php/)){ text = text.replace(/'\/index.php/, "http://en.wikitolearn.org/index.php"); text = text.replace(/&/,"&"); text = text.replace(/MathShowImage&/, "MathShowImage&") text = text.replace(/mode=mathml'/, "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(/&mode=mathml\"/ , "&mode=mathml>\""); } console.log(text); // after strip :p . webEngineView.loadHtml(text); } }; http.open('GET','http://en.wikitolearn.org/api.php?action=parse&page=Linear%20Algebra/Sets&format=json'); http.send(); } WebView{ 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() { ... dbmanager.add(); }
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()));