Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Question about signal and slot
Forum Updated to NodeBB v4.3 + New Features

Question about signal and slot

Scheduled Pinned Locked Moved Solved Mobile and Embedded
9 Posts 4 Posters 1.1k 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.
  • Bhushan_SureB Offline
    Bhushan_SureB Offline
    Bhushan_Sure
    wrote on last edited by Bhushan_Sure
    #1

    firstclass.h

    #ifndef FIRSTCLASS_H
    #define FIRSTCLASS_H
    
    #include <QObject>
    
    class FirstClass : public QObject
    {
        Q_OBJECT
    public:
        explicit FirstClass(QObject *parent = nullptr);
    
    signals:
        void signalfirst();
    
    public slots:
        void displayfirst();
    
    };
    

    firstclass.cpp

    #include "firstclass.h"
    #include "secondclass.h"
    #include <QDebug>
    
    FirstClass::FirstClass(QObject *parent) : QObject(parent)
    {
    
    }
    
    void FirstClass::displayfirst()
    {
       qDebug()<<"Display First";
       emit signalfirst();
      SecondClass scnd;
    scnd.displaysecond();
    }
    

    Secondclass.h

    #ifndef SECONDCLASS_H
    #define SECONDCLASS_H
    
    #include <QObject>
    
    class SecondClass : public QObject
    {
        Q_OBJECT
    public:
        explicit SecondClass(QObject *parent = nullptr);
        void displaysecond();
    
    signals:
        void signalSecond();
    
    public slots:
    
    };
    
    #endif // SECONDCLASS_H
    

    Secondclass.cpp

    #include "secondclass.h"
    #include <QDebug>
    
    SecondClass::SecondClass(QObject *parent) : QObject(parent)
    {
    
    }
    
    void SecondClass::displaysecond()
    {
        qDebug()<<"Display Second";
        emit signalSecond();
    }
    

    main.cpp

    #include <QQmlContext>
    #include <firstclass.h>
    #include <secondclass.h>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        qmlRegisterType<SecondClass>("com.mycompany.qmlcomponents", 1, 0, "Secondclasssignal");
        FirstClass first;
    
        QQmlApplicationEngine engine;
        engine.rootContext()->setContextProperty("bhushan",&first);
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    main.qml

    import QtQuick 2.9
    import QtQuick.Window 2.2
    import com.mycompany.qmlcomponents 1.0
    
    Rectangle
        {
            width: parent.width
            height: parent.height
            color: "red"
    
            MouseArea
            {
                anchors.fill: parent
                onClicked:
                {
                    bhushan.displayfirst()
                }
            }
    
            Connections
            {
                target: bhushan
                onSignalfirst:
                {
                    console.log("first came")
                }
            }
    
            Secondclasssignal
            {
                onSignalSecond:
                {
                    console.log("Second came")
                }
            }
        }
    

    I am able to get signal of first class, But i am not able to get signal of second class,
    Is there any solution or explanation why it is so ?

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

      Hi,

      If you are talking about the second class object you create in displayfirst, it's not connected anywhere so there's no reason for anything to receive the signal.

      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
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        In addition to what @SGaist said, where do you want the signal of Object of SecondClass ? You are just creating second object in display(..) method. This is object gets destroyed once the function call is over. Second object is not exposed to QML. Second object emits signal. However you have not connected or handling this signal anywhere. No destructors exist in any of the classes. This is making you think something else becz you are not noticing the destruction of objects.

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

        Bhushan_SureB 1 Reply Last reply
        2
        • dheerendraD dheerendra

          In addition to what @SGaist said, where do you want the signal of Object of SecondClass ? You are just creating second object in display(..) method. This is object gets destroyed once the function call is over. Second object is not exposed to QML. Second object emits signal. However you have not connected or handling this signal anywhere. No destructors exist in any of the classes. This is making you think something else becz you are not noticing the destruction of objects.

          Bhushan_SureB Offline
          Bhushan_SureB Offline
          Bhushan_Sure
          wrote on last edited by
          #4

          @dheerendra @SGaist Sir, How can i expose second object that i have created in qml ?

          J.HilkJ 1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5
            1. setContextProperty OR
            2. Return the object from method of first class. You need to use qmlRegisterMetaType<>() for this to work.

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

            1 Reply Last reply
            3
            • Bhushan_SureB Bhushan_Sure

              @dheerendra @SGaist Sir, How can i expose second object that i have created in qml ?

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

              @Bhushan_Sure to add to @dheerendra,

              to return an object to qml, it has to be created on the heap and if parentless, the QML-Engine will - untill explicitly told otherwise - take ownership of the object and the gc may delete it at any time without you knowing it.

              So keep that in mind.


              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
              2
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                @J-Hilk super caution !!!.

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

                1 Reply Last reply
                3
                • dheerendraD Offline
                  dheerendraD Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on last edited by
                  #8

                  Did the suggestion worked for you ? Hope the issue is resolved.

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

                  Bhushan_SureB 1 Reply Last reply
                  0
                  • dheerendraD dheerendra

                    Did the suggestion worked for you ? Hope the issue is resolved.

                    Bhushan_SureB Offline
                    Bhushan_SureB Offline
                    Bhushan_Sure
                    wrote on last edited by
                    #9

                    @dheerendra @J-Hilk I was doing mistake in this, in
                    FirstClass.cpp i am making SecondClass scnd(Second class object) and using secondclass object i am emitting signal, but i have registered firstclass object in main.cpp and not SecondClassobject.

                    1. In short, i was emiting signal using secondclass_object but i registered only firstclass_object in main.cpp and not Secondclass_object. That's why it is creating problem.
                    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