Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

[SOLVED] How to use connect in a shared library?



  • Hi
    I've written an application, now I copied the functions in a shared library to make a dll.
    Now I can see a weird error:

     #include "library1.h"
    
    Library1::Library1()
    {
    }
    
    void Library1::Inint_Lib()
    {
        if(Scan_Port())
        {
            Serial=new QSerialPort;
    QObject::connect(Serial,SIGNAL(readyRead()),this,SLOT(Capture_Received_Data()));
        }
    }
    

    It's working in the application with GUI, but this error occurs in the shared library project:

    Library1\library1.cpp:16: error: no matching function for call to 'QObject::connect(QSerialPort*&, const char*, Library1* const, const char*)'
    Library1\library1.cpp:16: candidates are:
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qiodevice.h:47: In file included from D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore/qiodevice.h:47:0,
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\QIODevice:1: from D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore/QIODevice:1,
    Library1\library1.h:7: from ..\Library1\library1.h:7,
    Library1\library1.cpp:1: from ..\Library1\library1.cpp:1:
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:198: static QMetaObject::Connection QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:201: static QMetaObject::Connection QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:201: note:   no known conversion for argument 2 from 'const char*' to 'const QMetaMethod&'
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:440: QMetaObject::Connection QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:440: note:   no known conversion for argument 3 from 'Library1* const' to 'const char*'
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:214: template<class Func1, class Func2> static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType)
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:214: note:   template argument deduction/substitution failed:
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:-1: In substitution of 'template<class Func1, class Func2> static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType) [with Func1 = const char*; Func2 = const char*]':
    \Library1\library1.cpp:16: required from here
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:214: error: no type named 'Object' in 'struct QtPrivate::FunctionPointer<const char*>'
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:244: template<class Func1, class Func2> static typename QtPrivate::QEnableIf<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2)
    .
    .
    .
    D:\Qt-5.1.0\5.1.0\mingw48_32\include\QtCore\qobject.h:267: template<class Func1, class Func2> static typename QtPrivate::QEnableIf<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == (-1)), QMetaObject::Connection>::Type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2)
    Library1\library1.cpp:16: note:   candidate expects 3 arguments, 4 provided
    

    How can I connect a signal to the library slot?
    Thank you all


  • Moderators

    Is the slot declared as a slot? Does live in a class that inherits from QObject, and is there Q_OBJECT macro there in the header?



  • @Mohammadsm
    I have added markdown tags for code sections of post.
    Please checkout the "markdown tags" at the end of thread.



  • @sierdzio
    Yes :

    class LIBRARY1SHARED_EXPORT Library1
    {
        Q_OBJECT
    public:
        Library1();
        void Inint_Lib();
    private slots:
        void Capture_Received_Data();
    

    [edit: koahnig, code markers]



  • @koahnig
    I used code format but seems not working!



  • @Mohammadsm
    You need to use three of ` at start and end of your code block.Check the end of the whole thread here. There is a short explanation.
    Some keyboards do not support ` as a key. In this case you can go to the end of the thread and copy the three characters required and paste them where needed.


  • Moderators

    @koahnig said:

    You need to use three of ` at start and end of your code block

    No need for that. You can just make a code block by having a separate paragraph that has a four-space intendation


  • Moderators

    @Mohammadsm

    Your class does not inherit from QObject. Try this:

    class LIBRARY1SHARED_EXPORT Library1 : public QObject
    

    Then remeber to rebuild and relink the library, and your project that uses it.


  • Lifetime Qt Champion

    @sierdzio Indeed the back ticks are not mandatory, however it gives the forum engine a good hint of what is following



  • @sierdzio
    It worked by adding ": public QObject"
    Thank you, very much



  • @SGaist said:

    @sierdzio Indeed the back ticks are not mandatory, however it gives the forum engine a good hint of what is following

    in the cheat sheet you can actually find:
    Blocks of code are either fenced by lines with three back-ticks ```, or are indented with four spaces. I recommend only using the fenced code blocks -- they're easier and only they support syntax highlighting.

    The question is what is the syntax highlighting now?