Qt:将任何 C++ 对象暴露给 Javascript

Qt: Expose any C++ object to Javascript

我想公开一个 C++ 对象从我自己的 class 到 Javascript。

一个例子可能会说明我到底想达到什么目的。在一个特定的项目中,我正在尝试编写一些简单的 ShopApp。

那么我怎样才能使这样的事情成为可能(其中 mySqlObj 是我的 sqlfunctions-object):

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

我有一个叫 sqlfunctions 的 class。 sqlfunctions.h 看起来像这样

#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

我的 main.cpp 目前看起来像下面这样:

#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();
}

我已经考虑过 documentation and this thread (and some more like this 一个),但是 SO-thread 似乎已经过时了,因为那个例子没有编译并且在构建 process/directory.[=20 时出现了奇怪的问题=]

由于 sqlfunctions 继承自 QObject,您可以将 sqlfunctions* 传递给需要 QObject* 的函数:

frame->addToJavaScriptWindowObject(objJavascriptName, &obj);