我正在尝试使用python脚本插件扩展我的Qt应用程序。 如果我调用任何非pyqt脚本,效果都很好。 如果我从c ++函数调用任何pyqt脚本,它也可以正常工作,但是我不在Qt Widget应用程序之外。像这样:
#include "/usr/include/python3.5m/Python.h"
int CargaPlugins(const char* ruta, const char* nombremodulo, const char* nombrefuncion);
int main(int argc, char** argv)
{
std::string path = "PYTHONPATH=";
path.append(argv[1]);
putenv ((char*)path.c_str());
Py_Initialize();
CargaPlugins(argv[1],"plugin_loader","iniciar");
Py_Finalize();
return 0;
}
int CargaPlugins(const char* ruta, const char* nombremodulo, const char* nombrefuncion)
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
pName = PyUnicode_DecodeFSDefault(nombremodulo);
/* Error checking of pName left out */
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule != NULL)
{
pFunc = PyObject_GetAttrString(pModule, nombrefuncion);
/* pFunc is a new reference */
if (pFunc && PyCallable_Check(pFunc))
{
pArgs = PyTuple_New(1);
pValue = PyUnicode_FromString(ruta);
if (!pValue)
{
Py_DECREF(pArgs);
Py_DECREF(pModule);
fprintf(stderr, "Cannot convert argument\n");
return 1;
}
/* pValue reference stolen here: */
PyTuple_SetItem(pArgs, 0, pValue);
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL)
{
printf("Result of call: %ld\n", PyLong_AsLong(pValue));
Py_DECREF(pValue);
}
else
{
Py_DECREF(pFunc);
Py_DECREF(pModule);
PyErr_Print();
fprintf(stderr,"Call failed\n");
return 1;
}
}
else
{
if (PyErr_Occurred())
PyErr_Print();
fprintf(stderr, "Cannot find function \"%s\"\n", nombrefuncion);
}
Py_XDECREF(pFunc);
Py_DECREF(pModule);
}
else
{
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", nombremodulo);
return 1;
}
}
还有pyqt5模块:
plugin_loader.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import imp
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from DialogoImprimir import DialogoImprimir
def iniciar(ruta):
import sys
if not hasattr(sys,'argv'):
sys.argv = []
app = QtWidgets.QApplication(sys.argv)
myapp = DialogoImprimir(getPlugins(ruta))
myapp.show()
sys.exit(app.exec_())
DialogoImprimir.py
class DialogoImprimir(QtWidgets.QDialog):
def __init__(self, datos):
QtWidgets.QDialog.__init__(self)
self.datos = datos
self.GeneraUI(datos)
-------------------
好吧,我的问题是,如果我像函数一样将C ++ int CargaPlugins(const char* ruta, const char* nombremodulo, const char* nombrefuncion)
插入到Qt Widgwets应用程序中,则在调用时会出现此错误:
QCoreApplication :: exec:事件循环已在运行
我认为解决方案是将当前QApplication的指针传递给python脚本,或者如果有任何方法可以在运行脚本python并使用它时获取当前QApplication,但是我不这样做不知道该怎么办。
编辑:
当我在Qt中调用函数时的代码片段:
mainwindow.cpp
void MainWindow::ActionImprimir()
{
Imprimir impresor("/home/user/pathofpythonmodules/","plugin_loader","iniciar");
imprimir.cpp
Imprimir::Imprimir(const char* ruta, const char* nombremodulo, const char* nombrefuncion)
{
std::string path = "PYTHONPATH=";
path.append(ruta);
putenv ((char*)path.c_str());
Py_Initialize();
pFuncion = CargarPlugins(ruta,nombremodulo,nombrefuncion);
if (pFuncion)
{
//more things
}
}
(和CargarPlugins()与以前的功能相同)
答案 0 :(得分:2)
由于您拥有一个QApplication,因此无需创建另一个QApplication,因此解决方案是:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import importlib
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from DialogoImprimir import DialogoImprimir
def iniciar(ruta):
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication([])
myapp = DialogoImprimir(getPlugins(ruta))
return myapp.exec_()
您可以找到一个完整的示例here