Felix
Felix

Reputation: 3581

Is it possible to embed C++ widget to PyQt application?

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?

Upvotes: 5

Views: 5871

Answers (3)

Mhadhbi issam
Mhadhbi issam

Reputation: 420

i had spent a lot of time dealing with the missing sipconfig module and pyqt5 is build with sip4 despite is cheaped for higher python version where you can not install it , the conclusion i come to draw is forget about sip , and focus on on this : https://doc.qt.io/archives/qtforpython-5.12/shiboken2/samplebinding.html , consider this example also : https://www.basyskom.de/en/using-shiboken2-to-create-python-bindings-for-a-qt-library/

Upvotes: 0

eyllanesc
eyllanesc

Reputation: 243907

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:

enter image description here

Upvotes: 13

user3419537
user3419537

Reputation: 5000

You can use SIP to create bindings for your C++ code the same way that the PyQt bindings are made.

However, the bindings generated by SIP are themselves C++ code, and in the case of PyQt link directly into the Qt binaries. If you just intend to rewrite PyQt code as C++ then any speedup realised will be negligible, as very little native python is executed in the first place.

Upvotes: 3

Related Questions