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. How to create attribute like python flask's route?
Forum Update on Monday, May 27th 2025

How to create attribute like python flask's route?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 1.5k 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.
  • H Offline
    H Offline
    Hakan AFAT
    wrote on last edited by
    #1

    I have a struct list for functions and i use those for input a switch case method.
    But i add functions by manually. How can i create a attribute for auto recognizing like flask?

    0_1532423780487_Ekran Resmi 2018-07-24 12.15.31.png

    typedef struct {
        int a;
        void (*print)(int x);
    } _w_md;
    
    QList<_w_md> tasklist;
    
    // Some macro/attribute here
    void my_print_func(int x){ //***** }
    
    int main(){
      _w_md job = { 1, my_print_func };
      tasklist.push_back(job);
    
      //switch-case and if statements for running true function
     //like run if(_w_md.a == job.a) job.print(1234)
    }
    
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      I have no idea what flask does.
      However, if u need a list of tasks with the same function like DoJob
      and different implementations, you can use base and subclasses
      http://www.cplusplus.com/doc/tutorial/polymorphism/

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

        Hi,

        If you are thinking about the decorator pattern from Python, AFAIK, there's no direct equivalent in C++.

        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
        • mrjjM mrjj

          Hi
          I have no idea what flask does.
          However, if u need a list of tasks with the same function like DoJob
          and different implementations, you can use base and subclasses
          http://www.cplusplus.com/doc/tutorial/polymorphism/

          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by
          #4

          @mrjj said in How to create attribute like python flask's route?:

          I have no idea what flask does.

          Flask is python web framework. http://flask.pocoo.org/

          Do what you want.

          mrjjM 1 Reply Last reply
          1
          • Taz742T Taz742

            @mrjj said in How to create attribute like python flask's route?:

            I have no idea what flask does.

            Flask is python web framework. http://flask.pocoo.org/

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Taz742
            Thank you but it was more what the route thing did.

            JonBJ 1 Reply Last reply
            0
            • mrjjM mrjj

              @Taz742
              Thank you but it was more what the route thing did.

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by
              #6

              @mrjj
              Not that I know anything having never used Flask(!), but I think

              @app.route('/hello')
              def hello():
                  return 'Hello, World!'
              

              The @app.route('/hello') directive tells a Flask application that a web request for url /hello should be responded to by routing the request to the method with that attribute, i.e. the def hello() in this case.

              This allows the programmer to produce an interface by declaratively decorating desired functions with @app.route()s, which I suppose is "neat".

              The OP is saying that to achieve that at present he is instead maintaining a large struct of all the desired functions to call, and writing large switch/ifs to call the corresponding function in response to a given list of urls. He maintains that struct list manually, does not like doing so, and would like "Some macro/attribute" he can put on his functions instead which will magically do the job for him.

              So unless you can think of a way he can emulate this in C++/Qt (e.g. via its metatypes??), he is going to have soldier on manually....

              mrjjM 1 Reply Last reply
              3
              • JonBJ JonB

                @mrjj
                Not that I know anything having never used Flask(!), but I think

                @app.route('/hello')
                def hello():
                    return 'Hello, World!'
                

                The @app.route('/hello') directive tells a Flask application that a web request for url /hello should be responded to by routing the request to the method with that attribute, i.e. the def hello() in this case.

                This allows the programmer to produce an interface by declaratively decorating desired functions with @app.route()s, which I suppose is "neat".

                The OP is saying that to achieve that at present he is instead maintaining a large struct of all the desired functions to call, and writing large switch/ifs to call the corresponding function in response to a given list of urls. He maintains that struct list manually, does not like doing so, and would like "Some macro/attribute" he can put on his functions instead which will magically do the job for him.

                So unless you can think of a way he can emulate this in C++/Qt (e.g. via its metatypes??), he is going to have soldier on manually....

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @JonB
                ah. thank you.
                so basically it tags code for a given URL
                /xxx -> func a
                /aa/yy -> func b
                or is it more complex ?

                JonBJ 1 Reply Last reply
                0
                • mrjjM mrjj

                  @JonB
                  ah. thank you.
                  so basically it tags code for a given URL
                  /xxx -> func a
                  /aa/yy -> func b
                  or is it more complex ?

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8

                  @mrjj
                  Who knows? Like I said I've never used Flask!

                  https://stackoverflow.com/questions/47467658/flask-why-app-route-decorator-should-always-be-the-outermost shows

                  app.route does

                  self.add_url_rule(rule, endpoint, f, **options)

                  [f is the decorated function] so that gives an idea of what it does.

                  The gist is along the line of http://flask.pocoo.org/docs/1.0/patterns/viewdecorators/

                  View Decorators

                  Python has a really interesting feature called function decorators. This allows some really neat things for web applications. Because each view in Flask is a function, decorators can be used to inject additional functionality to one or more functions. The route() decorator is the one you probably used already.

                  So I suspect the behaviour is tied to using Python for this kind of feature....

                  mrjjM 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @mrjj
                    Who knows? Like I said I've never used Flask!

                    https://stackoverflow.com/questions/47467658/flask-why-app-route-decorator-should-always-be-the-outermost shows

                    app.route does

                    self.add_url_rule(rule, endpoint, f, **options)

                    [f is the decorated function] so that gives an idea of what it does.

                    The gist is along the line of http://flask.pocoo.org/docs/1.0/patterns/viewdecorators/

                    View Decorators

                    Python has a really interesting feature called function decorators. This allows some really neat things for web applications. Because each view in Flask is a function, decorators can be used to inject additional functionality to one or more functions. The route() decorator is the one you probably used already.

                    So I suspect the behaviour is tied to using Python for this kind of feature....

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @JonB
                    oh. so basically its a decorator pattern.
                    Just much smoother to look at than anything possible with c++ :)

                    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