PYQT 可直接通过信号槽,或者 invokeMethod 直接访问QT库的C++插件.

假设插件有一下接口:

class EchoInterface
{


public:
    virtual ~EchoInterface() {}
    virtual QString echo(const QString &message=QLatin1String("default_Value")) = 0;
    public slots:
    virtual void accept() = 0; 
signals:
    virtual void onEcho(const QString &message) = 0;


};


PYQT 一下代码,正常运行:

# -*- coding:utf-8 -*-
'''
Created on 2015年11月13日


@author: DXL


Copyright (C) 2004-2019 Shandong Zhaoyuan Software Development Co.,Ltd


'''
import sys
import inspect
import cgitb
cgitb.enable(format='text')
#sys.path.append(object)
import os
print os.curdir
import PyQt5
dirname = os.path.dirname(PyQt5.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
dir(PyQt5)
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
_app = None
class EchoWindow(QtWidgets.QWidget):
    onSendEcho = QtCore.pyqtSignal(str)
    def __init__(self,parent = None):
        QtWidgets.QWidget.__init__(self,parent)
        self.echoInterface = None
        self.lineEdit = None
        self.label = None
        self.button = None
        self._layout = None
        self.createGUI()
        self.setLayout(self._layout)
        self.setWindowTitle(u'Echo Plugin Example')
        if not self.loadPlugin():
            QtWidgets.QMessageBox.information(self,'error',"Could not load the plugin")
            
    def accept(self):
        pass
    def sendEcho(self):
        self.onSendEcho.emit(self.lineEdit.text() )
        ret_v = QtCore.Q_RETURN_ARG('QString')
        test = QtCore.QMetaObject.invokeMethod(self.echoInterface,'echo',QtCore.Qt.DirectConnection,ret_v,QtCore.Q_ARG('QString',self.lineEdit.text() ))
        self.label.setText(test);


    def printEcho(self,msg):
        print 'MessageFrom QT signal:',msg
        pass
    def createGUI(self):
        self.lineEdit = QtWidgets.QLineEdit()
        self.label = QtWidgets.QLabel()
        self.button = QtWidgets.QPushButton(u'Send Message')
        self.lineEdit.editingFinished.connect(self.sendEcho)
        self.button.clicked.connect(self.sendEcho)
        self._layout = QtWidgets.QGridLayout()
        self._layout.addWidget(QtWidgets.QLabel(u'Message:'),0,0)
        self._layout.addWidget(self.lineEdit,0,1)
        self._layout.addWidget(QtWidgets.QLabel(u'Answer:'),1,0)
        self._layout.addWidget(self.label,1,1)
        self._layout.addWidget(self.button,2,1,QtCore.Qt.AlignRight)
        pass
    def loadPlugin(self):
        pluginsDir = QtCore.QDir(_app.applicationDirPath());
        pluginsDir = QtCore.QDir('./');
        pluginsDir.cd("plugins");
        for fileName in pluginsDir.entryList(QtCore.QDir.Files):
            abs_path = pluginsDir.absoluteFilePath(fileName)
            pluginLoader = QtCore.QPluginLoader(abs_path);
            print 'dll status:',pluginLoader.isLoaded()
            self.echoInterface = pluginLoader.instance();
            if self.echoInterface:
                self.echoInterface.onEcho.connect(self.printEcho,QtCore.Qt.QueuedConnection)
                self.onSendEcho.connect(self.echoInterface.echo)
                return True
        return False
        
if __name__ == '__main__':
    global _app
    a= QtWidgets.QApplication([])
    _app = a
    w = EchoWindow(None)
    w.show()
    _app.exec_()



附C++ 全部代码:

echointerface.h


/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/


#ifndef ECHOINTERFACE_H
#define ECHOINTERFACE_H


#include <QString>


//! [0]
class EchoInterface
{


public:
    virtual ~EchoInterface() {}
    virtual QString echo(const QString &message=QLatin1String("default_Value")) = 0;
    public slots:
    virtual void accept() = 0; 
signals:
    virtual void onEcho(const QString &message) = 0;


};




QT_BEGIN_NAMESPACE


#define EchoInterface_iid "org.qt-project.Qt.Examples.EchoInterface"


Q_DECLARE_INTERFACE(EchoInterface, EchoInterface_iid)
QT_END_NAMESPACE


//! [0]
#endif



/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/


#ifndef ECHOPLUGIN_H
#define ECHOPLUGIN_H


#include <QObject>
#include <QtPlugin>
#include "echointerface.h"


//! [0]
class EchoPlugin : public QObject, EchoInterface
{
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.Examples.EchoInterface" FILE "echoplugin.json")
    Q_INTERFACES(EchoInterface)
private:
    QString m_message;
signals:
    void onEcho(const QString &message);
public slots:
    QString echo(const QString &message=QLatin1String("default_Value"));
    void accept(/* = 0 */);
public:
    int fun(const QString &msg);
    Q_INVOKABLE int fun2(const QString &msg);
};
//! [0]


#endif


/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtWidgets>


#include "echoplugin.h"


//! [0]
QString EchoPlugin::echo(const QString &message)
{
    if(m_message !=message)
        emit onEcho(message);
    m_message = message;
    return message;
}


void EchoPlugin::accept( /* = 0 */ )
{
    qDebug()<<"Accepted.";


}


int EchoPlugin::fun( const QString &msg )
{
    qDebug()<< "FUN msg:"<< msg;
    return msg.length();
}


int EchoPlugin::fun2( const QString &msg )
{
    qDebug()<< "__FUN2____ msg:"<< msg;
    return msg.length();
}
//! [0]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值