简体   繁体   English

Qt C ++:在动态库的标头中,Connect调用失败

[英]Qt C++: Connect call fails in header in dynamic library

I have the following code which fails to connect for signals in header file for dynamic library(TestTemplateInheritance) but does work fine for static library 我有以下代码无法连接动态库的头文件中的信号(TestTemplateInheritance),但对于静态库却能正常工作

//TestTemplateInheritance.pro //TestTemplateInheritance.pro

QT       += core
QT       -= gui
TARGET = $$qtLibraryTarget($$TARGET)
TEMPLATE = lib
SOURCES += \
    base.cpp \  

HEADERS += \
    base.h \

//base.h //base.h

#ifndef BASE_H
#define BASE_H
#include <QObject>
#include <QDebug>
class Q_DECL_EXPORT Base : public QObject
{
    Q_OBJECT
public:
    explicit Base(QObject *parent = 0);

signals:
    void testSignal();
  public:
    template <class T>
    void callInHeader(T arg)
    {
        qCritical()<<"Does it connect here? in " << arg;
        connect(this, &Base::testSignal, this, &Base::testSlot);
    }
    void callInDefinition();
public slots:
    void testSlot()
    {
        qCritical() << "Slot called";
    }
};
#endif // BASE_H

//base.cpp //base.cpp

#include "base.h"

Base::Base(QObject *parent) : QObject(parent)
{    
}
void Base:: callInDefinition()
{
    qCritical()<<"Does it connect here?";
    connect(this, &Base::testSignal, this, &Base::testSlot);
    qCritical()<<"Yes it does!";
}

Now the application 现在申请

//TestTemplateFail.pro //TestTemplateFail.pro

QT       += core
QT       -= gui
TARGET = TestTemplateFail
CONFIG   += console
CONFIG   -= app_bundle
LIBS += -L$$PWD/../release/release -lTestTemplateInheritance
TEMPLATE = app
SOURCES += main.cpp

//main.cpp //main.cpp

#include <QCoreApplication>
#include "../TestTemplateInheritance/base.h"
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QObject *obj = new QObject();
    Base* d = new Base(obj);
    d->callInHeader<QString>("header");
    d->callInDefinition();
    return a.exec();
}

RESULT: 结果:

Does it connect here? in  "header"
QObject::connect: signal not found in Base
Does it connect here?
Yes it does!

If I build a static library(CONFIG += staticlib) instead of a dynamic library. 如果我建立一个静态库(CONFIG + = staticlib)而不是一个动态库。 There is no connect error 没有连接错误

RESULT: 结果:

Does it connect here? in  "header"
Does it connect here?
Yes it does!

I also observed that its not just function template, the connect statement in any function which is defined in the header file fails. 我还观察到它不仅是函数模板,而且在头文件中定义的任何函数中的connect语句都会失败。 Function templates are more obvious ones to have definition in header file. 函数模板是更明显的模板,可以在头文件中进行定义。 So why does the connect statement fail in the functions defined in the header file in dynamic library fail? 那么,为什么在动态库的头文件中定义的函数中的connect语句失败?

You have a problem with this line: 您对此行有疑问:

class Q_DECL_EXPORT Base : public QObject

When the header is included from another program (as opposed to when you compile your library) it should be : 如果标头包含在另一个程序中(而不是编译库时),则标头应为:

class Q_DECL_IMPORT Base : public QObject

The easy solution is to replace it by: 简单的解决方案是将其替换为:

#if defined(MYLIB_LIBRARY)
#  define MYLIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define MYLIBSHARED_EXPORT Q_DECL_IMPORT
#endif
class MYLIBSHARED_EXPORT Base : public QObject

and add 并添加

DEFINES += MYLIB_LIBRARY

ONLY in the .pro file of your library. 仅在您的库的.pro文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM