logplus/app/example_plugin/pythonhandler.h

60 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef PYTHONHANDLER_H
#define PYTHONHANDLER_H
#include <QObject>
#include <QString>
#include <QVariant>
#include <QDebug>
// 在包含 Python.h 之前,保存并取消 Qt 的 slots 宏
#pragma push_macro("slots")
#undef slots
#include <Python.h>
#include <numpy/arrayobject.h>
// 包含之后恢复 slots 宏(如果需要继续使用 Qt 的 slots
#pragma pop_macro("slots")
#define PYTHON_VER "Python312"
class PythonHandler : public QObject
{
Q_OBJECT
public:
explicit PythonHandler(QObject *parent = nullptr);
~PythonHandler();
static PythonHandler* getInstance();
// 初始化Python解释器
bool initialize();
void setupEnvironment(QString strPy);
QString capturePythonError();
// 执行Python脚本
QVariant executeScript(const QString &scriptPath,
const QString &functionName,
const QVariantList &args = QVariantList() ,
float* outAtt0all = NULL, int nlen = 0);
// 直接执行Python代码
QVariant executeCode(const QString &code);
// 关闭Python解释器
void finalize();
signals:
void addLog(QString msg);
private:
bool m_initialized;
QVariant pythonObjectToQVariant(PyObject *obj);
PyObject* qVariantToPythonObject(const QVariant &value);
};
#endif // PYTHONHANDLER_H