export qt object to javascript in webkit

--添加object到javascript对象中
  webView->page()->mainFrame()->addToJavaScriptWindowObject("formExtractor", this);
        第一个参数是javascript中的名字,第二个参数是对象
  注:必须将上述的添加连接到javaScriptWindowObjectCleared信号,确保每次新页面也会有效
  connect(webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
            this, SLOT(addJavaScriptWindowObjectMethod()));

--javascript访问属性
  cpp中:
      Q_PROPERTY(float blue READ lastBlue);
      float lastBlue();
  javascript中:
      obj.blue

--javascript调用object方法
  cpp中:object方法声明为public slots或者public并使用Q_INVOKABLE来修饰 (signal可以直接调用吗?没有尝试)
  javascript中:使用name.method()调用

--javascript连接object signal
  cpp中:
     public signals:
           void nameChanged();
           void progressChanged(int completed, int total);
  javascript中:
     function onNameChanged() {
     }
     function progress(int complete, int total) {
     }
     function connectSlot() {
         obj.nameChanged.connect(this, onNameChanged);  //disconnect
         obj.progressChanged.connect(this, progress);
     }

--参数的传递:
  函数调用中参数传递支持最基本的类型,复杂类型可以通过QVariant和QVariantMap来包装

  ===使用QVariantMap包装一个结构,javascript中则是Joson来对应

static void unpack(QVariantMap &map, MyStruct *st)
{   
#define Retrieve(name, type, convert, def) \
    if (map.end() != map.find(#name)) \ 
    { \
        st->name = (type)map[#name].convert(); \
    } \
    else \ 
    { \ 
        st->name = def; \
    } 
    Retrieve(name, QString,  toString, "");
    Retrieve(age, int,  toInt, 0);
}
static void pack(QVariantMap &map, MyStruct *st)              
{
    map["name"] = st->name;                  
    map["age"] = st->age;                           
}

  ===使用QVariant包装一个void *指针

static void* unpackPointer(QVariant &var)
{
    long add = var.toLongLong();
    void *p = (void*)add;
    return p;
}
static void packPointer(QVariant &var, void*p)
{
    long add = (int)p;
    var.setValue(add);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To batch-export files to JSON Object Scene format, you can use a script or a dedicated software tool. Here is an example of how to do it using a script in Python: 1. First, make sure you have Python installed on your computer. 2. Install the required Python modules: three.js and argparse. You can do this using pip, the Python package installer, by running the following commands: ``` pip install three pip install argparse ``` 3. Create a Python script that reads in a directory of files in a specific format (e.g. OBJ) and converts them to JSON Object Scene format using the three.js module. Here is an example script: ```python import os import argparse import three def convert_file(file_path): # Load the file using the three.js OBJLoader loader = three.OBJLoader() mesh = loader.load(file_path) # Convert the mesh to a JSON Object Scene json_data = three.exporters.OBJExporter().parse(mesh) json_string = three.utils.stringify(json_data) # Write the JSON data to a file json_file_path = file_path.replace('.obj', '.json') with open(json_file_path, 'w') as f: f.write(json_string) print('Converted {} to {}'.format(file_path, json_file_path)) def convert_files(directory): for file_name in os.listdir(directory): if file_name.endswith('.obj'): file_path = os.path.join(directory, file_name) convert_file(file_path) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Batch-convert OBJ files to JSON Object Scene format.') parser.add_argument('directory', type=str, help='The directory containing the OBJ files.') args = parser.parse_args() convert_files(args.directory) ``` 4. Run the script in the command line, passing in the directory containing your files as an argument. For example: ``` python convert.py /path/to/files ``` This will convert all OBJ files in the specified directory to JSON Object Scene format and save them in the same directory with a .json extension. Note that this is just an example script, and you may need to modify it to fit your specific needs. Additionally, there are also dedicated software tools available that can batch-convert files to JSON Object Scene format, such as the Three.js Editor.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值