-2

I'd like to expose a C++ object from my own class to Javascript.

An example might clarify, what exactly I am trying to achieve. In a particular project I am trying to program some simple ShopApp.

So how can i make something like this possible (where mySqlObj is my sqlfunctions-object):

$(document).ready(function(){
    $("#someButtonId").click(function(){
        mySqlObj.refillBalance(1000); // adds 1000 units of money to user's account
        // or
        mySqlObj.listAllPorducts();
    });
});

There I have got a class called sqlfunctions. sqlfunctions.h looks like this

#ifndef SQLFUNCTIONS_H
#define SQLFUNCTIONS_H

#include <iostream>
#include <vector>
#include <algorithm>

#include <QSqlQuery>
#include <QSql>
#include <QSqlDatabase>
#include <QDebug>
#include <QMessageBox>
#include <QObject>
#include <QString>

#include "product.h"

class product;
using namespace std;

typedef vector<product>::iterator iter;

class sqlfunctions:public QObject{
    Q_OBJECT

    public:
        sqlfunctions();

    signals:
        void        purchaseDone(vector<product> cart);
        void        adminLoggedIn();

    public slots:
        // Warenmanagement
        product     isAlreadyInCart(product myProduct);
        void        listAllProducts();
        void        addToCart(product myProduct);
        void        showCart();
        void        clearCart();
        void        changeAmount(product myProduct, string mode);
        void        changeAmount(product myProduct, int diff, string mode);
        int         checkStock();
        double      checkBalance();
        void        purchase();

        // Usermanagement
        void        registerUser(QString username, QString password);
        void        login(QString username, QString password);
        void        empowerUser();
        void        disempowerUser();
        void        listAllUsers();
        void        refillBalance(int amount);

    private:
        // Accountmanagement
        vector<product>     cart;
        bool                isLogin;
        bool                isAdminLoggedIn;
        int                 uid;
        QSqlDatabase        db;
};

#endif // SQLFUNCTIONS_H

My main.cpp currently looks like the following:

#include <QApplication>  
#include <QGraphicsWebView>
#include <QWebFrame>
#include <QtWebKit>

#include "html5applicationviewer.h"  
#include "sqlfunctions.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    sqlfunctions obj;

    Html5ApplicationViewer viewer;
    viewer.setOrientation(Html5ApplicationViewer::ScreenOrientationAuto);
    viewer.showExpanded();
    viewer.loadFile(QLatin1String("src/index.html"));
    viewer.setFixedSize(1200, 900);

    QWebFrame *frame = viewer.webView()->page()->mainFrame();
    QString objJavascriptName = "mySqlObj";
    frame->addToJavaScriptWindowObject(objJavascriptName, &obj);

    return app.exec();
}

I have considered the documentation and this thread (and some more like this one here), but the the SO-thread seems to be outdated, since that example does not compile and strange trouble with the building process/directory.

Community
  • 1
  • 1
Alex
  • 751
  • 1
  • 6
  • 34
  • I suggest you learn C++ before Qt. Not knowing the address-of operator is one thing, but the difference between IsA/HasA relationships and polymorphism are core tenets of the language. – cmannett85 May 11 '15 at 21:45
  • Oh, well i actually know about the adress-of operator, i simply overlooked that asterisk in the signature :) Due to overlooking that asterisk this didn't compile, leaving me irritated. I know that a class inheriting another inherits that class's members. I actually just looked into my C++ book and it didn't expicitly say whether a subclass **is** the baseclass or only **has** the baseclass in it. Thanks for the clarification anyway. I edited the question accordingly. – Alex May 11 '15 at 22:18

1 Answers1

3

Since sqlfunctions inherits from QObject, you can pass a sqlfunctions* to a function expecting a QObject*:

frame->addToJavaScriptWindowObject(objJavascriptName, &obj);
m.s.
  • 16,063
  • 7
  • 53
  • 88
  • Thank you very much, I'll answer my original question tomorrow, after a good night's rest :) – Alex May 11 '15 at 22:25