当前位置: 首页 > news >正文

QtScript模块

在Qt中,可以使用Qt Script模块来将C++类和方法绑定到Qt脚本引擎中,从而使得可以在Qt脚本中调用这些C++类和方法。以下是一个简单的示例,演示了如何在Qt中将C++类暴露给Qt Script引擎:

假设有一个名为 MyClass 的C++类,其头文件 MyClass.h 如下所示:

#ifndef MYCLASS_H
#define MYCLASS_H#include <QObject>class MyClass : public QObject
{Q_OBJECTpublic:MyClass(QObject *parent = nullptr);Q_INVOKABLE void doSomething();
};#endif // MYCLASS_H

接着,在 MyClass.cpp 文件中实现 MyClass 类的方法:

#include "MyClass.h"
#include <QDebug>MyClass::MyClass(QObject *parent) : QObject(parent)
{
}void MyClass::doSomething()
{qDebug() << "Doing something in C++";
}

现在,我们将 MyClass 类暴露给Qt Script引擎。创建一个 ScriptManager 类,用来管理Qt Script引擎,将 MyClass 类注册到引擎中:

#include <QScriptEngine>
#include <QScriptValue>
#include "MyClass.h"class ScriptManager
{
public:ScriptManager(){engine.globalObject().setProperty("MyClass", engine.newQObject(new MyClass()));}void evaluateScript(const QString &script){engine.evaluate(script);}private:QScriptEngine engine;
};

在主程序中,创建 ScriptManager 实例并执行脚本:

#include <QCoreApplication>
#include "ScriptManager.h"int main(int argc, char *argv[])
{QCoreApplication app(argc, argv);ScriptManager scriptManager;scriptManager.evaluateScript("MyClass.doSomething();");return app.exec();
}

通过这样的方式,可以在Qt脚本中调用C++类的方法,并实现C++和Qt脚本之间的交互。

注意工程文件创建中需要添加QT += script:

QT -= gui
QT += scriptCONFIG += c++11 console
CONFIG -= app_bundle# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \myclass.cpp# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetHEADERS += \ScriptManager.h \myclass.h

http://www.lryc.cn/news/376312.html

相关文章:

  • qt中for循环不要使用循环中会更改的变量
  • spark独立集群搭建
  • 【BFS算法】广度搜索·由起点开始逐层向周围扩散求得最短路径(算法框架+题目)
  • 微信小程序---登录
  • IPython大师课:提升数据科学工作效率的终极工具
  • 抖音素材网站平台有哪些?素材下载网站库分享
  • MODBUS TCP协议实例数据帧详细分析
  • Spring Boot启动与运行机制详解:初学者友好版
  • Ubuntu 22.04 解决 firefox 中文界面乱码
  • 前端面试题日常练-day77 【面试题】
  • 团队协同渗透测试报告输入输出平台部署
  • vue3-父子通信
  • 微信小程序—页面滑动,获取可视区域数据
  • C#语言进阶(一)—委托
  • VST3音频插件技术介绍
  • MySQL数据库管理 二
  • android system UI 基础的基础
  • ARM32开发——GD32F4定时器查询
  • 【机器学习】第7章 集成学习(小重点,混之前章节出题但小题)
  • 代码随想录——子集Ⅱ(Leecode 90)
  • vue关闭页面时触发的函数(ai生成)
  • 马尔可夫性质与Q学习在强化学习中的结合
  • 【LeetCode 5.】 最长回文子串
  • 联邦学习周记|第四周
  • 机器学习课程复习——逻辑回归
  • Rocky Linux 更换CN镜像地址
  • Linux rm命令由于要删的文件太多报-bash: /usr/bin/rm:参数列表过长,无法删除的解决办法
  • 【包管理】Node.JS与Ptyhon安装
  • SpringMVC系列四: Rest-优雅的url请求风格
  • Hexo 搭建个人博客(ubuntu20.04)