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. How to emit signal using a QString?
Qt 6.11 is out! See what's new in the release blog

How to emit signal using a QString?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 3.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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    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);
    

    Kind Regards,
    Sy

    jsulmJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      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);
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      emit strSignal(parameters);

      Not in C++.
      Use https://doc.qt.io/qt-5/qmetaobject.html#invokeMethod

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #3

        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'
        

        Kind Regards,
        Sy

        jsulmJ J.HilkJ 2 Replies Last reply
        0
        • SPlattenS SPlatten

          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'
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • SPlattenS SPlatten

            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'
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

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


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            0
            • SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #6

              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)
              

              Kind Regards,
              Sy

              jsulmJ 1 Reply Last reply
              0
              • B Offline
                B Offline
                Bonnie
                wrote on last edited by
                #7

                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));
                
                1 Reply Last reply
                5
                • SPlattenS SPlatten

                  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)
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #9

                    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));
                    

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    1

                    • Login

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