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 pass signal from one class to another.
Forum Updated to NodeBB v4.3 + New Features

How to pass signal from one class to another.

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 816 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.
  • S summit

    How to pass signal from one class to another.

    This is the first class

    #include <QtWidgets/QMainWindow>
    #include "ui_MouseClickTest.h"
    
    class MouseClickTest : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MouseClickTest(QWidget *parent = Q_NULLPTR);
    
    protected:
        void mousePressEvent(QMouseEvent* event) override;
    
    signals:
        void MouseClicked(QMouseEvent* event);
    
    private:
        Ui::MouseClickTestClass ui;
    };
    

    i want to pass the MouseClicked signal to another class , whenever any object of this class is created it should be connected to the signal from the MouseClickTest class.

    class Geometry : public QObject
    {
    
    public slots:
    	void Clicked(QMouseEvent* event);
    
    };
    
    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by jsulm
    #2

    @summit Well, you connect the signal to the slot using connect(). What exactly is your question/problem?
    https://doc.qt.io/qt-5/signalsandslots.html

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

    S 1 Reply Last reply
    0
    • jsulmJ jsulm

      @summit Well, you connect the signal to the slot using connect(). What exactly is your question/problem?
      https://doc.qt.io/qt-5/signalsandslots.html

      S Offline
      S Offline
      summit
      wrote on last edited by summit
      #3

      @jsulm What would be the connect syntax in this case where i have not created any objects , i can connect a object of geometry to MouseClickTest class but how can i do it without creating any object.

      JonBJ 1 Reply Last reply
      0
      • S summit

        @jsulm What would be the connect syntax in this case where i have not created any objects , i can connect a object of geometry to MouseClickTest class but how can i do it without creating any object.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #4

        @summit said in How to pass signal from one class to another.:

        without creating any object.

        You can't. Signals & slots both require an object instance to connect(). You have to go to wherever the second one --- usually the slot object --- is created, or the caller of where it is created, and do your connection there. For each slot object instance you create.

        S J.HilkJ 2 Replies Last reply
        1
        • JonBJ JonB

          @summit said in How to pass signal from one class to another.:

          without creating any object.

          You can't. Signals & slots both require an object instance to connect(). You have to go to wherever the second one --- usually the slot object --- is created, or the caller of where it is created, and do your connection there. For each slot object instance you create.

          S Offline
          S Offline
          summit
          wrote on last edited by summit
          #5

          @JonB Is it possible to do it in any other way , or assign mouseclick() event to Geometry class ?

          JonBJ 1 Reply Last reply
          0
          • S summit

            @JonB Is it possible to do it in any other way , or assign mouseclick() event to Geometry class ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #6

            @summit
            No. That's why I wrote "You can't", else I would have said "You can". Look at the definitions of the various connect() overloads.

            No matter how many times you ask, the answer still remains the connections are between object instances of the signaller and the slotter.

            S 1 Reply Last reply
            0
            • JonBJ JonB

              @summit
              No. That's why I wrote "You can't", else I would have said "You can". Look at the definitions of the various connect() overloads.

              No matter how many times you ask, the answer still remains the connections are between object instances of the signaller and the slotter.

              S Offline
              S Offline
              summit
              wrote on last edited by
              #7

              @JonB Thanks just one more question can i use this function in Geometry Class.
              void mousePressEvent(QMouseEvent* event) override;

              jsulmJ JonBJ 2 Replies Last reply
              0
              • S summit

                @JonB Thanks just one more question can i use this function in Geometry Class.
                void mousePressEvent(QMouseEvent* event) override;

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @summit said in How to pass signal from one class to another.:

                can i use this function in Geometry Class

                No, since it is not a QWidget

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

                1 Reply Last reply
                2
                • S summit

                  @JonB Thanks just one more question can i use this function in Geometry Class.
                  void mousePressEvent(QMouseEvent* event) override;

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #9

                  @summit
                  By the look of it: make your void MouseClickTest::mousePressEvent(QMouseEvent* event) override go emit MouseClicked(event). Now you have a signal with the desired parameter, which you can connect() to each Geometry* geometryInstance's &Geometry::Clicked slot.

                  S 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @summit
                    By the look of it: make your void MouseClickTest::mousePressEvent(QMouseEvent* event) override go emit MouseClicked(event). Now you have a signal with the desired parameter, which you can connect() to each Geometry* geometryInstance's &Geometry::Clicked slot.

                    S Offline
                    S Offline
                    summit
                    wrote on last edited by
                    #10

                    @JonB Thanks very much for the answer.

                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @summit said in How to pass signal from one class to another.:

                      without creating any object.

                      You can't. Signals & slots both require an object instance to connect(). You have to go to wherever the second one --- usually the slot object --- is created, or the caller of where it is created, and do your connection there. For each slot object instance you create.

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

                      @JonB said in How to pass signal from one class to another.:

                      You can't. Signals & slots both require an object instance to connect(

                      Thats not strictly correct, you can connect without an receiver QObject, the overload exists for a reason. but the sender has to be an QObject

                      https://doc.qt.io/qt-5/qobject.html#connect-2
                      https://doc.qt.io/qt-5/qobject.html#connect-4

                      That said, @summit what actually do you want to archive with the MouseClick event? maybe you're asking the wrong question here


                      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.

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

                        @JonB said in How to pass signal from one class to another.:

                        You can't. Signals & slots both require an object instance to connect(

                        Thats not strictly correct, you can connect without an receiver QObject, the overload exists for a reason. but the sender has to be an QObject

                        https://doc.qt.io/qt-5/qobject.html#connect-2
                        https://doc.qt.io/qt-5/qobject.html#connect-4

                        That said, @summit what actually do you want to archive with the MouseClick event? maybe you're asking the wrong question here

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #12

                        @J-Hilk said in How to pass signal from one class to another.:

                        you can connect without an receiver QObject

                        You can indeed, but you cannot use that to connect to instances of the OP's Geometry, which is what he is trying to do. He might write, e.g., a static method in Geometry to use as the slot, but it won't then do what he has in mind, which is to "auto-connect" each instance created to main window's signal.

                        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