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. Exposing signal of the embedded QWidget
Qt 6.11 is out! See what's new in the release blog

Exposing signal of the embedded QWidget

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.2k Views 2 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.
  • J Offline
    J Offline
    Jendker
    wrote on last edited by
    #1

    Hello everyone,

    Is there any nice way of exposing the signal of the embedded private member (in my case the MyWidget, which stays private, I am adding it with Qt Designer), so that it will be possible to connect to its signal from the outside (e.g. MainWindow)? I guess that would brake the encapsulation, but maybe there is a nicer way, than exposing it like this:

    QObject::connect(my_widget, &MyWidget::my_signal, this, &MyDialog::my_signal);
    

    Here MyWidget is embedded into MyDialog and its signal has to be accessed from the outside (from MainWindow) to connect.

    With this solution it will be possible to connect to 'my_signal' of MyDialog instead of directly connecting to the signal of the private 'my_widget' as the walkaround. The obvious issue is the additional code while exposing all the necessary signals every signal time I am embedding this MyWidget in any form.

    Thank you for any hints!

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

      Hi,

      That's the correct way of doing it.

      If you have that many signals to forward that it becomes cumbersome then you might want to rethink the design of your widget.

      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
      4
      • J Jendker

        Hello everyone,

        Is there any nice way of exposing the signal of the embedded private member (in my case the MyWidget, which stays private, I am adding it with Qt Designer), so that it will be possible to connect to its signal from the outside (e.g. MainWindow)? I guess that would brake the encapsulation, but maybe there is a nicer way, than exposing it like this:

        QObject::connect(my_widget, &MyWidget::my_signal, this, &MyDialog::my_signal);
        

        Here MyWidget is embedded into MyDialog and its signal has to be accessed from the outside (from MainWindow) to connect.

        With this solution it will be possible to connect to 'my_signal' of MyDialog instead of directly connecting to the signal of the private 'my_widget' as the walkaround. The obvious issue is the additional code while exposing all the necessary signals every signal time I am embedding this MyWidget in any form.

        Thank you for any hints!

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

        @Jendker
        Writing a public accessor which returns the private MyWidget to the outside world lets them call anything they like on it with barely any effort....

        Of course, as @SGaist says you're supposed to expose each signal or property on a case-by-case basis :)

        J 1 Reply Last reply
        2
        • JonBJ JonB

          @Jendker
          Writing a public accessor which returns the private MyWidget to the outside world lets them call anything they like on it with barely any effort....

          Of course, as @SGaist says you're supposed to expose each signal or property on a case-by-case basis :)

          J Offline
          J Offline
          Jendker
          wrote on last edited by
          #4

          @JonB Thank you, of course you are right, I will keep it in mind :) I have no idea why didn't I think about it. I guess this is the reason why you should avoid programming late in the night...

          JonBJ 1 Reply Last reply
          0
          • J Jendker

            @JonB Thank you, of course you are right, I will keep it in mind :) I have no idea why didn't I think about it. I guess this is the reason why you should avoid programming late in the night...

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

            @Jendker
            Well careful! I never advised this, I only mentioned it :)
            It will let you get lots of things done quickly, but dirtily.
            Providing proper wrappers like @SGaist said is the proper way.
            We don't know whether you're part of company or a one-man band!
            It's up to you :)

            J 1 Reply Last reply
            2
            • JonBJ JonB

              @Jendker
              Well careful! I never advised this, I only mentioned it :)
              It will let you get lots of things done quickly, but dirtily.
              Providing proper wrappers like @SGaist said is the proper way.
              We don't know whether you're part of company or a one-man band!
              It's up to you :)

              J Offline
              J Offline
              Jendker
              wrote on last edited by Jendker
              #6

              @JonB Thanks for the advice! In this case I wanted restrain myself to the solution from my first post which @SGaist has recommended, I don't have that much signals to expose. I was just considering how to act in the future if the Widget would get more complex and will be careful with the design.

              Thanks guys one more time for the help! I appreciate it.

              JKSHJ 1 Reply Last reply
              1
              • J Jendker

                @JonB Thanks for the advice! In this case I wanted restrain myself to the solution from my first post which @SGaist has recommended, I don't have that much signals to expose. I was just considering how to act in the future if the Widget would get more complex and will be careful with the design.

                Thanks guys one more time for the help! I appreciate it.

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #7

                @Jendker said in Exposing signal of the embedded QWidget:

                I don't have that much signals to expose. I was just considering how to act in the future if the Widget would get more complex and will be careful with the design.

                When your widget grows in complexity, take the time to figure out how to logically split it into a multiple smaller, independent widgets. The benefits are:

                • You can unit-test each widget independently
                • Each widget doesn't have that many signals to expose

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                1 Reply Last reply
                1

                • Login

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