从Qt小部件应用程序调用pyqt小部件

2024-09-30 00:38:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用python脚本插件扩展Qt应用程序。 如果我调用任何not pyqt脚本,它可以正常工作。 如果我从c++函数调用任何pyqt脚本,它也可以正常工作,但我在Qt小部件应用程序之外。像这样:

#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模块:

插件_加载程序.py

^{pr2}$

对话框omprimir.py

class DialogoImprimir(QtWidgets.QDialog):
    def __init__(self, datos):
        QtWidgets.QDialog.__init__(self)
        self.datos = datos
        self.GeneraUI(datos)
        -------------------
< >我的问题是,如果我把C++ ^ {< CD1>}插入到我的QT WiGWET应用程序中,就像函数一样,当调用:

时,我会得到这个错误

QCoreApplication::exec: The event loop is already running

我认为解决方案应该是将当前QApplication的指针传递给python脚本,或者如果有任何方法可以做到这一点,那么在python脚本运行时获取当前QApplication并使用它,但我不知道如何做到这一点。在

编辑:

在Qt中调用函数时的代码片段:

主窗口.cpp

void MainWindow::ActionImprimir()
{
    Imprimir impresor("/home/user/pathofpythonmodules/","plugin_loader","iniciar");

改进.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()与前面的函数相同)


Tags: pathpy脚本ifcharconstpvaluepname
1条回答
网友
1楼 · 发布于 2024-09-30 00:38:58

由于您有一个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

相关问题 更多 >

    热门问题