[PyKDE] embedding python widgets in C++ app

Patrick Stinson patrickkidd.lists at gmail.com
Mon Jan 22 09:21:07 GMT 2007


oops, I think the title is backwards. I will send the author an email.
I am trying to embed a python widget in a C++ application, where he is
trying to access his C++ app code from his python code.

I decided to manually import a module with a factory function, call
that factory function from C++ and convert the python object to a
QWidget * using sipConvertToInstance. After doing this, I have a few
questions:

What should the transfer object be as passed to sipConvertToInstance?
I'm a little confused about the wording in the documentation, and
sipTransferObj is automatically included if you are using this in a
sip file.

How are you supposed to statically link the PyQt libraries if you then
lose the PyQt4 package, which initQtGui() expects to import QtCore
from? initQtCore() works fine but initQtGui() fails looking for
PyQt4.QtGui.

Any comments on the following code?

  char nameBuf[2048];
  strcpy(nameBuf, moduleName);
  PyObject *module = PyImport_ImportModule(nameBuf);
  if(module)
    {
      PyObject *_create = PyObject_GetAttrString(module, "_create");
      if(_create)
        {
          if(PyCallable_Check(_create))
            {
              PyObject *sipPy = PyObject_Call(_create,
Py_BuildValue("()"), NULL);
              if(sipPy)
                {
                  int iserr = 0;
                  QWidget *widget = reinterpret_cast<QWidget
*>(sipConvertToInstance(sipPy, sipClass_QWidget, NULL, SIP\
_NO_CONVERTORS, 0, &iserr));
                  if(iserr == 0 && widget)
                    {
                      widget->show();
                      qDebug("GOT WIDGET %p", widget);
                      ret = widget;
                    }
                  else
                    qWarning("could not sip => widget");
                }
              else
                qWarning("error in _create return (%p)", sipPy);
            }
          else
            qWarning("error \"_create\" is not callable");
          Py_DECREF(_create);
        }
      else
        qWarning("no attribute \"_create\"");
    }
  else
    qWarning("no module \"pktest\"");


cheers


On 1/20/07, Patrick Stinson <patrickkidd.lists at gmail.com> wrote:
> Bingo. thanks for that, I think that turtorial will help quite a bit.
>
> On Mon, 15 Jan 2007 07:51:28 -0800
> "Patrick Stinson" <patrickkidd at gmail.com> wrote:
>
> > I know I've seen info here and there about using python widgets in C++ apps,
> > but I need a recap.
> >
> > So I've already got this great C++ app with an embedded interpreter for
> > scripting special app functions. What do I need to do to write a widget in
> > python and use it as a child in my C++ app?
> >
>
> http://wiki.python.org/moin/EmbedingPyQtTutorial
>
> Gerard
>
> On 1/19/07, Andreas Pakulat <apaku at gmx.de> wrote:
> > On 19.01.07 21:41:34, Patrick Stinson wrote:
> > > oh, right. I noticed a bit later that I sent it from an unregistered
> > > adress, so I never got the replies. I wasn't sure if my message made
> > > it to the list either..
> > >
> > > would you mind copying the replies to this message? maybe it's already
> > > in the archive...
> >
> > http://mats.imk.fraunhofer.de/pipermail/pykde/2007-January/015178.html
> >
> > And following mails.
> >
> > Andreas
> >
> > --
> > You need more time; and you probably always will.
> >
> > _______________________________________________
> > PyKDE mailing list    PyKDE at mats.imk.fraunhofer.de
> > http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
> >
>
>
> --
> Patrick Kidd Stinson
> http://www.patrickkidd.com/
> http://pkaudio.sourceforge.net/
> http://pksampler.sourceforge.net/
>


-- 
Patrick Kidd Stinson
http://www.patrickkidd.com/
http://pkaudio.sourceforge.net/
http://pksampler.sourceforge.net/




More information about the PyQt mailing list