Qt:将任何C ++对象暴露给Javascript [英] Qt: Expose any C++ object to Javascript

查看:144
本文介绍了Qt:将任何C ++对象暴露给Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个C ++对象从我自己的类暴露给Javascript。

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

一个例子可能会澄清,我究竟要实现什么。在一个特定的项目中,我试图编写一些简单的ShopApp。

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

那么我如何做这样的可能( mySqlObj 是我的 sqlfunctions - 对象):

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

我有一个类 sqlfunctions sqlfunctions.h 看起来像这样

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

我的 main .cpp 目前类似于以下内容:

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

我考虑过文档和此线程(以及一些更像这里这里),但是SO线程似乎已过时,因为该示例不编译和奇怪的建设过程/目录的麻烦。 / p>

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.

推荐答案

由于 sqlfunctions 继承自 QObject ,您可以将 sqlfunctions * 传递到期望 QObject * 的函数:

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

frame->addToJavaScriptWindowObject(objJavascriptName, &obj);

这篇关于Qt:将任何C ++对象暴露给Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆