C++实例化的QApplication不被PyQt4所识别

3
我有一个使用C++/Qt编写的应用程序,它使用QPluginLoader工具加载插件(.dll/.so)。该插件基本上是一个嵌入式Python解释器,允许通过PyQt4模块检查主应用程序中的Qt对象。
问题在于,从插入的Python解释器执行的命令PyQt4.QtCore.QCoreApplication.instance()返回None,即使QCoreApplication实例已由C++应用程序创建。
这只发生在Windows的调试模式下。
在Linux或Windows的发布模式下,PyQt4.QtCore.QCoreApplication.instance()命令将正确返回由C++应用程序创建的QCoreApplication实例。
以下是一些最小化的代码,显示了该问题。
在发布模式下编译时:
$ ./a.out
1+1
2
import PyQt4
import PyQt4.QtCore
PyQt4.QtCore.QCoreApplication.instance()
<PyQt4.QtCore.QCoreApplication object at 0x00C69198>

=> 好的

当调试模式编译时:

$ ./a.out
import PyQt4
import PyQt4.QtCore
PyQt4.QtCore.QCoreApplication.instance()

=> 不行(返回了None)

文件 main.cpp

#include <QCoreApplication>
#include <QPluginLoader>
#include <QDebug>

int main(int argc, char **argv)
{
        QCoreApplication app(argc, argv);

        QPluginLoader loader("plugin.dll");
        loader.setLoadHints(QLibrary::ResolveAllSymbolsHint | QLibrary::ExportExternalSymbolsHint);
        loader.load();
        if(!loader.isLoaded()) {
                qDebug() << loader.errorString();
                return 1;
        }
        (void)loader.instance();

        return app.exec();
}

文件plugin.h
#ifndef PLUGIN_H
#define PLUGIN_H

#include <QObject>
#include <Python.h>

class Plugin : public QObject
{
public:
        Plugin();
        ~Plugin();

private:
        PyThreadState *m_ts;
};

class InterpInput : public QObject
{
        Q_OBJECT
public:
        InterpInput(QObject *parent = 0) : QObject(parent) { }
public slots:
        void monitorInput();
signals:
        void done();
        void inputReady();
};

class InterpOutput : public QObject
{
        Q_OBJECT
public:
        InterpOutput(QObject *parent = 0) : QObject(parent) { }
public slots:
        void processLine();
public:
        PyThreadState *m_ts;
};

#endif

文件 plugin.cpp

#include "plugin.h"

#include <QCoreApplication>
#include <QThread>
#include <QtPlugin>
#include <QPluginLoader>

Q_EXPORT_PLUGIN2(Plugin, Plugin)

Plugin::Plugin()
{
        Py_Initialize();
        PyEval_InitThreads();

        InterpInput *in = new InterpInput();
        InterpOutput *out = new InterpOutput(this);
        in->connect(in, SIGNAL(inputReady()), out, SLOT(processLine()));
        in->connect(in, SIGNAL(done()), QCoreApplication::instance(), SLOT(quit()));

        QThread *thr = new QThread(this);
        in->moveToThread(thr);
        thr->connect(thr, SIGNAL(started()), in, SLOT(monitorInput()));

        m_ts = PyEval_SaveThread();
        out->m_ts = m_ts;

        thr->start();
}

Plugin::~Plugin()
{
        PyEval_RestoreThread(m_ts);
        Py_Finalize();
}

void InterpInput::monitorInput()
{
        PyGILState_STATE gstate;
        gstate = PyGILState_Ensure();

        int ret = PyRun_SimpleString("import sys\nimport code\nic = code.InteractiveConsole()");
        assert(ret == 0);
        while(true) {
                ret = PyRun_SimpleString("line = ic.raw_input()");
                if(ret) { break; }
                inputReady();
        }
        done();

        PyGILState_Release(gstate);
}

void InterpOutput::processLine()
{
        PyEval_RestoreThread(m_ts);
        int ret = PyRun_SimpleString("ic.push(line)");
        PyRun_SimpleString("sys.stdout.flush()");
        PyRun_SimpleString("sys.stderr.flush()");
        (void)PyEval_SaveThread();
        assert(ret == 0);
}

文件 Makefile

MOC=/cygdrive/c/Qt/4.8.4/bin/moc
GCC=/cygdrive/c/MinGW/bin/mingw32-g++.exe
FLAGS=-Ic:/Qt/4.8.4/include -Ic:/Qt/4.8.4/include/QtCore -Lc:/Qt/4.8.4/lib -Lc:/Qt/4.8.4/bin -lQtCore4 -Lc:/Python27/libs -lpython27 -Ic:/Python27/include -DQT_NO_DEBUG
#FLAGS=-Ic:/Qt/4.8.4/include -Ic:/Qt/4.8.4/include/QtCore -Lc:/Qt/4.8.4/bin -lQtCored4 -Lc:/Python27/libs -lpython27 -Ic:/Python27/include -g
LIBFLAGS=-shared

all:
        $(MOC) plugin.h > plugin_moc.cpp
        $(GCC) -o a.out main.cpp $(FLAGS)
        $(GCC) -o plugin.dll $(LIBFLAGS) plugin.cpp plugin_moc.cpp $(FLAGS)

你是否尝试过像在加载/初始化插件时从主代码中传递Qapp实例到插件中一样的解决方法? - Mat
或多或少。确实有一些解决方法可以获取QCoreApplication实例。但是,我还没有提到的根本问题是,如果PyQt本身无法看到QApplication实例,则无法从PyQt执行某些需要QApplication的操作(例如QPixmap创建)。这些操作可以在自定义模块中从C++包装到Python中,但这将重复PyQt应该执行的操作。 - user2179288
问题只发生在调试模式下,在发布模式下一切正常。 - user2179288
可能是因为(CoreApplication)符号在(debug)Qt库和(non debug)PyQt4模块之间具有略微不同的名称,这会使“C++ QCoreApplication”在PyQt4中不可见? - user2179288
可能的解释是插件的调试版本链接到QtCore4d.dll,而PyQt4链接到QtCore4.dll,因此应用程序+插件和PyQt4分别引用了两个不同的QtCore库。 - user2179288
1个回答

0

解释如下。

在调试版本中,C++应用程序和插件链接到调试Qt库(QtCored4.dll等)。 而安装的PyQt4模块(QtCore.pyd等)链接到发布的Qt库(QtCore4.dll等)

因此,C++应用程序和PyQt4模块各自看到一个QCoreApplication实例,但每个实例都看到不同的实例,这些实例位于不同的库中(分别是Qt库的调试版本和发布版本)。

由此可见,在C++应用程序初始化时未初始化PyQt4模块中的实例,因此为空。

通过编译指定应将PyQt4模块链接到调试Qt库来验证此问题:然后问题消失,因为调试模式的C++应用程序和PyQt4模块链接到相同的Qt库。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接