Qt Python Scriptable Application
Qt Python Scriptable Application
eryar@163.com
Abstract. Python and C++ are in many ways as different as two languages could be: while C++ is usually compiled to machine-code, Python is interpreted. Python's dynamic type system is often cited as the foundation of its flexibility, while in C++ static typing is the cornerstone of its efficiency. C++ has an intricate and difficult compile-time meta-language, while in Python, practically everything happens at runtime.
Key Words. Qt, Python, Shiboken2, PySide2
1 Introduction
Python和C++是两种不同的开发语言:C++通常编译成机器码,而Python是解释型的。Python的动态类型系统是它灵活性的基础,而C++的静态类型运行效率高。对于许多程序员来说,让Python和C++互为补充来进行程序开发是很完美的。Python程序的性能瓶颈可以用C++来重写;C++程序选择Python作为中间脚本语言,可以为程序提供方便地集成和扩展能力,即支持程序二次开发,方便为程序定制功能。
2 Shiboken2
Shiboken2是一个为C/C++库生成CPython绑定代码的生成器,用来创建PySide的模块,换句话说,Shiboken可以用来暴露Qt C++API给Python。
Shiboken使用ApiExtractor库来分析所有的Qt头文件。由于使用的clang,不依赖Qt,所以Shiboken也可用于将非Qt的C++项目。并且Shiboken有一个类型系统Typesytem,基于XML文件。
编译PySide的前提条件就是先要编译出Shiboken。当成功编译PySide后,就会生成Shiboken等类库。既然使用Shiboken可以将Qt的C++对象暴露给Python,那么如何将C++和Python结合起来,生成一个支持Python脚本的程序呢?
在PySide2的例子文件夹中提供了两个例子:
l SampleBinding:暴露非Qt的C++对象给Python;
l ScriptableApplication:支持脚本的程序示例;
如何编译这两个例子,在其中的ReadMe.md中都有详细说明,主要是配置好PySide2,Shiboken2等环境。其中ScriptableApplication中提供了CMake和QMake两种编译方式,将他们生成VS的工程可以看出,在VS项目中增加了XML文件,并将其设置成Custom Build Tool:
并为其配置Shiboken的一些命令选项:
3 ScriptableApplication Example
编译成功程序运行截图如下所示:
程序支持了Python脚本,当然也可以使用PySide来开发GUI,如下所示:
4 Conclusion
为C++程序提供Python脚本支持有很多开源库可以实现,如boost.python, pycxx等,而Qt 的PySide2使用Shiboken2来将Qt的C++类型暴露给Python,所以也可以直接使用Shiboken2来将C++类封装成Python类。
5 References
- Building Hybrid Systems With Boost Python, by Dave Abrahams and Ralf W. Grosse-Kunstleve
- https://wiki.qt.io/Qt_for_Python/Shiboken
- https://doc.qt.io/qtforpython/shiboken2/contents.html
- https://www.qt.io/blog/2018/05/31/write-python-bindings
- https://code.qt.io/cgit/pyside/pyside-setup.git/tree/examples/scriptableapplication
Qt Python Scriptable Application的更多相关文章
- Create a Qt Widget Based Application—Windows
This turtorial describes how to use Qt Creator to create a small Qt application, Text Finder. It is ...
- How to create PDF files in a Python/Django application using ReportLab
https://assist-software.net/blog/how-create-pdf-files-python-django-application-using-reportlab CONT ...
- How to export Excel files in a Python/Django application
https://assist-software.net/blog/how-export-excel-files-python-django-application CONTENTS Introduct ...
- 如何用Qt Python创建简单的桌面条形码应用
Qt for Python可以快速跨平台的GUI应用.这篇文章分享下如何结合Dynamsoft Barcode Reader SDK来创建一个简单的读码应用. 安装Qt for Python 官方站点 ...
- Qt+Python开发百度图片下载器
一.资源下载地址 https://www.aliyundrive.com/s/jBU2wBS8poH 本项目路径:项目->收费->百度图片下载器(可试用5分钟) 安装包直接下载地址:htt ...
- 【QT】qt python install pip
https://pip.pypa.io/en/stable/installing/ http://www.runoob.com/w3cnote/python-pip-install-usage.htm ...
- 史上最全的Python电子书教程资源下载(转)
网上搜集的,点击即可下载,希望提供给有需要的人^_^ O'Reilly.Python.And.XML.pdf 2.02 MB OReilly - Programming Python 2nd. ...
- Python框架、库以及软件资源汇总
转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...
- Awesome Python
Awesome Python A curated list of awesome Python frameworks, libraries, software and resources. Insp ...
随机推荐
- C++ 中的 unique 函数
unique 函数是用来去除一个集合中重复元素的函数 若是在数组中,则调用此函数后,返回的除去重复元素的下一个指针的地方 若是在 vector中,则会返回重复元素下一个位置的迭代器,在调用erase函 ...
- Eclipse常用快捷键【转】
Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键.1. [ALT+/]此快捷键为用户编辑的好帮手,能为用户提供内容的 ...
- nrm npm nvm
1.nvm: node version manager node版本管理器 可以来回切换node.js版本号,而直接使用node的.msi安装则版本比较固定,无法实现node版本的自行切换nvm安装方 ...
- “Your build settings specify a provisioning profile with the UUID “”, however, no such provisioning profile was found”
解决方法: 终端命令行输入下面语句,删除所有的Profilescd ~/Library/MobileDevice/Provisioning\ Profiles/rm *.mobileprovision
- idea实现svn拉分支和合并分支的教程
原文地址:https://blog.csdn.net/qq_27471405/article/details/78498260 今天测试了一下svn拉分支和合并分支的教程,决定分享给大家 拉分支教程: ...
- CSS-12-盒子模型
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 笔记常用Linux命令(二) 进程和端口
查看系统进程 ps:用于报告当前系统的进程状态 a:显示所有终端机下执行的程序 ps -ef/ps aux: 这两个命令都是查看当前系统正在运行进程,两者的区别是展示格式不同. 如果想要查看特定的进程 ...
- 安装Eclipse activity插件 报异常 Cannot complete the install because one or more required items could not be
下载插件:Activiti Designer 5.17 2)安装过程中错误处理 a.错误: Cannot complete the install because one or more requir ...
- axure如何实现提示框3s后自动消失
本示例基于axure8 实现 1.先做两个元件,一个按钮,一个提示框 2.将弹框“发布成功提示”设置为,页面载入时隐藏,这样预览页面时,该弹框是隐藏状态 3.给按钮添加交互样式,如下: 4.预览,点击 ...
- java线程池及创建多少线程合适
java线程池 1.以下是ThreadPoolExecutor参数完备构造方法: public ThreadPoolExecutor(int corePoolSize,int maximumPoolS ...