Results 1 to 2 of 2

Thread: Embedding PyQt4 into an Application running Python via Boost::Python

  1. #1
    Join Date
    Apr 2010
    Posts
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Embedding PyQt4 into an Application running Python via Boost::Python

    Hi,

    first of all, i hope i chose the right forum, but it thought i am having "installation" problems, so i might be right here.

    I'm developing an Application (which is Qt based itself) .. with Python integration using boost:ython.

    At first i want to mention that i don't have any problems running PyQt4 code using my native python interpreter /somewhere/Python26/Python.exe

    Now i try to have my script sourrounding understand pyqt4 so scripts can take usage of it. Unfortunatly i am getting

    ImportError: DLL load failed: The specified procedure could not be found.
    errors when i try to run python code like

    Qt Code:
    1. from PyQt4 import QtGui
    To copy to clipboard, switch view to plain text mode 
    .

    Qt Code:
    1. import PyQt4
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. from PyQt4 import *
    To copy to clipboard, switch view to plain text mode 
    will pass without any errors, but not giving me access to anything inside the Module.

    I spent a few hours on investigation by now and couldn't find anything related so far, the most issues about PyQt + embedding are about sharing objects, not about the installation process itself.

    I guess i miss some setup required for my runtime python interpreter to know where to look for the dlls or something similar.

    My current application folder setup is like this:

    • Application.exe
    • boost_python-vc90-mt-1_41.dll
    • python26.dll
    • QtCore4.dll
    • QtGui4.dll
    • Qt.pyd
    • QtCore.pyd
    • QtGui.pyd
    • TestFile.txt


    I successfully read the TestFile.txt by reading "./TestFile.txt" via Python ... so the base path of the interpreter should find those modules as well?

    I also tried placing all *.pyd files from my PyQt4 installation inside the application folder to be sure its not a problem because the qt system is not complete.

    I've no idea how to get PyQt working inside my application and would be very grateful for any hint that might get me into the right direction.

    Thanks

    Gunnar
    Last edited by GreyHound; 12th April 2010 at 20:50.

  2. #2
    Join Date
    Feb 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Embedding PyQt4 into an Application running Python via Boost::Python

    This is an example how to integrate PyQt4 and boost:ython

    first of all we must define wrap/unwrap function to deal with bare pointers

    Qt Code:
    1. long int unwrap(QObject* ptr) {
    2. return reinterpret_cast<long int>(ptr);
    3. }
    4.  
    5. template <typename T>
    6. T* wrap(long int ptr) {
    7. return reinterpret_cast<T*>(ptr);
    8. }
    To copy to clipboard, switch view to plain text mode 

    after that we must register all classes we want integrate to

    Qt Code:
    1. class_<QObject, QObject*, boost::noncopyable>("QObject", no_init)
    2. .def("unwrap", unwrap)
    3. .def("wrap", make_function( wrap<QObject>, return_value_policy<return_by_value>() ))
    4. .staticmethod("wrap");
    5.  
    6. class_<QWidget, bases<QObject>, QWidget*, boost::noncopyable>("QWidget")
    7. .def("wrap", make_function( wrap<QWidget>, return_value_policy<return_by_value>() ))
    8. .staticmethod("wrap");
    9.  
    10. class_<QFrame, bases<QWidget>, QFrame*, boost::noncopyable>("QFrame")
    11. .def("wrap", make_function( wrap<QFrame>, return_value_policy<return_by_value>() ))
    12. .staticmethod("wrap");
    13.  
    14. class_<QLabel, bases<QFrame>, QLabel*, boost::noncopyable>("QLabel")
    15. .def("wrap", make_function( wrap<QLabel>, return_value_policy<return_by_value>() ))
    16. .staticmethod("wrap");
    To copy to clipboard, switch view to plain text mode 

    and for example we have class that works with.. QLabel:

    Qt Code:
    1. class worker: public QObject {
    2. ...
    3. void add_label(QLabel*);
    4. };
    To copy to clipboard, switch view to plain text mode 

    we must expose this class to python too:
    Qt Code:
    1. class_<worker, bases<QObject>, worker*, boost::noncopyable>("worker")
    2. .def("add_label", &worker::add_label);
    To copy to clipboard, switch view to plain text mode 
    now we a ready to interaction,
    on C++-size do something like this

    Qt Code:
    1. worker* w = new worker;
    2. main_namespace["worker"] = boost::ref(w);
    To copy to clipboard, switch view to plain text mode 

    python:
    Qt Code:
    1. from PyQt4.Qt import *
    2. import sip
    3. import mylib as MyLib
    4.  
    5. #...
    6.  
    7. #If you are using QApplication on C++-size you don't need to create another one
    8.  
    9. lb = QLabel("label from PyQt4!")
    10.  
    11. lb_ptr = sip.unwrapinstance(f)
    12.  
    13. my_lb = MyLib.QLabel.wrap(lb_ptr)
    14.  
    15. worker.add_label(my_lb)
    To copy to clipboard, switch view to plain text mode 

    In other case if you wan't send you own Q-object to PyQt4 :

    Qt Code:
    1. QLabel* lb = new QLabel("C++ label");
    2. main_namespace["lb"] = boost::ref(lb);
    To copy to clipboard, switch view to plain text mode 

    python:
    Qt Code:
    1. from PyQt4.Qt import *
    2. import sip
    3. import mylib as MyLib
    4.  
    5. #...
    6.  
    7. my_lb_ptr = lb.unwrap()
    8.  
    9. qt_lb = sip.wrapinstance(my_lb_ptr, QLabel)
    To copy to clipboard, switch view to plain text mode 


    And this is my real little helper:
    Qt Code:
    1. from PyQt4.Qt import *
    2. import sip
    3.  
    4. def toQt(object, type):
    5. ptr = object.unwrap()
    6. return sip.wrapinstance(ptr, type)
    7.  
    8. def fromQt(object, type):
    9. ptr = sip.unwrapinstance(object)
    10. return type.wrap(ptr)
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. PyQt4 app : high CPU usage from python and Xorg
    By tipote in forum Qt Programming
    Replies: 3
    Last Post: 30th January 2012, 18:50
  2. Embedding python code in Qt
    By lixo1 in forum Qt Programming
    Replies: 2
    Last Post: 12th March 2010, 19:02
  3. Replies: 1
    Last Post: 16th February 2010, 00:20
  4. communication between QT application and python
    By HERC in forum Qt Programming
    Replies: 2
    Last Post: 1st February 2010, 11:47
  5. Objective C, Python and Ruby code with C++ Qt application
    By Berberis in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2008, 13:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.