SourceForge.net Logo

Examples

Examples are available in the examples directory. The PyScriptingConsole implements a simple interactive scripting console that shows how to script a simple application. The PyLauncher application can be used to run arbitrary PythonQt scripts given on the commandline.

The following shows a simple example on how to integrate PythonQt into your Qt application:

 #include "PythonQt.h"
 #include <QApplication>
 ...

 int main( int argc, char **argv )
 {

  QApplication qapp(argc, argv);

  // init PythonQt and Python itself
  PythonQt::init();

  // get a smart pointer to the __main__ module of the Python interpreter
  PythonQtObjectPtr context = PythonQt::self()->getMainModule();

  // add a QObject as variable of name "example" to the namespace of the __main__ module
  PyExampleObject example;
  context.addObject("example", &example);

  // do something
  context.evalScript("print example");
  context.evalScript("def multiply(a,b):\n  return a*b;\n");
  QVariantList args;
  args << 42 << 47;
  QVariant result = context.call("multiply", args);
  ...