Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. How to connect signal and slot in different classes in Qt
Qt 6.11 is out! See what's new in the release blog

How to connect signal and slot in different classes in Qt

Scheduled Pinned Locked Moved Unsolved Game Development
10 Posts 4 Posters 4.4k Views 3 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.
  • A Offline
    A Offline
    amarism
    wrote on last edited by
    #1
    This post is deleted!
    K 1 Reply Last reply
    0
    • A amarism

      This post is deleted!

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @amarism

      Did you checkout already signal-slot page ?

      Basically you need pointers of both classes for use in connect statement. Since your slot

      private slots:
            void SlotDisplayProgress(QString sActivity_i, int nProgressPercentage_i);
      

      you can only connect from a routine in MainWidnow (e.g. the ctor).

      So if you have in MainWindow something like:

      reader *MyReader; 
      

      you can have for instance in ctor

      connect ( MyReader, &reader::SetProgress, this, &mainwindow::SlotDisplayProgress );
      

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      4
      • A Offline
        A Offline
        amarism
        wrote on last edited by
        #3

        @koahnig said in How to connect signal and slot in different classes in Qt:

        connect ( MyReader, &reader::SetProgress, this, &mainwindow::SlotDisplayProgress );

        I am getting an exception after apply connect "Exception thrown at 0x00007FFE23CA063A (Qt5Cored.dll) in RadSpaEngine.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF."

        K W 2 Replies Last reply
        0
        • A amarism

          @koahnig said in How to connect signal and slot in different classes in Qt:

          connect ( MyReader, &reader::SetProgress, this, &mainwindow::SlotDisplayProgress );

          I am getting an exception after apply connect "Exception thrown at 0x00007FFE23CA063A (Qt5Cored.dll) in RadSpaEngine.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF."

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @amarism

          You may have to adapt the connect statement to your needs, since it is based in the example above on a number of assumptions.

          You need to post the relevant parts of mainwindow.h.

          Vote the answer(s) that helped you to solve your issue(s)

          A 1 Reply Last reply
          0
          • K koahnig

            @amarism

            You may have to adapt the connect statement to your needs, since it is based in the example above on a number of assumptions.

            You need to post the relevant parts of mainwindow.h.

            A Offline
            A Offline
            amarism
            wrote on last edited by
            #5

            @koahnig I tried debugging and everything works correctly till the emit part. However, the slot is never executed. Any help is useful to me.

            class mainwindow : public QMainWindow
            {
                Q_OBJECT
                   reader *MyReader;
            
             private slots:
                        void SlotDisplayProgress(QString sActivity_i, int nProgressPercentage_i);
            }
            
            K 1 Reply Last reply
            0
            • A amarism

              @koahnig I tried debugging and everything works correctly till the emit part. However, the slot is never executed. Any help is useful to me.

              class mainwindow : public QMainWindow
              {
                  Q_OBJECT
                     reader *MyReader;
              
               private slots:
                          void SlotDisplayProgress(QString sActivity_i, int nProgressPercentage_i);
              }
              
              K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              @amarism

              Stupid question: did you also allocate memory for an instance of reader?

              Vote the answer(s) that helped you to solve your issue(s)

              A 1 Reply Last reply
              2
              • K koahnig

                @amarism

                Stupid question: did you also allocate memory for an instance of reader?

                A Offline
                A Offline
                amarism
                wrote on last edited by
                #7

                @koahnig Yes, reader *MyReader = new reader();

                K SGaistS 2 Replies Last reply
                0
                • A amarism

                  @koahnig Yes, reader *MyReader = new reader();

                  K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  @amarism

                  And you do the instance allocation prior to connect?

                  Probably you need to show more code and to tell us where exactly the error occurs when you step through the debugger.

                  What is your OS?

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  1
                  • A amarism

                    @koahnig Yes, reader *MyReader = new reader();

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    @amarism said in How to connect signal and slot in different classes in Qt:

                    @koahnig Yes, reader *MyReader = new reader();

                    Any chance that this line is in your constructor ? If so, you are shadowing your member variable and not allocating anything to it.

                    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
                    • A amarism

                      @koahnig said in How to connect signal and slot in different classes in Qt:

                      connect ( MyReader, &reader::SetProgress, this, &mainwindow::SlotDisplayProgress );

                      I am getting an exception after apply connect "Exception thrown at 0x00007FFE23CA063A (Qt5Cored.dll) in RadSpaEngine.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF."

                      W Offline
                      W Offline
                      wrosecrans
                      wrote on last edited by
                      #10

                      @amarism said in How to connect signal and slot in different classes in Qt:

                      location 0xFFFFFFFFFFFFFFFF.

                      That's definitely a pointer error that doesn't have anything directly to do with signals or slots. What you really want to do is read the documentation on the QtCreator debugger so you can understand which variable has that invalid address 0xFFFFFFFFFFFFFFFF, and rework your logic. That sort of error message indicates that any function you tried to use on or in that object would explode badly, not just the connect().

                      Because you are focused on the signals/slots that you are trying to accomplish, you may be "missing the forest for the trees" in trying to find a solution.

                      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