Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Bad connection between signal and slot
Forum Updated to NodeBB v4.3 + New Features

[Solved] Bad connection between signal and slot

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    silep
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      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

      (Z(:^

      1 Reply Last reply
      0
      • S Offline
        S Offline
        silep
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          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.

          (Z(:^

          1 Reply Last reply
          0
          • S Offline
            S Offline
            silep
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved