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