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. Using signals and slots in a static method?
Qt 6.11 is out! See what's new in the release blog

Using signals and slots in a static method?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.2k 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.
  • G Offline
    G Offline
    graniteDev
    wrote on last edited by graniteDev
    #1

    I am making a static method that can be used across our code base. It is intended to be able to restart a motion controller, ping the motion controllers address, and once a response is recieved, return "true"

    However, since it's a static class, I can't get the connection to work.

    bool GmasManager::gmasSoftReset()
    {
        bool gmasRestarted = false;
        QProcess p;
        QStringList args;
        args << "-t" << GmasIP;
        QObject::connect(&p, &QProcess::readyReadStandardOutput, [=]
        {
            QString s = p.readAllStandardOutput();
            qDebug() << s;
            if(s.contains("bytes"))
            {
                p.close();
                gmasRestarted = true;
            }
        });
        p.start("ping", args);
        return gmasRestarted;
    }
    

    However I'm getting a list of errors with this code:

    error: C2248: 'QProcess::QProcess' : cannot access private member declared in class 'QProcess'
    
    error: C2662: 'QByteArray QProcess::readAllStandardOutput(void)' : cannot convert 'this' pointer from 'const QProcess' to 'QProcess &'
    Conversion loses qualifiers
    
    error: C2662: 'void QProcess::close(void)' : cannot convert 'this' pointer from 'const QProcess' to 'QProcess &'
    Conversion loses qualifiers
    
    error: C3491: 'gmasRestarted': a by-value capture cannot be modified in a non-mutable lambda
    

    I'm completely baffled. I have used this sort of logic in other places in the code without issue. I need some way to read from the standard out to determine when the motor controller restarts, but I can't seam to do that in a static method.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      From the looks of it, you should rather use QProcess::waitForFinished.

      Because currently, you are going to return from your function probably before the lambda has been called.

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

      JonBJ G 2 Replies Last reply
      3
      • SGaistS SGaist

        Hi,

        From the looks of it, you should rather use QProcess::waitForFinished.

        Because currently, you are going to return from your function probably before the lambda has been called.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @SGaist
        Yes, but these are compilation errors!?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          He passes the context by value. QProcess is a QObject, therefore it can't be copied. Hence the error his getting.

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

          1 Reply Last reply
          3
          • SGaistS SGaist

            Hi,

            From the looks of it, you should rather use QProcess::waitForFinished.

            Because currently, you are going to return from your function probably before the lambda has been called.

            G Offline
            G Offline
            graniteDev
            wrote on last edited by
            #5

            @SGaist Actually, my process will carry on forever because of how I set ping up. I just want to kill the process as soon as I receive bytes back from the ping. It's not a fixed amount of time. Various factors affect the restart time of the controllers from machine to machine, so I want to set a very long process time out, and just wait to hear when bytes are received.

            I have not yet added that functionality because I'm just trying to get the signal working at this point.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The way you wrote it won't do what you expect. Your p object will get destroyed at the end of gmasSoftReset .

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

              1 Reply Last reply
              2

              • Login

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