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. QTest a connection from a private member
Forum Update on Monday, May 27th 2025

QTest a connection from a private member

Scheduled Pinned Locked Moved Solved General and Desktop
qtestconnectionprivate
16 Posts 4 Posters 1.0k 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.
  • W Offline
    W Offline
    wasawi2
    wrote on last edited by
    #1

    Dear all,

    So I would like to test if a connection is successful or if a connection has been fired.
    I have been looking at topics like:
    qSignalSpy
    connection with Qt::UniqueConnection
    isSignalConnected

    but my problem is that the connection is done from a private member of the class:

    void myMainWindow::aFunctionToTest( )
    {
    	connect( aPrivateMember, &aClass::aSignal, this, &myMainWindow::aSlot);
    }
    

    Would there be any way to test this function in some way?

    Thank you very much!
    w

    Axel SpoerlA 1 Reply Last reply
    0
    • jsulmJ jsulm

      @wasawi2 @Axel-Spoerl Actually with the Qt5 connect syntax you can connect methods and lambdas which are not declared as slots. So, it should be possible without code modification. You just need to make the test class friend, so it can access non-public members of the class being tested.

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

      @jsulm said in QTest a connection from a private member:

      You just need to make the test class friend

      So as the OP is asking this does require the code being tested to be changed!

      @wasawi2
      Whichever way up you look at it, you will have to make a change to allow a private member to be accessed outside. Which is what private means (i.e. cannot access outside, unless you change that or put in friend) :)

      jsulmJ 1 Reply Last reply
      1
      • W wasawi2

        Dear all,

        So I would like to test if a connection is successful or if a connection has been fired.
        I have been looking at topics like:
        qSignalSpy
        connection with Qt::UniqueConnection
        isSignalConnected

        but my problem is that the connection is done from a private member of the class:

        void myMainWindow::aFunctionToTest( )
        {
        	connect( aPrivateMember, &aClass::aSignal, this, &myMainWindow::aSlot);
        }
        

        Would there be any way to test this function in some way?

        Thank you very much!
        w

        Axel SpoerlA Offline
        Axel SpoerlA Offline
        Axel Spoerl
        Moderators
        wrote on last edited by
        #2

        @wasawi2
        You have to make the private member a private slot. Then it should work.

        Software Engineer
        The Qt Company, Oslo

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wasawi2
          wrote on last edited by
          #3

          Thank you for your answer @Axel-Spoerl !
          Could you please elaborate your answer?
          I'm very sorry but I don´t understand what you mean...

          Are you talking about modifying the code to test?
          I would like not to have to modify the code to test. Although a minor modification might be possible...

          1 Reply Last reply
          0
          • W Offline
            W Offline
            wasawi2
            wrote on last edited by
            #4

            Ideally i would like to test that the connection has been successful with something like this:
            QVERIFY( false == QObject::connect(&myMainWindow_instance.aPrivateMember, aSignal, &aMainwindow_instance, aSlot, Qt::UniqueConnection) );

            1 Reply Last reply
            0
            • Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by
              #5

              Can you show the declaration of the method you want to test?

              Probably it’s in the

              private
              

              Section of the header. A connection won’t work here. You have to put it in a

              private slots
              

              section.

              Software Engineer
              The Qt Company, Oslo

              1 Reply Last reply
              0
              • W Offline
                W Offline
                wasawi2
                wrote on last edited by
                #6

                the method is in the "public slots:"

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  wasawi2
                  wrote on last edited by
                  #7

                  but of course the so called "aPrivateMember" is under the "private:" section

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    wasawi2
                    wrote on last edited by
                    #8

                    Sorry actually the method I'm testing is under the public: section... maybe that is the problem...

                    1 Reply Last reply
                    0
                    • Axel SpoerlA Offline
                      Axel SpoerlA Offline
                      Axel Spoerl
                      Moderators
                      wrote on last edited by
                      #9

                      It needs to be a slot, no matter if private or public.

                      Software Engineer
                      The Qt Company, Oslo

                      1 Reply Last reply
                      0
                      • W Offline
                        W Offline
                        wasawi2
                        wrote on last edited by
                        #10

                        ok, let me see if I understand well.
                        so the following code does not work because the object "aPrivateMember" is actually private. if it was public the code actually works.

                        the test code

                        void MainWindowTest::init ( ){}
                        void MainWindowTest::cleanup ( ){}
                        void MainWindowTest::test_case ( )
                        {
                        	qwidget_instance = new QWidget(  );
                        	mainWindow_instance = new myMainWindow( qwidget_instance );
                        
                        	mainWindow_instance->aFunctionToTest( );
                        	QCOMPARE( (bool) QObject::connect(mainWindow_instance->aPrivateMember, &aClass::aSignal, mainWindow_instance, &myMainWindow::aSlot, Qt::UniqueConnection), false  );
                        // the test passes if, and only if aPrivateMember was declared in the public section.
                        // if aPrivateMember is private the following compile error occurs:
                        // error: 'aClass* myMainWindow::aPrivateMember' is private within this context
                        }
                        
                        QTEST_MAIN( MainWindowTest )
                        

                        the code to test

                        .h
                        class myMainWindow: public QMainWindow
                        {
                        Q_OBJECT
                        
                        public slots:
                        void aFunctionToTest ( );
                        void aSlot ( );
                        
                        private:
                        aClass aPrivateMember;
                        };
                        
                        
                        .cpp
                        void myMainWindow::aFunctionToTest( )
                        {
                        	connect( aPrivateMember, &aClass::aSignal, this, &myMainWindow::aSlot);
                        }
                        

                        So my question stays the same. Is it possible to test the success of the connection done in aFunctionToTest given that aPrivateMember is actually private?

                        Thank you so much for the patience!
                        I tried to be as clear as possible now.

                        And I'm pretty sure I must be missing something obvious.. but I cant see what it is...

                        1 Reply Last reply
                        0
                        • Axel SpoerlA Offline
                          Axel SpoerlA Offline
                          Axel Spoerl
                          Moderators
                          wrote on last edited by
                          #11

                          Ah, now I understand better.
                          The private member can’t be tested (called) in an external test class normally. It also can’t be invoked / connected, because it isn’t a slot.
                          So if you want to connect it, it has to be a slot. If you want to keep it private, you could befriend the test class.

                          Software Engineer
                          The Qt Company, Oslo

                          1 Reply Last reply
                          2
                          • W Offline
                            W Offline
                            wasawi2
                            wrote on last edited by
                            #12

                            I see.
                            So If i understand correctly without modifying the code to test there is no way to test the connection.

                            jsulmJ 1 Reply Last reply
                            0
                            • W wasawi2

                              I see.
                              So If i understand correctly without modifying the code to test there is no way to test the connection.

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

                              @wasawi2 @Axel-Spoerl Actually with the Qt5 connect syntax you can connect methods and lambdas which are not declared as slots. So, it should be possible without code modification. You just need to make the test class friend, so it can access non-public members of the class being tested.

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

                              JonBJ 1 Reply Last reply
                              1
                              • jsulmJ jsulm

                                @wasawi2 @Axel-Spoerl Actually with the Qt5 connect syntax you can connect methods and lambdas which are not declared as slots. So, it should be possible without code modification. You just need to make the test class friend, so it can access non-public members of the class being tested.

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

                                @jsulm said in QTest a connection from a private member:

                                You just need to make the test class friend

                                So as the OP is asking this does require the code being tested to be changed!

                                @wasawi2
                                Whichever way up you look at it, you will have to make a change to allow a private member to be accessed outside. Which is what private means (i.e. cannot access outside, unless you change that or put in friend) :)

                                jsulmJ 1 Reply Last reply
                                1
                                • JonBJ JonB

                                  @jsulm said in QTest a connection from a private member:

                                  You just need to make the test class friend

                                  So as the OP is asking this does require the code being tested to be changed!

                                  @wasawi2
                                  Whichever way up you look at it, you will have to make a change to allow a private member to be accessed outside. Which is what private means (i.e. cannot access outside, unless you change that or put in friend) :)

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

                                  @JonB said in QTest a connection from a private member:

                                  So as the OP is asking this does require the code being tested to be changed!

                                  Well, yes. But it does not require to change the visibility of the class members/methods.

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

                                  JonBJ 1 Reply Last reply
                                  1
                                  • jsulmJ jsulm

                                    @JonB said in QTest a connection from a private member:

                                    So as the OP is asking this does require the code being tested to be changed!

                                    Well, yes. But it does not require to change the visibility of the class members/methods.

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

                                    @jsulm
                                    I absolutely concur, hence my:

                                    [private] cannot access outside, unless you change that or put in friend

                                    The point being to answer OP's

                                    So If i understand correctly without modifying the code to test there is no way to test the connection.

                                    They will have to make some change, which doubtless they were attempting to avoid. Just a heads-up.

                                    1 Reply Last reply
                                    1
                                    • W wasawi2 has marked this topic as solved on

                                    • Login

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