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. DP-01背包 (题)

    nyoj 325   http://acm.nyist.net/JudgeOnline/problem.php?pid=325 zb的生日 时间限制:3000 ms  |  内存限制:65535 KB ...

  2. github 关掉邮件通知

  3. MyBatis.多条件排序

    <if test="orderBy != null"> order by <choose> <when test='orderBy=="ag ...

  4. restframework 序列化补充(自定义ModelSerializerl)

    一.知识点 1.source title = serializers.CharField(source='courses.title') source用于one2one.foreginkey.choi ...

  5. [洛谷P1606] [USACO07FEB] 荷叶塘Lilypad Pond

    Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...

  6. java面试| 精选基础题(3)

    每天进步一点点,距离大腿又近一步! 阅读本文大概需要6分钟 系列文章 java面试| 精选基础题(1) java面试|精选基础题(2) 1.float f=3.4;是否正确? 答:不正确,编译无法通过 ...

  7. robotframework,移动端(小程序)自动化,获取元素属性值的方法

    如下图,获取商品价格 属性值显示在content-desc内 传统的get text指定是无法获得到这个元素指定属性的值的 只有通过使用AppiumLibrary.get element attrib ...

  8. Mybatis基础(一)

    mybatis概述: MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...

  9. CSS-17-页面布局

    页面布局: 静态布局: 静态布局:元素不变的布局. 布局特点:缩小后内容被遮挡,拖动滚动条显示布局 设计方法: PC:居中布局,所有样式使用绝对宽度和高度 移动设备:另外建立移动网站,以m.域名为域名 ...

  10. ThinkPHP 5.0.7 + MySQL 构建RESTful API的小程序---02-ThinkPHP5中的orm的模型关联

    ThinkPHP5.0中的操作ORM的一对一,一对多,多对多的操作: 由以下表举例: banner表的设计 id name description delete_time update_time 1 ...