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. Check in C++ whether signal in QML file exists.
Forum Updated to NodeBB v4.3 + New Features

Check in C++ whether signal in QML file exists.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 1.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.
  • C Offline
    C Offline
    ChrisTof
    wrote on last edited by
    #1

    Read this post, but it is not helpful: Signal exists
    I wonder whether it is possible to detect whether signal in QML files exists before connecting it in C++.
    I create a qml component in c++ and try to connect a signal between qml and c++:

    QQmlComponent component(qmlEngine, QUrl::fromLocalFile("example.qml"));
        if(component.isError()) {
            return;
        }
    
        QQmlContext* rootItemContext = QQmlEngine::contextForObject(rootWindow);
        QQuickItem *object = qobject_cast<QQuickItem*>(component.beginCreate(rootItemContext));
        if(object != nullptr) {
            QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
            object->setParentItem(rootWindow->contentItem());
            object->setParent(qmlEngine);
    
            //if(SIGNAL(generateEvent(QVariant, QString) exists)
            QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
    
            component.completeCreate();
    }
    

    Is it possible to check (during run-time) if signal in qml file exists before connecting it?

    J.HilkJ 1 Reply Last reply
    0
    • J.HilkJ J.Hilk

      @ChrisTof said in Check in C++ whether signal in QML file exists.:

      QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));

      you're already using Qt4 Syntax so the connection will be created at run time, simply store the return value of QObject::connect and check if it was successful

      QMetaObject::Connection c = QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
      if(c){
         //Successful connected
      }
      
      C Offline
      C Offline
      ChrisTof
      wrote on last edited by
      #5

      Thank you guys, that is working solution for me:

      int signalExists = object->metaObject()->indexOfSignal(QMetaObject::normalizedSignature("generateEvent(QVariant, QString)"));
      if(signalExists != -1)
          QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
      

      I had to pass full signature with arguments: (QVariant, QString), without them signal was not found.

      @J-Hilk
      Yes, it is a working solution but I want to check if the signal exists before connecting, because I want to get rid of warning in case the signal does not exist.

      Pablo J. RoginaP 1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #2

        You can check from Metaobject. You can see the method indexOfSignal(..).
        Also i'm not sure why do you want to do this.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        2
        • KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #3

          @ChrisTof said in Check in C++ whether signal in QML file exists.:

          Is it possible to check (during run-time) if signal in qml file exists before connecting it?

          You can check this QMetaObject, like this:

           index = object->metaObject()->indexOfSlot(QMetaObject::normalizedSignature("generateEvent"));
              if (index == -1) {
                  qWarning("Wrong slot name!");
              }
              else {
                  // do connect...
              }
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          1 Reply Last reply
          0
          • C ChrisTof

            Read this post, but it is not helpful: Signal exists
            I wonder whether it is possible to detect whether signal in QML files exists before connecting it in C++.
            I create a qml component in c++ and try to connect a signal between qml and c++:

            QQmlComponent component(qmlEngine, QUrl::fromLocalFile("example.qml"));
                if(component.isError()) {
                    return;
                }
            
                QQmlContext* rootItemContext = QQmlEngine::contextForObject(rootWindow);
                QQuickItem *object = qobject_cast<QQuickItem*>(component.beginCreate(rootItemContext));
                if(object != nullptr) {
                    QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);
                    object->setParentItem(rootWindow->contentItem());
                    object->setParent(qmlEngine);
            
                    //if(SIGNAL(generateEvent(QVariant, QString) exists)
                    QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
            
                    component.completeCreate();
            }
            

            Is it possible to check (during run-time) if signal in qml file exists before connecting it?

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #4

            @ChrisTof said in Check in C++ whether signal in QML file exists.:

            QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));

            you're already using Qt4 Syntax so the connection will be created at run time, simply store the return value of QObject::connect and check if it was successful

            QMetaObject::Connection c = QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
            if(c){
               //Successful connected
            }
            

            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.

            C 1 Reply Last reply
            2
            • J.HilkJ J.Hilk

              @ChrisTof said in Check in C++ whether signal in QML file exists.:

              QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));

              you're already using Qt4 Syntax so the connection will be created at run time, simply store the return value of QObject::connect and check if it was successful

              QMetaObject::Connection c = QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
              if(c){
                 //Successful connected
              }
              
              C Offline
              C Offline
              ChrisTof
              wrote on last edited by
              #5

              Thank you guys, that is working solution for me:

              int signalExists = object->metaObject()->indexOfSignal(QMetaObject::normalizedSignature("generateEvent(QVariant, QString)"));
              if(signalExists != -1)
                  QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
              

              I had to pass full signature with arguments: (QVariant, QString), without them signal was not found.

              @J-Hilk
              Yes, it is a working solution but I want to check if the signal exists before connecting, because I want to get rid of warning in case the signal does not exist.

              Pablo J. RoginaP 1 Reply Last reply
              0
              • C ChrisTof

                Thank you guys, that is working solution for me:

                int signalExists = object->metaObject()->indexOfSignal(QMetaObject::normalizedSignature("generateEvent(QVariant, QString)"));
                if(signalExists != -1)
                    QObject::connect(object, SIGNAL(generateEvent(QVariant, QString)), this, SLOT(receiveEventFromQml(QVariant, QString)));
                

                I had to pass full signature with arguments: (QVariant, QString), without them signal was not found.

                @J-Hilk
                Yes, it is a working solution but I want to check if the signal exists before connecting, because I want to get rid of warning in case the signal does not exist.

                Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on last edited by
                #6

                @ChrisTof said in Check in C++ whether signal in QML file exists.:

                I want to get rid of warning in case the signal does not exist.

                If you're using Qt 5, go and use the signal & slot new syntax. You'll have error checking at compile-time for free...

                Counter a, b;
                QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
                

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                J.HilkJ 1 Reply Last reply
                0
                • Pablo J. RoginaP Pablo J. Rogina

                  @ChrisTof said in Check in C++ whether signal in QML file exists.:

                  I want to get rid of warning in case the signal does not exist.

                  If you're using Qt 5, go and use the signal & slot new syntax. You'll have error checking at compile-time for free...

                  Counter a, b;
                  QObject::connect(&a, &Counter::valueChanged, &b, &Counter::setValue);
                  
                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #7

                  @Pablo-J-Rogina
                  I don't think Qt5 Syntax works on signals that you define in the QML file only with signals defined in the underlaying c++ class.

                  However, I'm not entirely sure! 🤷‍♂️


                  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

                  • Login

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