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. Calling a method before executing a slot
QtWS25 Last Chance

Calling a method before executing a slot

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.6k 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.
  • fmopeF Offline
    fmopeF Offline
    fmope
    wrote on last edited by
    #1

    Hello,

    I would like to ask whether it is possible to perform a specific function before executing a slot? I mean a mechanism similar to "before triggers" found in SQL.

    Example:

    class TestA : public QObject
    {
         Q_OBJECT
    
    public slots:
         void slotOne();
         void slotTwo();
    
    private:
        bool ready;
        void beforeAnySlot()
        {
             if(ready)
                  //execute slot
             else
                  //reject
        }
    };
    

    I would like to skip this solution:

    void slotOne() 
    {
        if(!ready)
            return;
    }
    
    void slotTwo() 
    {
        if(!ready)
            return;
    }
    

    Regards
    Paul

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      No, there is no such mechanism. But you could connect the signals to the slots when ready becomes true and disconnect when it becomes false.

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

      1 Reply Last reply
      1
      • fmopeF Offline
        fmopeF Offline
        fmope
        wrote on last edited by
        #3

        Unfortunately I can not use this solution, because I want to use QWebChannel.

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          hi
          Could you not call a function from the slots that
          can check if ready before performing the "action" ?

          If you provide more info about your use case, we might get good ideas :)

          1 Reply Last reply
          2
          • fmopeF Offline
            fmopeF Offline
            fmope
            wrote on last edited by
            #5

            Example code:

            class MyClass2 : public QObject
            {
                Q_OBJECT
            
            public slots:
                void logout()
                {
                    //can call slot only if user is logged
                }
            
                void slot2()
                {
                    //can call slot only if user is logged
                }
            
                //...
            
            private:
                bool logged;
            }
            
            class Client : public QObject
            {
               Q_OBJECT
            
            public:   
            
               Client() 
               {
                   channel.registerObject("dashboard", myClass);
                   channel.registerObject("user", myClass2);
                   //...
               }
            
               QWebChannel channel;
               QWebSocket socket;
               MyClass myClass;
               MyClass2 myClass2;
            }
            

            I want to avoid a situation where the client-side method 'logout' is called when the user is offline.

            //client.js
            
            //...
            channel.objects.user.logout();
            

            By limiting Web Channel can not register the object 'user' at the moment when the client logs in. Because the objects must be registered before initialize the client.
            "Note: A current limitation is that objects must be registered before any client is initialized."

            jsulmJ 1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Ok. i see.

              who will call the slots?

              Could the signals go to same slot and you use the
              sender() to know whom send
              the signal?
              That way u need only check once.

              1 Reply Last reply
              2
              • fmopeF fmope

                Example code:

                class MyClass2 : public QObject
                {
                    Q_OBJECT
                
                public slots:
                    void logout()
                    {
                        //can call slot only if user is logged
                    }
                
                    void slot2()
                    {
                        //can call slot only if user is logged
                    }
                
                    //...
                
                private:
                    bool logged;
                }
                
                class Client : public QObject
                {
                   Q_OBJECT
                
                public:   
                
                   Client() 
                   {
                       channel.registerObject("dashboard", myClass);
                       channel.registerObject("user", myClass2);
                       //...
                   }
                
                   QWebChannel channel;
                   QWebSocket socket;
                   MyClass myClass;
                   MyClass2 myClass2;
                }
                

                I want to avoid a situation where the client-side method 'logout' is called when the user is offline.

                //client.js
                
                //...
                channel.objects.user.logout();
                

                By limiting Web Channel can not register the object 'user' at the moment when the client logs in. Because the objects must be registered before initialize the client.
                "Note: A current limitation is that objects must be registered before any client is initialized."

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

                @fmope You should always check whether the user is logged on or not in logout()! It is similar to the rule: check input parameters before using them and don't expect them to be always correct.

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

                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