Commits

Henning Schröder committed 685a274

firsts working prototype can execute Python which can execute C++ slots

Comments (0)

Files changed (10)

 TEMPLATE = app
 
 SOURCES += main.cpp\
-        mainwindow.cpp \
-    embpyqt/embeddedpyqt.cpp
-
-HEADERS  += mainwindow.h \
-    embpyqt/embeddedpyqt.h
-
+        mainwindow.cpp
+HEADERS  += mainwindow.h
 FORMS    += mainwindow.ui
 
 # Python support
-LIBS     += -L/usr/lib/ -lpython2.7
+INCLUDEPATH += -I./embpyqt
+LIBS        += -lpython2.7 -L/usr/lib/
 INCLUDEPATH += /usr/include/python2.7/
+SOURCES += \
+    embpyqt/embeddedpyqt.cpp \
+    embpyqt/pythonize.cpp
+HEADERS += \
+    embpyqt/embeddedpyqt.h \
+    embpyqt/pythonize.h
+

embeddedpyqt.pro.user

  <data>
   <variable>ProjectExplorer.Project.EditorSettings</variable>
   <valuemap type="QVariantMap">
-   <value key="EditorConfiguration.Codec" type="QByteArray">Default</value>
+   <value key="EditorConfiguration.AutoIndent" type="bool">true</value>
+   <value key="EditorConfiguration.AutoSpacesForTabs" type="bool">false</value>
+   <value key="EditorConfiguration.Codec" type="QByteArray">UTF-8</value>
+   <value key="EditorConfiguration.DoubleIndentBlocks" type="bool">false</value>
+   <value key="EditorConfiguration.IndentBraces" type="bool">false</value>
+   <value key="EditorConfiguration.IndentSize" type="int">4</value>
+   <value key="EditorConfiguration.MouseNavigation" type="bool">true</value>
+   <value key="EditorConfiguration.PaddingMode" type="int">1</value>
+   <value key="EditorConfiguration.ScrollWheelZooming" type="bool">true</value>
+   <value key="EditorConfiguration.SmartBackspace" type="bool">true</value>
+   <value key="EditorConfiguration.SpacesForTabs" type="bool">true</value>
+   <value key="EditorConfiguration.TabKeyBehavior" type="int">1</value>
+   <value key="EditorConfiguration.TabSize" type="int">8</value>
+   <value key="EditorConfiguration.UseGlobal" type="bool">true</value>
+   <value key="EditorConfiguration.Utf8BomBehavior" type="int">1</value>
+   <value key="EditorConfiguration.addFinalNewLine" type="bool">true</value>
+   <value key="EditorConfiguration.cleanIndentation" type="bool">true</value>
+   <value key="EditorConfiguration.cleanWhitespace" type="bool">true</value>
+   <value key="EditorConfiguration.inEntireDocument" type="bool">false</value>
   </valuemap>
  </data>
  <data>
   <variable>ProjectExplorer.Project.Target.0</variable>
   <valuemap type="QVariantMap">
    <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Desktop</value>
-   <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
+   <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString">Desktop</value>
    <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.Target.DesktopTarget</value>
    <value key="ProjectExplorer.Target.ActiveBuildConfiguration" type="int">0</value>
    <value key="ProjectExplorer.Target.ActiveDeployConfiguration" type="int">0</value>
    <value key="ProjectExplorer.Target.ActiveRunConfiguration" type="int">0</value>
    <valuemap key="ProjectExplorer.Target.BuildConfiguration.0" type="QVariantMap">
+    <value key="ProjectExplorer.BuildCOnfiguration.ToolChain" type="QString">ProjectExplorer.ToolChain.Gcc:/usr/bin/g++.x86-linux-generic-elf-32bit.</value>
     <valuemap key="ProjectExplorer.BuildConfiguration.BuildStepList.0" type="QVariantMap">
      <valuemap key="ProjectExplorer.BuildStepList.Step.0" type="QVariantMap">
       <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">qmake</value>
       <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
       <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">QtProjectManager.QMakeBuildStep</value>
-      <valuelist key="QtProjectManager.QMakeBuildStep.QMakeArguments" type="QVariantList"/>
+      <value key="QtProjectManager.QMakeBuildStep.LinkQmlDebuggingLibrary" type="bool">false</value>
+      <value key="QtProjectManager.QMakeBuildStep.QMakeArguments" type="QString"></value>
+      <value key="QtProjectManager.QMakeBuildStep.QMakeForced" type="bool">false</value>
      </valuemap>
      <valuemap key="ProjectExplorer.BuildStepList.Step.1" type="QVariantMap">
       <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Make</value>
       <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
       <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.MakeStep</value>
       <value key="Qt4ProjectManager.MakeStep.Clean" type="bool">false</value>
-      <valuelist key="Qt4ProjectManager.MakeStep.MakeArguments" type="QVariantList"/>
+      <value key="Qt4ProjectManager.MakeStep.MakeArguments" type="QString"></value>
       <value key="Qt4ProjectManager.MakeStep.MakeCommand" type="QString"></value>
      </valuemap>
      <value key="ProjectExplorer.BuildStepList.StepsCount" type="int">2</value>
       <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
       <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.MakeStep</value>
       <value key="Qt4ProjectManager.MakeStep.Clean" type="bool">true</value>
-      <valuelist key="Qt4ProjectManager.MakeStep.MakeArguments" type="QVariantList">
-       <value type="QString">clean</value>
-      </valuelist>
+      <value key="Qt4ProjectManager.MakeStep.MakeArguments" type="QString">clean</value>
       <value key="Qt4ProjectManager.MakeStep.MakeCommand" type="QString"></value>
      </valuemap>
      <value key="ProjectExplorer.BuildStepList.StepsCount" type="int">1</value>
     <value key="ProjectExplorer.BuildConfiguration.BuildStepListCount" type="int">2</value>
     <value key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment" type="bool">false</value>
     <valuelist key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges" type="QVariantList"/>
-    <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Debug</value>
-    <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
-    <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.Qt4BuildConfiguration</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration" type="int">2</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.BuildDirectory" type="QString">/data/henning/src/embeddedpyqt-build-desktop</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.QtVersionId" type="int">2</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.ToolChain" type="int">0</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild" type="bool">true</value>
-   </valuemap>
-   <valuemap key="ProjectExplorer.Target.BuildConfiguration.1" type="QVariantMap">
-    <valuemap key="ProjectExplorer.BuildConfiguration.BuildStepList.0" type="QVariantMap">
-     <valuemap key="ProjectExplorer.BuildStepList.Step.0" type="QVariantMap">
-      <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">qmake</value>
-      <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
-      <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">QtProjectManager.QMakeBuildStep</value>
-      <valuelist key="QtProjectManager.QMakeBuildStep.QMakeArguments" type="QVariantList"/>
-     </valuemap>
-     <valuemap key="ProjectExplorer.BuildStepList.Step.1" type="QVariantMap">
-      <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Make</value>
-      <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
-      <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.MakeStep</value>
-      <value key="Qt4ProjectManager.MakeStep.Clean" type="bool">false</value>
-      <valuelist key="Qt4ProjectManager.MakeStep.MakeArguments" type="QVariantList"/>
-      <value key="Qt4ProjectManager.MakeStep.MakeCommand" type="QString"></value>
-     </valuemap>
-     <value key="ProjectExplorer.BuildStepList.StepsCount" type="int">2</value>
-     <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Build</value>
-     <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
-     <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">ProjectExplorer.BuildSteps.Build</value>
-    </valuemap>
-    <valuemap key="ProjectExplorer.BuildConfiguration.BuildStepList.1" type="QVariantMap">
-     <valuemap key="ProjectExplorer.BuildStepList.Step.0" type="QVariantMap">
-      <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Make</value>
-      <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
-      <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.MakeStep</value>
-      <value key="Qt4ProjectManager.MakeStep.Clean" type="bool">true</value>
-      <valuelist key="Qt4ProjectManager.MakeStep.MakeArguments" type="QVariantList">
-       <value type="QString">clean</value>
-      </valuelist>
-      <value key="Qt4ProjectManager.MakeStep.MakeCommand" type="QString"></value>
-     </valuemap>
-     <value key="ProjectExplorer.BuildStepList.StepsCount" type="int">1</value>
-     <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Bereinigen</value>
-     <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
-     <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">ProjectExplorer.BuildSteps.Clean</value>
-    </valuemap>
-    <value key="ProjectExplorer.BuildConfiguration.BuildStepListCount" type="int">2</value>
-    <value key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment" type="bool">false</value>
-    <valuelist key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges" type="QVariantList"/>
-    <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">Release</value>
+    <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">4.7.3 Release</value>
     <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
     <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.Qt4BuildConfiguration</value>
     <value key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration" type="int">0</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.BuildDirectory" type="QString">/data/henning/src/embeddedpyqt-build-desktop</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.QtVersionId" type="int">2</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.ToolChain" type="int">0</value>
-    <value key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild" type="bool">true</value>
+    <value key="Qt4ProjectManager.Qt4BuildConfiguration.BuildDirectory" type="QString">/home/henning/src/embeddedpyqt</value>
+    <value key="Qt4ProjectManager.Qt4BuildConfiguration.QtVersionId" type="int">4</value>
+    <value key="Qt4ProjectManager.Qt4BuildConfiguration.ToolChain" type="QString">ProjectExplorer.ToolChain.Gcc:/usr/bin/g++.x86-linux-generic-elf-32bit.</value>
+    <value key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild" type="bool">false</value>
    </valuemap>
-   <value key="ProjectExplorer.Target.BuildConfigurationCount" type="int">2</value>
+   <value key="ProjectExplorer.Target.BuildConfigurationCount" type="int">1</value>
    <valuemap key="ProjectExplorer.Target.DeployConfiguration.0" type="QVariantMap">
     <valuemap key="ProjectExplorer.BuildConfiguration.BuildStepList.0" type="QVariantMap">
      <value key="ProjectExplorer.BuildStepList.StepsCount" type="int">0</value>
    </valuemap>
    <value key="ProjectExplorer.Target.DeployConfigurationCount" type="int">1</value>
    <valuemap key="ProjectExplorer.Target.RunConfiguration.0" type="QVariantMap">
+    <valuelist key="Analyzer.Valgrind.AddedSupressionFiles" type="QVariantList"/>
+    <value key="Analyzer.Valgrind.FilterExternalIssues" type="bool">true</value>
+    <value key="Analyzer.Valgrind.NumCallers" type="int">25</value>
+    <valuelist key="Analyzer.Valgrind.RemovedSupressionFiles" type="QVariantList"/>
+    <value key="Analyzer.Valgrind.TrackOrigins" type="bool">true</value>
+    <value key="Analyzer.Valgrind.ValgrindExecutable" type="QString">valgrind</value>
+    <valuelist key="Analyzer.Valgrind.VisibleErrorKinds" type="QVariantList">
+     <value type="int">0</value>
+     <value type="int">1</value>
+     <value type="int">2</value>
+     <value type="int">3</value>
+     <value type="int">4</value>
+     <value type="int">5</value>
+     <value type="int">6</value>
+     <value type="int">7</value>
+     <value type="int">8</value>
+     <value type="int">9</value>
+     <value type="int">10</value>
+     <value type="int">11</value>
+     <value type="int">12</value>
+     <value type="int">13</value>
+     <value type="int">14</value>
+    </valuelist>
     <value key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName" type="QString">embeddedpyqt</value>
     <value key="ProjectExplorer.ProjectConfiguration.DisplayName" type="QString"></value>
     <value key="ProjectExplorer.ProjectConfiguration.Id" type="QString">Qt4ProjectManager.Qt4RunConfiguration</value>
     <value key="Qt4ProjectManager.Qt4RunConfiguration.BaseEnvironmentBase" type="int">2</value>
-    <valuelist key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments" type="QVariantList"/>
+    <value key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments" type="QString"></value>
     <value key="Qt4ProjectManager.Qt4RunConfiguration.ProFile" type="QString">embeddedpyqt.pro</value>
     <value key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix" type="bool">false</value>
     <value key="Qt4ProjectManager.Qt4RunConfiguration.UseTerminal" type="bool">false</value>
     <valuelist key="Qt4ProjectManager.Qt4RunConfiguration.UserEnvironmentChanges" type="QVariantList"/>
-    <value key="Qt4ProjectManager.Qt4RunConfiguration.UserSetWorkingDirectory" type="bool">false</value>
     <value key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory" type="QString"></value>
     <value key="RunConfiguration.QmlDebugServerPort" type="uint">3768</value>
     <value key="RunConfiguration.UseCppDebugger" type="bool">true</value>
  </data>
  <data>
   <variable>ProjectExplorer.Project.Updater.EnvironmentId</variable>
-  <value type="QString">{82f65e68-5bb8-4a77-89dc-271f23e1a6c6}</value>
+  <value type="QString">{1bd1386d-682a-4340-8c84-939385b840a6}</value>
  </data>
  <data>
   <variable>ProjectExplorer.Project.Updater.FileVersion</variable>
-  <value type="int">8</value>
+  <value type="int">9</value>
  </data>
 </qtcreator>
+import sys
+from PyQt4.QtGui import qApp
+from PyQt4.QtCore import QMetaType, QMetaObject
+import sip
+
+def on_error(*args):
+	print "error", args
+sys.excepthook = on_error
+
+embpyqt = qApp.property("embedded_pyqt").toPyObject()
+
+ptr = embpyqt.metaObjectByName("QPushButton")
+mo = sip.wrapinstance(int(ptr), QMetaObject)
+print mo.className()
+w = mo.newInstance(qApp.activeWindow())
+print w
+
+embpyqt.setException("foo", "baz")
+
+
+print "done"
+1/0

embpyqt/embeddedpyqt.cpp

 #include "pythonize.h"
 #include <QApplication>
 #include <QVariant>
+#include <QPushButton>
+#include <qdebug.h>
+
+
 
 EmbeddedPyQt::EmbeddedPyQt(QObject *parent) :
     QObject(parent)
 {
+    setObjectName("embeddedPyQt");
+    //REGISTER(QObject);
+    classes["QObject"] = &QObject::staticMetaObject;
+    classes["QPushButton"] = &QPushButton::staticMetaObject;
+
+    //EmbeddedPyQt::instance = this;
     python = new Pythonize();
     Q_ASSERT(python);
-    QApplication::instance()->setProperty("embedded_pyqt", QVariant(this))
+    QApplication::instance()->setProperty("embedded_pyqt",
+                                          qVariantFromValue((QObject*)this));
+    python->runScript("embeddedpyqt.py");
+    //python->appendToSysPath("");
+    //python->runString("import embeddedpyqt");
 }
 
 
-EmbeddedPyQt::deleteLater() {
+void EmbeddedPyQt::deleteLater() {
     Q_ASSERT(python);
+    QApplication::instance()->setProperty("embedded_pyqt",
+                                          qVariantFromValue(NULL));
     delete(python);
 }
+
+
+void EmbeddedPyQt::setException(const QString &type, const QString &message) {
+    QApplication::instance()->setProperty("embedded_pyqt_exception_type",
+                                         qVariantFromValue(type));
+    QApplication::instance()->setProperty("embedded_pyqt_exception_msg",
+                                         qVariantFromValue(message));
+    const char *cmsg = message.toLocal8Bit().constData();
+    python->raiseException(cmsg);
+}
+
+
+void EmbeddedPyQt::clearException() {
+    setException(NULL, NULL);
+}
+
+void* EmbeddedPyQt::metaObjectByName(const QString &name) {
+    return (void*)classes[name];
+}
+

embpyqt/embeddedpyqt.h

 #define EMBEDDEDPYQT_H
 
 #include <QObject>
+#include <QMap>
 #include "pythonize.h"
 
 
+#define RAISE(type,msg) EmbeddedPyQt::instance->setException(type, msg)
+#define REGISTER(type) EmbeddedPyQt::instance->classes[type::staticMetaObject.className()] = &type::staticMetaObject
+
+
 class EmbeddedPyQt : public QObject
 {
     Q_OBJECT
+
 public:
     explicit EmbeddedPyQt(QObject *parent = 0);
+    static EmbeddedPyQt *instance;
+    QMap<QString,const QMetaObject*> classes;
 
 signals:
 
 public slots:
+    void deleteLater();
+    void setException(const QString &type, const QString &message);
+    void clearException();
+    void* metaObjectByName(const QString &name);
 
 private:
     Pythonize *python;
+
 };
 
+
+
 #endif // EMBEDDEDPYQT_H

embpyqt/pythonize.cpp

 // this just gets rid of a warning when Python.h redefines it
 #undef _POSIX_C_SOURCE
 
-#include <pythonize.h>
+#include "pythonize.h"
 
 #define debug 1
 
     return module;
 }
 
+void Pythonize::raiseException(const char *msg) {
+    PyErr_SetString(PyExc_RuntimeError, msg);
+}
 
 Pythonize::~Pythonize ()
 {
     if (debug) printf (" --- Py_Finalized\n");
 }
 
-//// XXX: disabled
-//// The global Pythonize instance
-//Pythonize *pyize = new Pythonize ();
-
-extern "C"
-{
-    Pythonize *_pythonize;
-
-    Pythonize *initialize ()
-    {
-        if (_pythonize) return _pythonize;
-
-        _pythonize = new Pythonize ();
-        if (!_pythonize || !_pythonize->getPythonInit ())
-        {
-            if (_pythonize) delete _pythonize;
-            return NULL;
-        }
-
-        return _pythonize;
-    }
-
-    void finalize ()
-    {
-        if (_pythonize) {
-            if (debug) printf(" --- Pythonize finalize()\n");
-            delete _pythonize;
-        }
-    }
-
-    // adds a path to sys.path
-    bool appendToSysPath (const char* newPath)
-    {
-        return _pythonize ? _pythonize->appendToSysPath (newPath) : false;
-    }
-
-    // imports a module into the interpreter
-    // or gets a PyObject for an already loaded module
-    PyObject *importModule (char *moduleName)
-    {
-        return _pythonize ? _pythonize->importModule (moduleName) : NULL;
-    }
-
-    // returns an object from a loaded module
-    // you must decref the object returned when done with it (new reference returned)
-    PyObject *getNewObjectRef (PyObject *module, char *object)
-    {
-        return _pythonize ? _pythonize->getNewObjectRef (module, object) : NULL;
-    }
-
-    bool getPythonInit ()
-    {
-        return _pythonize ? _pythonize->getPythonInit () : false;
-    }
-
-    // decrements the ref count of an object
-    void decref (PyObject *object)
-    {
-        Py_XDECREF (object);
-    }
-
-    // runs a script on the current sys.path
-    bool runScript (char *scriptPath)
-    {
-        return _pythonize ? _pythonize->runScript (scriptPath) : false;
-    }
-
-    // executes a string of Python in the interpreter
-    bool runString (char *str)
-    {
-        return _pythonize ? _pythonize->runString (str) : false;
-    }
-
-    // runs a callable Python object
-    PyObject *runFunction (PyObject *object, PyObject *args)
-    {
-        return _pythonize ? _pythonize->runFunction (object, args) : NULL;
-    }
-}

embpyqt/pythonize.h

     // or gets a PyObject for an already loaded module
     PyObject *importModule (char *moduleName);
 
+    void raiseException(const char *msg);
     // returns an object from a loaded module
     // you must decref the object returned when done with it (new reference returned)
     PyObject *getNewObjectRef (PyObject *module, char *object) {
     w.show();
 
     EmbeddedPyQt *embpyqt;
-    embpyqt = new EmbeddedPyQt(w);
+    embpyqt = new EmbeddedPyQt();
     return a.exec();
 }
+
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
 
-MainWindow::MainWindow(QWidget *parent=null) :
+MainWindow::MainWindow(QWidget *parent) :
     QMainWindow(parent),
     ui(new Ui::MainWindow)
 {
 
 private:
     Ui::MainWindow *ui;
-    EmbeddedPyQt *embpyqt;
 };
 
 #endif // MAINWINDOW_H