Is it possible to embed C++ widget to PyQt application?(是否可以将 C++ 小部件嵌入到 PyQt 应用程序中?)
问题描述
我有一个 PyQt5 应用程序和大多数用 Python 编写的小部件.我想在 C++ Qt 中编写一些小部件以使其更快,然后将其嵌入到我的 PyQt QMainWindow 中.
有可能吗?
您可以使用 SIP 来执行从 python 中用 C++ 创建的小部件,在以下
I have a PyQt5 application and most widgets written in Python. I want to write some widget in C++ Qt to make it faster and then embed it into my PyQt QMainWindow.
Is it possible?
You can use SIP to be able to execute a widget created in C ++ from python, in the following link I show an example of how to do it.
The structure of the example is as follows:
├── configure.py
├── sip
│ ├── AnalogClock.sip
│ └── PyAnalogClock.sip
└── src
├── analogclock.cpp
├── analogclock.h
├── analogclockl_global.h
└── AnalogClock.pro
In the src folder you must create the widget library
In the sip folder you must place the structure of the class that you will expose:
AnalogClock.sip
%Import QtGui/QtGuimod.sip
%Import QtWidgets/QtWidgetsmod.sip
%If (Qt_5_0_0 -)
class AnalogClock : public QWidget{
%TypeHeaderCode
#include "analogclock.h"
%End
public:
AnalogClock(QWidget *parent /TransferThis/ = 0);
protected:
virtual void resizeEvent(QResizeEvent *);
virtual void paintEvent(QPaintEvent *e);
};
%End
PyAnalogClock.sip
%Module(name=PyAnalogClock, call_super_init=True, keyword_arguments="Optional")
%DefaultMetatype PyQt5.QtCore.pyqtWrapperType
%DefaultSupertype sip.simplewrapper
%Include AnalogClock.sip
configure.py
is the script that configures the compilation of the project, if you have any problems you must modify some path (it has been tested in Linux)
It is then compiled by executing the following:
python configure.py
make
sudo make install
When executing the previous one it generates a folder called modules, inside of it is the dynamic library, in the case of the example PyAnalogClock.so
, this file we place it in the folder of the source code:
.
├── main.py
└── PyAnalogClock.so
main.py
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyAnalogClock import AnalogClock
if __name__=="__main__":
import sys
a=QApplication(sys.argv)
w=AnalogClock()
w.show()
sys.exit(a.exec_())
output:
这篇关于是否可以将 C++ 小部件嵌入到 PyQt 应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以将 C++ 小部件嵌入到 PyQt 应用程序中?


基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01