Skip to content

Commit d5f861a

Browse files
committed
added convenience evalScript
1 parent 9eeab0e commit d5f861a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/PythonQt.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,24 @@ QVariant PythonQt::evalScript(PyObject* object, const QString& script, int start
913913
return result;
914914
}
915915

916+
917+
QVariant PythonQt::evalScript(const QString& script, PyObject* globals, PyObject* locals, int start)
918+
{
919+
QVariant result;
920+
PythonQtObjectPtr p;
921+
clearError();
922+
if (globals) {
923+
p.setNewRef(PyRun_String(script.toLatin1().data(), start, globals, locals ? locals : globals));
924+
if (p) {
925+
result = PythonQtConv::PyObjToQVariant(p);
926+
} else {
927+
handleError();
928+
}
929+
}
930+
return result;
931+
}
932+
933+
916934
void PythonQt::evalFile(PyObject* module, const QString& filename)
917935
{
918936
// NOTE: error checking is done by parseFile and evalCode

src/PythonQt.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ class PYTHONQT_EXPORT PythonQt : public QObject {
343343

344344
//! evaluates the given script code and returns the result value
345345
QVariant evalScript(PyObject* object, const QString& script, int start = Py_file_input);
346+
347+
//! evaluates the given script code in context of given globals and locals and returns the result value
348+
QVariant evalScript(const QString& script, PyObject* globals, PyObject* locals, int start);
346349

347350
//! evaluates the given script code from file
348351
void evalFile(PyObject* object, const QString& filename);

0 commit comments

Comments
 (0)