从Dll外部连接dsl信号/插槽

时间:2013-09-08 09:45:55

标签: c++ qt dll

在Qt中,我的Dll有问题。 在我的Dll中,我有一个从dll内部发出的信号。 在另一个我的测试应用程序我用Qliberary加载我的DLL并没有问题。 我已经导出了我的dll函数,我可以在我的测试项目中没有问题地调用它们。 但我无法将我的DLL信号连接到测试应用程序中的测试应用程序的插槽中。 它得到这个错误:QObject :: connect:没有这样的信号QLibrary :: dllsignal()。

我的dll.cpp

#include "qtmfcbrooks.h"


QtMfcBrooks::QtMfcBrooks()
{
    qDebug()<<"loading QtMfcBrooks ...";
    //donot send any signal or call other functions
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(slotTest()));
    timer->start(2000);
}

void QtMfcBrooks::slotTest(QString)
{
    emit sigTest("This signal is emitted from dll at"+QDateTime::currentDateTime().toString());
}

void QtMfcBrooks::slotTestMain(QString str)
{
    qDebug()<<str;
}

void QtMfcBrooks::slotTestPublic()
{
    qDebug()<<"slotTestPublic at"+QDateTime::currentDateTime().toString();
}

dll.h

#ifndef QTMFCBROOKS_H
#define QTMFCBROOKS_H


#include <QByteArray>
#include <math.h>
#include <QTimer>
#include <QtCore/qglobal.h>


#if defined(QTMFCBROOKS_LIBRARY)
#  define QTMFCBROOKSSHARED_EXPORT Q_DECL_EXPORT
#else
#  define QTMFCBROOKSSHARED_EXPORT Q_DECL_IMPORT
#endif


class QTMFCBROOKSSHARED_EXPORT QtMfcBrooks :public QObject
{
    Q_OBJECT

public:

    QtMfcBrooks();

signals:
       void sigTest(QString);
private:


    public slots:
         void slotTest(QString);
         void slotTestPublic();
         void slotTestMain(QString str);

private:

     QTimer * timer;
};

extern "C" {
   QTMFCBROOKSSHARED_EXPORT void testPublic();
   QTMFCBROOKSSHARED_EXPORT int main();
   QTMFCBROOKSSHARED_EXPORT void slotTestGlobal(QString);
}

#endif // QTMFCBROOKS_H

dll_global.cpp

#include <QApplication>
#include "qtmfcbrooks.h"

QtMfcBrooks * accessclass;

int main()
{
    qDebug()<<"loading main ...";
    if(accessclass) delete accessclass;
    accessclass = new QtMfcBrooks();

}

void testPublic()
{
    qDebug()<<"loading testPublic ...";
    accessclass->slotTestPublic();
}

void slotTestGlobal(QString msg)
{
    accessclass->slotTestMain(msg);
}

my test.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->qtmfcbrooks = new  QLibrary ("QtMfcBrooks");

    if (!qtmfcbrooks->load()) qDebug()<<qtmfcbrooks->errorString()<<QDateTime::currentDateTime();
    else {
        qDebug() << "Liberary is loaded";        
        connect(qtmfcbrooks,SIGNAL(sigTest(QString)),this->ui->label,SLOT(setText(QString)));
   }
    testMain();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::testMain()
{
    if(qtmfcbrooks && qtmfcbrooks->isLoaded()){
        typedef int (*f)(void);
        f func = (f) qtmfcbrooks->resolve("main");
        if (func){
            func();
            return;
        }else
            qDebug() <<"can not resolve "<<"main";
    }
}

我该怎么办?

0 个答案:

没有答案