Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Help with connect statement



  • Hi All,

    I'm trying to extend the CAN example (in examples - Qt Creator 4.9.1 / Qt 5.12.3) by adding a class that decodes some CAN messages and then displays the data on my own QML screen.

    I've added a message processor class, with a function under public slots:

    void pIncomingMessage(const QString message);
    

    and a signal in the 'processReceivedFrames() function in 'MainWindow.cpp' :

    void canMessageOut(const QString outGoingMessage);
    

    I haven't been able to get a connect statement to work anywhere (interpreter didn't like any variation of connect statement) until I finally put the statement in main.cpp:

    int main(int argc, char *argv[])
    {
        QLoggingCategory::setFilterRules(QStringLiteral("qt.canbus* = true"));
        QApplication a(argc, argv);    
    
        MainWindow w;
    
        QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor::pIncomingMessage);
    
        w.show();
    
        return a.exec();
    }
    

    and the interpreter doesn't throw an error, but it fails at runtime with LOTS of red ink in the compile screen. I'll put the compile output here, with the hope something makes sense to one of the gurus out there. I'm guessing the important clues have to do with the messages: 'no known conversion for argument 2'... but I have no idea what the problem could be.

    In file included from C:\Qt\5.12.3\mingw73_64\include\QtSerialBus/qcanbusdevice.h:40:0,
    from C:\Qt\5.12.3\mingw73_64\include\QtSerialBus/QCanBusDevice:1,
    from ..\monitorpBus\mainwindow.h:4,
    from ..\monitorpBus\main.cpp:1:
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h: In instantiation of 'static typename std::enable_if<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2) [with Func1 = void (MainWindow::)(QString); Func2 = void (pMessageProcessor::)(QString); typename std::enable_if<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::type = QMetaObject::Connection; typename QtPrivate::FunctionPointer<Func>::Object = MainWindow]':
    ..\monitorpBus\main.cpp:16:90: required from here
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:262:74: error: no matching function for call to 'QObject::connect(const Object*&, void (MainWindow::&)(QString), const Object&, void (pMessageProcessor::&)(QString), Qt::ConnectionType)'
    return connect(sender, signal, sender, slot, Qt::DirectConnection);
    ^
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:208:36: note: candidate: static QMetaObject::Connection QObject::connect(const QObject
    , const char*, const QObject*, const char*, Qt::ConnectionType)
    static QMetaObject::Connection connect(const QObject *sender, const char *signal,
    ^~~~~~~
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:208:36: note: no known conversion for argument 2 from 'void (MainWindow::)(QString)' to 'const char'
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:211:36: note: candidate: static QMetaObject::Connection QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
    static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal,
    ^~~~~~~
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:211:36: note: no known conversion for argument 2 from 'void (MainWindow::)(QString)' to 'const QMetaMethod&'
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:463:32: note: candidate: QMetaObject::Connection QObject::connect(const QObject
    , const char*, const char*, Qt::ConnectionType) const
    inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal,
    ^~~~~~~
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:463:32: note: candidate expects 4 arguments, 5 provided
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:228:43: note: candidate: static QMetaObject::Connection QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const typename QtPrivate::FunctionPointer<Func2>::Object*, Func2, Qt::ConnectionType) [with Func1 = void (MainWindow::)(QString); Func2 = void (pMessageProcessor::)(QString); typename QtPrivate::FunctionPointer<Func>::Object = MainWindow; typename QtPrivate::FunctionPointer<Func2>::Object = pMessageProcessor]
    static inline QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
    ^~~~~~~
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:228:43: note: no known conversion for argument 3 from 'const Object* {aka const MainWindow*}' to 'const Object* {aka const pMessageProcessor*}'
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:260:13: note: candidate: template<class Func1, class Func2> static typename std::enable_if<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2)
    connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot)
    ^~~~~~~
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:260:13: note: template argument deduction/substitution failed:
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:262:74: note: candidate expects 3 arguments, 5 provided
    return connect(sender, signal, sender, slot, Qt::DirectConnection);
    ^
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:269:13: note: candidate: template<class Func1, class Func2> static typename std::enable_if<(((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0) && (! QtPrivate::FunctionPointer<Func2>::IsPointerToMemberFunction)), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const QObject*, Func2, Qt::ConnectionType)
    connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, const QObject *context, Func2 slot,
    ^~~~~~~
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:269:13: note: template argument deduction/substitution failed:
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h: In substitution of 'template<class Func1, class Func2> static typename std::enable_if<(((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0) && (! QtPrivate::FunctionPointer<Func2>::IsPointerToMemberFunction)), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const QObject*, Func2, Qt::ConnectionType) [with Func1 = void (MainWindow::)(QString); Func2 = void (pMessageProcessor::)(QString)]':
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:262:74: required from 'static typename std::enable_if<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2) [with Func1 = void (MainWindow::)(QString); Func2 = void (pMessageProcessor::)(QString); typename std::enable_if<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::type = QMetaObject::Connection; typename QtPrivate::FunctionPointer<Func>::Object = MainWindow]'
    ..\monitorpBus\main.cpp:16:90: required from here
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:269:13: error: no type named 'type' in 'struct std::enable_if<false, QMetaObject::Connection>'
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h: In instantiation of 'static typename std::enable_if<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2) [with Func1 = void (MainWindow::)(QString); Func2 = void (pMessageProcessor::)(QString); typename std::enable_if<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::type = QMetaObject::Connection; typename QtPrivate::FunctionPointer<Func>::Object = MainWindow]':
    ..\monitorpBus\main.cpp:16:90: required from here
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:300:13: note: candidate: template<class Func1, class Func2> static typename std::enable_if<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == -1), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2)
    connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot)
    ^~~~~~~
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:300:13: note: template argument deduction/substitution failed:
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:262:74: note: candidate expects 3 arguments, 5 provided
    return connect(sender, signal, sender, slot, Qt::DirectConnection);
    ^
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:308:13: note: candidate: template<class Func1, class Func2> static typename std::enable_if<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == -1), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const QObject*, Func2, Qt::ConnectionType)
    connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, const QObject *context, Func2 slot,
    ^~~~~~~
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:308:13: note: template argument deduction/substitution failed:
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h: In substitution of 'template<class Func1, class Func2> static typename std::enable_if<(QtPrivate::FunctionPointer<Func2>::ArgumentCount == -1), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, const QObject*, Func2, Qt::ConnectionType) [with Func1 = void (MainWindow::)(QString); Func2 = void (pMessageProcessor::)(QString)]':
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:262:74: required from 'static typename std::enable_if<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::type QObject::connect(const typename QtPrivate::FunctionPointer<Func>::Object*, Func1, Func2) [with Func1 = void (MainWindow::)(QString); Func2 = void (pMessageProcessor::)(QString); typename std::enable_if<((int)(QtPrivate::FunctionPointer<Func2>::ArgumentCount) >= 0), QMetaObject::Connection>::type = QMetaObject::Connection; typename QtPrivate::FunctionPointer<Func>::Object = MainWindow]'
    ..\monitorpBus\main.cpp:16:90: required from here
    C:\Qt\5.12.3\mingw73_64\include/QtCore/qobject.h:308:13: error: no type named 'type' in 'struct std::enable_if<false, QMetaObject::Connection>'
    mingw32-make[1]: *** [Makefile.Debug:3009: debug/main.o] Error 1
    mingw32-make[1]: *** Waiting for unfinished jobs....


  • Moderators

    @mscottm said in Help with connect statement:

    QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor::pIncomingMessage);
    

    You're missing the 3rd argument: A pointer to the receiver instance


  • Lifetime Qt Champion

    @mscottm said in Help with connect statement:

    QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor::pIncomingMessage);

    This can't work: you need an instance of pMessageProcessor class (I don't see any in your code).
    Like:

    int main(int argc, char *argv[])
    {
        QLoggingCategory::setFilterRules(QStringLiteral("qt.canbus* = true"));
        QApplication a(argc, argv);    
    
        MainWindow w;
        pMessageProcessor proc;
    
        QObject::connect(&w, &MainWindow::canMessageOut, &proc, &pMessageProcessor::pIncomingMessage);
    
        w.show();
    
        return a.exec();
    }
    


  • When I add in the instance to the statement like so:

    QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
    

    the interpreter underlines it (&pMessageProcessor) and says "pMessageProcessor does not refer to a value"...then..."declared here" and points to the pMessageProcessor.h file line number where the class is declared:

    class pMessageProcessor : public QObject
    

  • Moderators

    @mscottm

    do not forget the
    Q_OBJECT
    inside your class



  • Sorry, here is the whole class .h file - it is there:

    #ifndef PMESSAGEPROCESSOR_H
    #define PMESSAGEPROCESSOR_H
    
    #include <QHash>
    #include <QObject>
    #include <QDebug>
    
    class pMessageProcessor : public QObject
    {
        Q_OBJECT    
    
    public:
        qint16 pValue;
    
        pMessageProcessor();
    
        void analogInputs(QString, QString);
    
        void analogInputsMessage(QString, qint16, qint16, qint16, qint16);
    
    public slots:
        void pIncomingMessage(QString message);
    
    signals:
        void analogInputsValue( QString pgn, QString sourceAddress, qint16 analogInputOne,
                                qint16 analogInputTwo, qint16 analogInputThree, qint16 analogInputFour);
    
    private:
        bool ok;
        int pgnTest;
        QString message1;
        QString message2;
        QStringList parts;
        QString leftPart;
        QString pgn;
        QString data;
        QString srcAddress;
    
        QHash<QString, int> pHash;
    
    };
    
    #endif // PMESSAGEPROCESSOR_H
    


  • @mscottm said in Help with connect statement:

    QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);

    In addition to @J-Hilk :

    If pMessageProcessor is already a pointer to your class instance, then you have to remove the &.

    MainWindow w is referenced by using &w in your connect-statement because it is NOT a pointer.

    Try:

    pMessageProcessor * pMProc = new pMessageProcessor();
    // [ ... ]
    QObject::connect(&w, &MainWindow::canMessageOut, pMProc, &pMessageProcessor::pIncomingMessage);
    

    Sender param != class of sender (you need the actual instance of that class there)



  • @pl45m4
    I think it should be
    connect(&w, &MainWindow::canMessageOut, mp, &pMessageProcessor::pIncomingMessage);
    right ?
    from this line pMessageProcessor * mp = new pMessageProcessor();

    Please tell if i am missing something.



  • @pradeep-p-n

    After re-reading my "code" I realized that I messed it up :)
    (Realized too late that pMessageProcessor is not the instance, but the class name -> low case first letter?!)
    I've edited my answer....

    I hope OP gets the point



  • @pl45m4
    Cool,

    All the best.



  • Error remains the same: "pMessageProcessor does not refer to a value".

    I will also say that when I move the connect statement into 'MainWindow.cpp', like so:

    QObject::connect(this, processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
    

    the error is the same ..."pMessageProcessor does not refer to a value". And when I move the statement into the pMessageProcessor.cpp file like so:

    connect(&MainWindow, &MainWindow::processReceivedFrames, &pMessageProcessor, pMessageProcessor::pIncomingMessage());
    

    the interpreter has three errors: "MainWindow does not refer to a value", "pMessageProcessor does not refer to a value", "cpp:11: error: too few arguments to function call, single argument 'message' was not specified"



  • @mscottm

    Take a look at my (edited) answer.

    Again:

    // a and b = no pointer
    Sender a;
    Receiver b;
    // Take address from a and b with &
    connect(&a, &Sender::signal, &b, &Receiver::slot);
    

    or

    //  a and b = pointers
    Sender * a = new Sender();
    Receiver * b = new Receiver();
    // NO & in connection
    connect(a, &Sender::signal, b, &Receiver::slot);
    

    If you mix a pointer with &, your connection cannot work :)

    Here's another example :)
    https://doc.qt.io/qt-5/signalsandslots.html#a-small-example



  • @pl45m4 said in Help with connect statement:

    If you mix a pointer with &, your connection cannot work :)

    Are you sure of this ?

    I did try a sample, it works.
    QObject::connect(&w, &Widget::sendMsg, mtc, &MyTestClass::receiveMsg);

    • main.cpp
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        Widget w;
        MyTestClass *mtc = new MyTestClass;
    
        QObject::connect(&w, &Widget::sendMsg, mtc, &MyTestClass::receiveMsg);
    
        return a.exec();
    }
    
    • Widget Class
    // .h File
    signals:
        void sendMsg(const QString msg);
    
    // .cpp File
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
        , m_Count(0)
        , m_testTimer(new QTimer(this))
    {
        m_testTimer->start();
        m_testTimer->setInterval(3000);
    
        connect(m_testTimer, &QTimer::timeout, [this]{
            m_Count++;
            emit sendMsg(" Number Sent :: " + QString::number(m_Count));
        });
    }
    
    • MyTestClass
    // .h File
    public slots:
        void receiveMsg(const QString msg);
    
    //.cpp File
    void MyTestClass::receiveMsg(const QString msg)
    {
        qDebug() << Q_FUNC_INFO << msg;
    }
    
    

    Output:

    void MyTestClass::receiveMsg(QString) " Number Sent :: 1"
    void MyTestClass::receiveMsg(QString) " Number Sent :: 2"
    void MyTestClass::receiveMsg(QString) " Number Sent :: 3"
    void MyTestClass::receiveMsg(QString) " Number Sent :: 4"
    void MyTestClass::receiveMsg(QString) " Number Sent :: 5"
    void MyTestClass::receiveMsg(QString) " Number Sent :: 6"
    

  • Lifetime Qt Champion

    @pradeep-p-n what @Pl45m4 was warning against is using & when the target is already a pointer to an object.



  • Ah, how did i misunderstood that.
    Oops @Pl45m4 i got it.
    @SGaist Thanks.



  • I've tried many variations of the above thoughts - no version of the connect statement works. Here's a little of what I've tried (these are not the only variations).

    When in main.cpp:

    QObject::connect(&w, &MainWindow::canMessageOut, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
    

    interpreter: error: 'pMessageProcessor' does not refer to a value

    QObject::connect(w, &MainWindow::canMessageOut, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
    

    interpreter: error: 'pMessageProcessor' does not refer to a value

    QObject::connect(w, MainWindow::canMessageOut, pMessageProcessor, pMessageProcessor::pIncomingMessage);
    

    interpreter:
    error: call to non-static member function without an object argument,
    error: 'pMessageProcessor' does not refer to a value,
    error: call to non-static member function without an object argument

    Connect statement in MainWindow.cpp:

    QObject::connect(this, processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
    

    interpreter: error: 'pMessageProcessor' does not refer to a value

    QObject::connect(this, processReceivedFrames, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
    

    interpreter: error: 'pMessageProcessor' does not refer to a value

    QObject::connect(this, processReceivedFrames, pMessageProcessor, pMessageProcessor::pIncomingMessage);
    

    interpreter:
    error: 'pMessageProcessor' does not refer to a value
    error: call to non-static member function without an object argument

    and so on...

    connect statement in pMessageProcessor.cpp:

    connect(&MainWindow, &MainWindow::processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);
    

    interpreter:
    error: 'MainWindow' does not refer to a value
    error: 'pMessageProcessor' does not refer to a value

    connect(MainWindow, &MainWindow::processReceivedFrames, pMessageProcessor, &pMessageProcessor::pIncomingMessage);
    

    interpreter:
    error: 'MainWindow' does not refer to a value
    error: 'pMessageProcessor' does not refer to a value

    and so on...

    I don't think that the connect statement is the issue, but I'm too much of a beginner to know what else to look for in the code.

    I appreciate the help so far!

    Oh - and if it's not clear - I'm trying the connect statement in one place only - not in three places.


  • Lifetime Qt Champion

    Hi @mscottm,

    it seems you have a fundamental understanding problem, and that in turn will make it hard for others to understand your problem, because the description is already wrong.

    and the interpreter doesn't throw an error, but it fails at runtime with LOTS of red ink in the compile screen.

    You probably mean: the code model does not show errors, but the program does not compile and the compile output shows LOTS of errors.

    Runtime, however, comes after your program successfully compiled and linked - when your program is actually running.

    Regarding your real problem: Why don't you just show your code? We can guess for long here, or simply have a look at it. It can either take long or you can get an answer very quick - it just depends on you.

    connect(&MainWindow, &MainWindow::processReceivedFrames, &pMessageProcessor, &pMessageProcessor::pIncomingMessage);

    You need to understand that the first and third parameter of connect need to be a pointer to a variable, while the second and the fourth need to be a pointer to a member function (the second: a signal, the fourth: a slot).

    In your example the parameters one and three are a class, not a variable, therefore it will not compile.

    Example how it could look like:

    MainWindow window;
    pMessageProcessor processor;
    connect(&window, &MainWindow::processReceivedFrames, 
    &processor, &pMessageProcessor::pIncomingMessage);
    

    As you can see, for the connect you need to have both classes as variable, and then it will work.

    Regards



  • @mscottm

    Your last reply gives the impression that you didn't read our answers to your problem.
    In this thread are at least 2-3 helpful explanations (with given code examples) how the connection statement works and how you can connect your signal with your function in your class.

    pMessageProcessor is the name of your class and is NOT a valid first or third parameter in your connect statement, neither as pMessageProcessor nor as &pMessageProcessor.
    You need the object / instance of this class there.

    If your code is compilable and you use your pMessageProcessor class, then there must be an object of this class.

    The same for MainWindow (as first / third parameter).



  • @JKSH @jsulm @J-Hilk @Pl45m4 @Pradeep-P-N @SGaist @aha_1980

    My sincere apologies. This is what I get for trying to solve coding problems when in a hurry, and just skimming the very helpful replies. I was able to solve the connect statement issue based on help given in one of the very first posts by @jsulm - no instance of the class, and @aha_1980 and @Pl45m4, you of course nailed the other issue.

    Thank you all for your replies and taking the time to help!

    Best regards


Log in to reply