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

[Solved] Bad connection between signal and slot



  • Hallo,

    I'm trying to connect a signal with a slot, to use this slot in another .cpp file. I already did that before and it worked, but this time something is wrong, and I don't know why.. I write here the important lines:

    ctrlMachine.h:

    @#include "mainform.h"
    #include "lensInfos.h"

    class ctrlMachine : public QObject
    {
    Q_OBJECT

    public:
    ctrlMachine();
    ~ctrlMachine();

    protected:
    lensInfos* lens;
    };@

    ctrlMachine.cpp:

    @#include "ctrlMachine.h"

    ctrlMachine::ctrlMachine()
    { lens = new lensInfos; }
    ctrlMachine::~ctrlMachine()
    { delete lens; }

    //In the initialization function:

    connect(lens, SIGNAL( signalMsg(QString, QString) ), window, SLOT(onLensInfos_info_msg(QString, QString)));@

    mainform.cpp:

    @#include "mainform.h"
    #include "ctrlMachine.h"

    void MainForm::onLensInfos_info_msg(QString msg, QString type_msg)
    { writeErrorMsg(msg, type_msg); }@

    lensInfos.h:

    @#include "mainform.h"

    class lensInfos : public QObject
    {
    Q_OBJECT
    signals:
    void signalMsg( QString msg, QString type_msg );
    };@

    lensInfos.cpp:

    @#include "lensInfos.h"

    //In every function:
    emit signalMsg(">> Parameter ok", "type_ready");@

    Would anyone have an idea about what could be wrong? Thank you in advance


  • Moderators

    You should be connecting in MainForm (creates better code flow and structure, not really an error)

    You should be using const references for Qt parameters (const QString&parameter) - this way you are conserving memory better

    You do not need to delete the lens object in ctrlMachine: it is a QObject, so you should assign ctrlMachine as a parent and then the object will be deleted automatically for you

    Use standard QObject constructors (that is, add QObject *parent parameters to all constructors of classes inheriting from QObject)

    please tell us what is wrong, we do not want to guess. Any error messages? Meta object problems? Is anything crashing? The code itself looks OK



  • Thanks for your reply.
    There is no error, no crash, nothing special. The only thing is that the information message, produced by the function writeErrorMsg, isn't shown in my Qt window (in my QTextEdit)..
    I think that the signal signalMsg is sent, but the connection between it and the slot onLensInfos_info_msg doesn't work, because nothing happens.


  • Moderators

    Please check the console output. If Qt connections fail for any reasons, the meta object system will print a message with error description to stderr. This can be easily observed in Qt Creator, which prints all application output into Output pane at runtime.

    If there is no message there, you need to debug your code to see what is really happening.



  • There is just a memory leak long message. I don't know if it helps. I'm trying to debug my code.


Log in to reply