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

Using signals and slots in a static method?



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


  • Lifetime Qt Champion

    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.



  • @SGaist
    Yes, but these are compilation errors!?


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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


Log in to reply