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

How to emit signal using a QString?



  • I have a signal name thats passed in a QString, is there a way to emit the signal using the content of the QString, e.g. my string contains the name of the signal:

        QString strSignal("jsonTextChanged");
    

    I want to use this to emit a signal:

        emit strSignal(parameters);

  • Lifetime Qt Champion



  • Thank you, I'm having difficulty using this:

        QMetaObject::invokeMethod(pobjOriginator
                                 ,objcpyJSON[clsXMLnode::mscszAttrSignal]
                                 ,Qt::DirectConnection, Q_ARG(QJsonObject, objcpyJSON));
    

    pobjOriginator is a pointer to my class that is derived from QObject.
    mscszAttrSignal contains "signal"
    objcpyJSON contains a signal string that contains "jsonTextChanged"
    The prototype for this signal is:

        void jsonTextChanged(QJsonObject objJSON);
    

    The message displayed in Qt Creator to the right of the invokeMethod call is:

        no matching function for call to 'invokeMethod'

  • Lifetime Qt Champion

    @SPlatten What type is objcpyJSON?
    "mscszAttrSignal contains "signal"" - what does that mean? Do you mean mscszAttrSignal is a signal?
    Please post the whole error message, I guess there is no such overload of invokeMethod...


  • Moderators

    @SPlatten

    This does work for me

    connect(this, &MainWindow::mySignal, this, [=]()->void{qDebug() << " Tada";});
        QString string("mySignal");
    
        QMetaObject::invokeMethod(this, string.toLocal8Bit());
    

    Is this what you're looking for?



  • objcpyJSON:

        QJsonObject objcpyJSON(*pobjJSON);
        objcpyJSON.insert(clsXMLnode::mscszAttrSubscriberID, strIDandAttr);
    

    Its a copy of a passed in object of the same type with an additional member added:

    clsXMLnode::mscszAttrSubscriberID is a char array containing "sid"

    objcpyJSON contains:

        "id"        txted1            QJsonValue (String)
        "sid"       win2:title        QJsonValue (String)
        "signal"    jsonTextChanged   QJsonValue (String)
        "text"      simon             QJsonValue (String)


  • I'm guessing objcpyJSON is a QJsonObject , and objcpyJSON[clsXMLnode::mscszAttrSignal] should be a string of the signal name?

    QMetaObject::invokeMethod(pobjOriginator
                              , objcpyJSON[clsXMLnode::mscszAttrSignal].toString().toUtf8()
                              , Q_ARG(QJsonObject, objcpyJSON));
    

  • Lifetime Qt Champion

    @SPlatten said in How to emit signal using a QString?:

    QJsonObject objcpyJSON(*pobjJSON)

    Please read the documentation for invokeMethod -it does not have an overload which takes a QJsonObject as parameter...
    See what @Bonnie wrote.



  • Thank you, @Bonnie, this works:

        QJsonObject objcpyJSON(*pobjJSON);
        objcpyJSON.insert(clsXMLnode::mscszAttrSubscriberID, strIDandAttr);
        QMetaObject::invokeMethod(pobjOriginator->pobjGetWidget()
                                 ,objcpyJSON[clsXMLnode::mscszAttrSignal].toString().toUtf8()
                                 ,Q_ARG(QJsonObject, objcpyJSON));

Log in to reply