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. Signal exists
QtWS25 Last Chance

Signal exists

Scheduled Pinned Locked Moved General and Desktop
14 Posts 3 Posters 6.6k Views
  • 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.
  • O Offline
    O Offline
    onek24
    wrote on last edited by
    #1

    Hello,

    is it possible to determine weather a signal exists or not? For example i would like to iterate trough all QML-rootObjects and connect a signal to my Cpp slot.

    Declaration
    @QHash<QString, QPointer<QObject> > _objectList;
    QPointer<QObject> _parent;@
    Function
    @if(_parent.isNull())
    return;
    connect(_parent, SIGNAL(changed(QVariant)), this, SIGNAL(componentChanged(QVariant)));
    QList<QObject*> list = _parent.data()->findChildren<QObject*>();
    for(QList<QObject*>::const_iterator it(list.constBegin());it!=list.constEnd();++it)
    {
    if((*it)->objectName() != ""){
    _objectList.insert((*it)->objectName(), *it);
    connect(*it, SIGNAL(changed(QVariant)), this, SIGNAL(componentChanged(QVariant)));
    }
    }@
    But i don't really want/need the console output for "No such signal".

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on last edited by
      #2

      You could try to cast to the Class that offers the signal and if the cast fails you know that you can't connect to the signal the class offers.
      @
      MyClass : public QObject
      {
      Q_OBJECT
      signals:
      void mySignal();
      };

      someFunction()
      {
      if (dynamic_cast<MyClass*>(_parent))
      {
      connect(_parent, SIGNAL(changed(QVariant)), this, SLOT(componentChanged(QVariant)));
      }
      }
      @

      Of course that only works if you know the class beforehand.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        onek24
        wrote on last edited by
        #3

        Thanks, i'll give it a try.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KA51O
          wrote on last edited by
          #4

          Little correction. I think for the connect to work you also have to cast there, so the code should be changed to this:
          @
          someFunction()
          {
          MClass* myClass = dynamic_cast<MyClass*>(_parent);
          if (myClass)
          {
          connect(myClass, SIGNAL(changed(QVariant)), this, SLOT(componentChanged(QVariant)));
          }
          }
          @

          Also instead of dynamic_cast you can use qobject_cast

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

            Hi,

            In the absolute you don't need to pass the casted pointer to this version of connect, but in practice it's cleaner to do so and also more readable.

            casting QObject class should always be done using qobject_cast

            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
            0
            • O Offline
              O Offline
              onek24
              wrote on last edited by
              #6

              Looks like i am misunderstanding something, doesn't the cast just check whether the object exists or not? Because the objects i am iterating trough obviously exists but there might be or might not be a signal named changed and that is what i want to find out. If the signal exists in the given objects or not.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                KA51O
                wrote on last edited by
                #7

                The cast checks if the instance (the QObject pointer) can be casted to another class pointer (eg. QObject* to QWidget*). So if you know that the signal you're looking for is only in the class QWidget and not in the class QObject you can use the cast to test for the signal by implication (class A, the base class, does not have the signal; class B the derived class has the signal).

                Example:
                @
                QObject* a;
                QWidget* b;
                QPushButton* c;
                QObject* d;

                b = new QWidget();
                a = b;
                c = new QPushButton();
                d = c
                if(qobject_cast<QWidget*>(a))
                {
                // this works
                }
                if(qobject_cast<QWidget*>(d ))
                {
                // this never works
                }
                @

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  onek24
                  wrote on last edited by
                  #8

                  All the objects are QObjects so im still not sure how this might help me, sorry. I've got just a list of QObjects and i want to check if there is a signal going outwards this QObject or not.

                  • Edit -
                    I think ill work in, kind of unlogical for me at the moment why a QObject cast should determine weather a custom signal exists or not.

                  • Edit 2 -
                    I'm not sure if this is what i am searching for, let me discribe my problem a litte bit further:

                  • I've got a QML file with several Components.
                  • Some of these Components might have a custom changed signal declared.
                  • I am iterating trough all Components(or QObjects) which are children of my top-level item.
                  • Determine whether this Component has got a signal named changed or not
                  • If it has got a changed signal it should connect the signal to a given slot.
                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KA51O
                    wrote on last edited by
                    #9

                    If you look at "the QObject documentation":http://qt-project.org/doc/qt-4.8/qobject.html#signals you will see that QObject has no signal changed(QVariant). So the only way you can have the signal is to have a QObject derived class that implements the signal, right?

                    By casting to this class (or QML component?) you can check if the QObject pointer you have is in fact a pointer to this class you're looking for (which offers the signal).

                    I have not used QML yet so maybe I'm missing something.

                    --Edit--
                    qobject_cast does not check if the pointer is a QObject it tries to cast a pointer to an QObject derived class. If it cannot cast the pointer to a specific QObject derived class it returns a NULL pointer.

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      onek24
                      wrote on last edited by
                      #10

                      Well basically each component in QML is a QObject. You can simply write:
                      @signal changed()@
                      This will create a signal in QML for the given Component. Still it is the same QObject. Isn't there a more "default" way to check for an existing signal, or maybe even catch the "no such signal" exception if it is one.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        KA51O
                        wrote on last edited by
                        #11

                        Hmm maybe the name of the component is like the name of the QObject derived class in C++? Just a guess.

                        1 Reply Last reply
                        0
                        • O Offline
                          O Offline
                          onek24
                          wrote on last edited by
                          #12

                          Well each component from QML is casted to a QObject Pointer. @QList<QObject*> list = _parent.data()->findChildren<QObject*>();@
                          But sure the Components derive from QObject. Still the signal is a custom signal and doesn't affect the type of the Component.
                          I might be wrong, still i am new to C++, Qt and QML.

                          A more specific QML example:
                          @import QtQuick 2.0

                          Rectangle {
                          width: 360
                          height: 360
                          signal changed()
                          Text {
                          text: qsTr("Hello World")
                          anchors.centerIn: parent
                          }
                          MouseArea {
                          anchors.fill: parent
                          onClicked: {
                          changed()
                          }
                          }
                          }@

                          I've got a signal changed() in my top-level component. On click of the MouseArea it emits the signal. Rectangle and MouseArea are default Qt(QML) Components.

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            KA51O
                            wrote on last edited by
                            #13

                            Have you tried if casting to Rectangle works?

                            1 Reply Last reply
                            0
                            • O Offline
                              O Offline
                              onek24
                              wrote on last edited by
                              #14

                              Im not sure if it is possible to cast it to a Rectangle, QML is only interpreted.

                              Also please keep in mind that i am new to developement and still in education. Thanks for your time and help.

                              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