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 Updated to NodeBB v4.3 + New Features

QTest a connection from a private member

Scheduled Pinned Locked Moved Solved General and Desktop
qtestconnectionprivate
16 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.
  • W Offline
    W Offline
    wasawi2
    wrote on 9 May 2024, 15:28 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
    • A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 9 May 2024, 15:30 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 9 May 2024, 15:32 last edited by
        #6

        the method is in the "public slots:"

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wasawi2
          wrote on 9 May 2024, 15:33 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 9 May 2024, 15:35 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
            • A Offline
              A Offline
              Axel Spoerl
              Moderators
              wrote on 9 May 2024, 16:02 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 9 May 2024, 16:37 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
                • A Offline
                  A Offline
                  Axel Spoerl
                  Moderators
                  wrote on 9 May 2024, 17:17 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 10 May 2024, 08:53 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.

                    J 1 Reply Last reply 10 May 2024, 08:57
                    0
                    • W wasawi2
                      10 May 2024, 08:53

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

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 10 May 2024, 08:57 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

                      J 1 Reply Last reply 10 May 2024, 09:15
                      1
                      • J jsulm
                        10 May 2024, 08:57

                        @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.

                        J Offline
                        J Offline
                        JonB
                        wrote on 10 May 2024, 09:15 last edited by JonB 5 Oct 2024, 09:16
                        #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) :)

                        J 1 Reply Last reply 10 May 2024, 09:19
                        1
                        • J JonB
                          10 May 2024, 09:15

                          @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) :)

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 10 May 2024, 09:19 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

                          J 1 Reply Last reply 10 May 2024, 09:22
                          1
                          • J jsulm
                            10 May 2024, 09:19

                            @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.

                            J Offline
                            J Offline
                            JonB
                            wrote on 10 May 2024, 09:22 last edited by JonB 5 Oct 2024, 10:16
                            #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 10 May 2024, 10:09

                            13/16

                            10 May 2024, 08:57

                            • Login

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