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. Help with connect statement
Forum Updated to NodeBB v4.3 + New Features

Help with connect statement

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 8 Posters 3.7k Views 3 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.
  • Pradeep P NP Pradeep P N

    @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.

    Pl45m4P Offline
    Pl45m4P Offline
    Pl45m4
    wrote on last edited by Pl45m4
    #9

    @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


    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

    ~E. W. Dijkstra

    Pradeep P NP 1 Reply Last reply
    0
    • Pl45m4P Pl45m4

      @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

      Pradeep P NP Offline
      Pradeep P NP Offline
      Pradeep P N
      wrote on last edited by Pradeep P N
      #10

      @pl45m4
      Cool,

      All the best.

      Pradeep Nimbalkar.
      Upvote the answer(s) that helped you to solve the issue...
      Keep code clean.

      1 Reply Last reply
      2
      • M Offline
        M Offline
        MScottM
        wrote on last edited by
        #11

        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"

        Pl45m4P 1 Reply Last reply
        0
        • M MScottM

          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"

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #12

          @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


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          Pradeep P NP 1 Reply Last reply
          4
          • Pl45m4P Pl45m4

            @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

            Pradeep P NP Offline
            Pradeep P NP Offline
            Pradeep P N
            wrote on last edited by Pradeep P N
            #13

            @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"
            

            Pradeep Nimbalkar.
            Upvote the answer(s) that helped you to solve the issue...
            Keep code clean.

            SGaistS 1 Reply Last reply
            0
            • Pradeep P NP Pradeep P N

              @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"
              
              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #14

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              Pradeep P NP 1 Reply Last reply
              2
              • SGaistS SGaist

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

                Pradeep P NP Offline
                Pradeep P NP Offline
                Pradeep P N
                wrote on last edited by
                #15

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

                Pradeep Nimbalkar.
                Upvote the answer(s) that helped you to solve the issue...
                Keep code clean.

                1 Reply Last reply
                2
                • M Offline
                  M Offline
                  MScottM
                  wrote on last edited by MScottM
                  #16

                  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.

                  aha_1980A Pl45m4P 2 Replies Last reply
                  0
                  • M MScottM

                    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.

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by aha_1980
                    #17

                    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

                    Qt has to stay free or it will die.

                    1 Reply Last reply
                    2
                    • M MScottM

                      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.

                      Pl45m4P Offline
                      Pl45m4P Offline
                      Pl45m4
                      wrote on last edited by Pl45m4
                      #18

                      @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).


                      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                      ~E. W. Dijkstra

                      1 Reply Last reply
                      2
                      • M Offline
                        M Offline
                        MScottM
                        wrote on last edited by
                        #19

                        @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

                        1 Reply Last reply
                        5

                        • Login

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