Donnerstag, 19. Juni 2008

The benefits of dynamic binding using the Qt meta object system

Here you can see the old way to write a function which can be used from Python:

PyObject *scribus_opendoc(PyObject* /* self */, PyObject* args)
{
char *Name;
if (!PyArg_ParseTuple(args, "es", "utf-8", &Name))
return NULL;
bool ret = ScCore->primaryMainWindow()->loadDoc(QString::fromUtf8(Name));
if (!ret)
{
PyErr_SetString(ScribusException, QObject::tr("Failed to open document.","python error").toLocal8Bit().constData());
return NULL;
}
return PyBool_FromLong(static_cast(true));
}



And now the new way:

bool ScripterNGImpl::openDocument(const QString & filename)
{
bool ret = ScCore->primaryMainWindow()->loadDoc(filename);
if (!ret)
{
RAISE("Cannot open " + filename);
return NULL;
}
return ret;
}


That's all :-)

You only have to make sure the method is defined as a slot or as invokeable. Parsing, checking and converting parameters is not needed anymore. For a more complex method you will save a lot of overhead.


Less code is easier to understand and to modify.
So I hope this will motivate others to contribute to this plug-in.

Keine Kommentare: