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

  1. Building Hybrid Systems With Boost Python, by Dave Abrahams and Ralf W. Grosse-Kunstleve
  2. https://wiki.qt.io/Qt_for_Python/Shiboken
  3. https://doc.qt.io/qtforpython/shiboken2/contents.html
  4. https://www.qt.io/blog/2018/05/31/write-python-bindings
  5. https://code.qt.io/cgit/pyside/pyside-setup.git/tree/examples/scriptableapplication

Qt Python Scriptable Application的更多相关文章

  1. 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 ...

  2. 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 ...

  3. How to export Excel files in a Python/Django application

    https://assist-software.net/blog/how-export-excel-files-python-django-application CONTENTS Introduct ...

  4. 如何用Qt Python创建简单的桌面条形码应用

    Qt for Python可以快速跨平台的GUI应用.这篇文章分享下如何结合Dynamsoft Barcode Reader SDK来创建一个简单的读码应用. 安装Qt for Python 官方站点 ...

  5. Qt+Python开发百度图片下载器

    一.资源下载地址 https://www.aliyundrive.com/s/jBU2wBS8poH 本项目路径:项目->收费->百度图片下载器(可试用5分钟) 安装包直接下载地址:htt ...

  6. 【QT】qt python install pip

    https://pip.pypa.io/en/stable/installing/ http://www.runoob.com/w3cnote/python-pip-install-usage.htm ...

  7. 史上最全的Python电子书教程资源下载(转)

    网上搜集的,点击即可下载,希望提供给有需要的人^_^   O'Reilly.Python.And.XML.pdf 2.02 MB   OReilly - Programming Python 2nd. ...

  8. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  9. Awesome Python

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

随机推荐

  1. LGV - 求多条不相交路径的方案数

    推荐博客 :https://blog.csdn.net/qq_25576697/article/details/81138213 链接:https://www.nowcoder.com/acm/con ...

  2. dp-最大递增子段和

      Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. M ...

  3. JS ES6补充

    补充点:1.let const 2.字符串模板 3.箭头函数  4.对象的单体模式 5.面向对象 一.定义变量 A.var 特点: 1.定义全局变量 2.可以重复定义 3.变量名提升 <!DOC ...

  4. 如何在匿名thread子类中保证线程安全

    在做性能测试的过程中,我写了两个虚拟类ThreadLimitTimeCount和ThreadLimitTimesCount做框架,通过对线程的标记来完成超时请求的记录.旧方法如下: @Override ...

  5. python函数和lambda表达式学习笔记

    1. python函数 不同于其他语言,python支持函数返回多个值 为函数提供说明文档:help(函数名)或者函数名.__doc__ def str_max(str1, str2): ''' 比较 ...

  6. GP工作室—团队项目总结

    GP工作室-团队项目总结 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/ 这个作业要求在哪里 ...

  7. Day6-Python3基础-面向对象编程

    面向过程 VS 面向对象 编程范式 编程是 程序 员 用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程 , 一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大 ...

  8. Python3-提高效率的方法

    1.字符串格式化 Python3.7或以上推荐使用f-string,其他版本推荐使用format方法. 2.字典的初始化 使用字面量初始化字典(以及其他集合类型). 说明:Python中初始化集合类型 ...

  9. 工具之awk

    转自:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html awk是一个强大的文本分析工具,相对于grep的查找,sed的编 ...

  10. linux容器技术之chroot

    linux容器技术之chroot linux chroot 机制的由来 root 用户启动一个daemon,必须用root 用户来启动,比如一个web 服务器(nginx/apapce 80端口)是在 ...