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












1














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?










share|improve this question





























    1














    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?










    share|improve this question



























      1












      1








      1


      1





      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?










      share|improve this question















      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?







      python c++ pyqt pyqt5






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 4:26









      eyllanesc

      74.4k103156




      74.4k103156










      asked May 30 '17 at 9:45









      Felix

      84141433




      84141433
























          2 Answers
          2






          active

          oldest

          votes


















          2














          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






          share|improve this answer























          • Thank you for your reply. I'm trying to build it, but it fails. python3 configure.py passes, but make step says that it cannot find library MyLabel
            – Felix
            Jun 23 '17 at 19:03










          • I've reloaded the project. Now make step passes, but when I try to run Examples.main.py, it gives an error: ImportError: libMyLabel.so.1: cannot open shared object file: No such file or directory
            – Felix
            Jun 24 '17 at 8:45












          • Great, it works. I'll try to build it under Windows (with nmake).
            – Felix
            Jun 25 '17 at 10:39



















          1














          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.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44258808%2fis-it-possible-to-embed-c-widget-to-pyqt-application%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            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






            share|improve this answer























            • Thank you for your reply. I'm trying to build it, but it fails. python3 configure.py passes, but make step says that it cannot find library MyLabel
              – Felix
              Jun 23 '17 at 19:03










            • I've reloaded the project. Now make step passes, but when I try to run Examples.main.py, it gives an error: ImportError: libMyLabel.so.1: cannot open shared object file: No such file or directory
              – Felix
              Jun 24 '17 at 8:45












            • Great, it works. I'll try to build it under Windows (with nmake).
              – Felix
              Jun 25 '17 at 10:39
















            2














            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






            share|improve this answer























            • Thank you for your reply. I'm trying to build it, but it fails. python3 configure.py passes, but make step says that it cannot find library MyLabel
              – Felix
              Jun 23 '17 at 19:03










            • I've reloaded the project. Now make step passes, but when I try to run Examples.main.py, it gives an error: ImportError: libMyLabel.so.1: cannot open shared object file: No such file or directory
              – Felix
              Jun 24 '17 at 8:45












            • Great, it works. I'll try to build it under Windows (with nmake).
              – Felix
              Jun 25 '17 at 10:39














            2












            2








            2






            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






            share|improve this answer














            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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 23 '17 at 21:04

























            answered May 30 '17 at 11:59









            eyllanesc

            74.4k103156




            74.4k103156












            • Thank you for your reply. I'm trying to build it, but it fails. python3 configure.py passes, but make step says that it cannot find library MyLabel
              – Felix
              Jun 23 '17 at 19:03










            • I've reloaded the project. Now make step passes, but when I try to run Examples.main.py, it gives an error: ImportError: libMyLabel.so.1: cannot open shared object file: No such file or directory
              – Felix
              Jun 24 '17 at 8:45












            • Great, it works. I'll try to build it under Windows (with nmake).
              – Felix
              Jun 25 '17 at 10:39


















            • Thank you for your reply. I'm trying to build it, but it fails. python3 configure.py passes, but make step says that it cannot find library MyLabel
              – Felix
              Jun 23 '17 at 19:03










            • I've reloaded the project. Now make step passes, but when I try to run Examples.main.py, it gives an error: ImportError: libMyLabel.so.1: cannot open shared object file: No such file or directory
              – Felix
              Jun 24 '17 at 8:45












            • Great, it works. I'll try to build it under Windows (with nmake).
              – Felix
              Jun 25 '17 at 10:39
















            Thank you for your reply. I'm trying to build it, but it fails. python3 configure.py passes, but make step says that it cannot find library MyLabel
            – Felix
            Jun 23 '17 at 19:03




            Thank you for your reply. I'm trying to build it, but it fails. python3 configure.py passes, but make step says that it cannot find library MyLabel
            – Felix
            Jun 23 '17 at 19:03












            I've reloaded the project. Now make step passes, but when I try to run Examples.main.py, it gives an error: ImportError: libMyLabel.so.1: cannot open shared object file: No such file or directory
            – Felix
            Jun 24 '17 at 8:45






            I've reloaded the project. Now make step passes, but when I try to run Examples.main.py, it gives an error: ImportError: libMyLabel.so.1: cannot open shared object file: No such file or directory
            – Felix
            Jun 24 '17 at 8:45














            Great, it works. I'll try to build it under Windows (with nmake).
            – Felix
            Jun 25 '17 at 10:39




            Great, it works. I'll try to build it under Windows (with nmake).
            – Felix
            Jun 25 '17 at 10:39













            1














            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.






            share|improve this answer


























              1














              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.






              share|improve this answer
























                1












                1








                1






                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.






                share|improve this answer












                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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered May 30 '17 at 10:19









                user3419537

                2,9371126




                2,9371126






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44258808%2fis-it-possible-to-embed-c-widget-to-pyqt-application%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...