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. Disconnect/Connect using RAII paradigm
QtWS25 Last Chance

Disconnect/Connect using RAII paradigm

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 435 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.
  • E Offline
    E Offline
    enne9
    wrote on 16 Jul 2020, 20:20 last edited by enne9
    #1

    Hello, I would like to use RAII paradigm for operations involving disconnect and then connect.
    In the following, an example of what I have:

    disconnect(sender, signal, receiver, method);
    // Do something
    connect(sender, signal, receiver, method);
    

    or even

    disconnect(sender, signal, receiver, method);
    disconnect(sender2, signal2, receiver2, method2);
    // Do something
    connect(sender, signal, receiver, method);
    connect(sender2, signal2, receiver2, method2);
    

    This is motivated by the fact that the code in the middle would otherwise send a signal, that I don't want to emit.

    Is there any clean way to write the same using RAII paradigm? Something like:

    TemporaryDisconnect(sender, signal, receiver, method, []{
      doSomething();
    });
    

    I don't have much experience so I was trying with:

    template <typename PointerToMemberFunction>
    class TemporaryDisconnect : public QObject {
      const QObject *sender, *receiver;
      PointerToMemberFunction signal, method;
    
      TemporaryDisconnect(const QObject *sender, PointerToMemberFunction signal,
                          const QObject *receiver, PointerToMemberFunction method,
                          std::function<void()> f) {
        this->sender = sender;
        this->signal = signal;
        this->receiver = receiver;
        this->method = method;
        disconnect(sender, signal, receiver, method);
    
        f();
      }
    
      ~TemporaryDisconnect() { connect(sender, signal, receiver, method); }
    };
    

    The code above is just to give an idea of what is my goal. Do you have any suggestions?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 16 Jul 2020, 20:23 last edited by
      #2

      Hi,

      I think QSignalBlocker would be simpler.

      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
      • E Offline
        E Offline
        enne9
        wrote on 16 Jul 2020, 20:32 last edited by
        #3

        So it can be solved just by using:

        {
          const QSignalBlocker blocker(sender);
          const QSignalBlocker blocker(sender2);
          doSomething();
        }
        

        Is that right? Thank you for the quick answer.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 16 Jul 2020, 20:36 last edited by
          #4

          That's the idea yes.

          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
          0
          • E Offline
            E Offline
            enne9
            wrote on 16 Jul 2020, 21:10 last edited by
            #5

            But what if I want to disable a specific signal?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 16 Jul 2020, 21:24 last edited by
              #6

              If you need that fine grained surgery then it might mean that your architecture may not be optimal.

              Can you give a practical use case example ?

              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
              0
              • E Offline
                E Offline
                enne9
                wrote on 16 Jul 2020, 21:50 last edited by
                #7

                Yeah, you are right, I found out that disabling all of them should not be a problem.
                Thanks

                1 Reply Last reply
                0

                1/7

                16 Jul 2020, 20:20

                • Login

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