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. QHttpServer: use function pointer as ViewHandler
Forum Updated to NodeBB v4.3 + New Features

QHttpServer: use function pointer as ViewHandler

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 4 Posters 1.3k 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.
  • T Tarae
    30 Mar 2023, 12:56

    @JonB As you suggested, I've tried this:

    const char * responseFunction(const QHttpServerRequest & request, QHttpServerResponder && responder)
    {
      return "Hello world!";
    }
    

    Still doesn't compile, same error.

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 30 Mar 2023, 12:58 last edited by
    #10

    @Tarae said in QHttpServer: use function pointer as ViewHandler:

    const char * responseFunction

    Try without const:

    char * responseFunction(...
    

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

    T 1 Reply Last reply 30 Mar 2023, 13:01
    0
    • J jsulm
      30 Mar 2023, 12:58

      @Tarae said in QHttpServer: use function pointer as ViewHandler:

      const char * responseFunction

      Try without const:

      char * responseFunction(...
      
      T Offline
      T Offline
      Tarae
      wrote on 30 Mar 2023, 13:01 last edited by
      #11

      @jsulm No, const char *, char *, QString, const QString, QByteArray, const QByteArray. Still same error.

      1 Reply Last reply
      0
      • T Tarae
        30 Mar 2023, 12:56

        @JonB As you suggested, I've tried this:

        const char * responseFunction(const QHttpServerRequest & request, QHttpServerResponder && responder)
        {
          return "Hello world!";
        }
        

        Still doesn't compile, same error.

        J Offline
        J Offline
        JonB
        wrote on 30 Mar 2023, 13:01 last edited by JonB
        #12

        @Tarae
        I'm only guessing, but since those parameters are optional try specifying defaults for them. Assuming you can do that in a free/C function, I don't know.

        The const char * for the return value is correct.

        BTW: if all else fails, you can always make it a lambda like the examples show and have that lambda simply call your free function....

        T 1 Reply Last reply 30 Mar 2023, 13:23
        0
        • J JonB
          30 Mar 2023, 13:01

          @Tarae
          I'm only guessing, but since those parameters are optional try specifying defaults for them. Assuming you can do that in a free/C function, I don't know.

          The const char * for the return value is correct.

          BTW: if all else fails, you can always make it a lambda like the examples show and have that lambda simply call your free function....

          T Offline
          T Offline
          Tarae
          wrote on 30 Mar 2023, 13:23 last edited by
          #13

          @JonB specifying defaults for const QHttpServerRequest & and QHttpServerResponder && is not that easy. But I don't feel like it will solve my problem.
          I know I can use a lambda to call my function, but the documentation says function pointer, so I want to figure it out.

          C 1 Reply Last reply 31 Mar 2023, 08:04
          0
          • T Tarae
            30 Mar 2023, 13:23

            @JonB specifying defaults for const QHttpServerRequest & and QHttpServerResponder && is not that easy. But I don't feel like it will solve my problem.
            I know I can use a lambda to call my function, but the documentation says function pointer, so I want to figure it out.

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 31 Mar 2023, 08:04 last edited by
            #14

            From my pov a plain function is currently not possible so either the documentation is wrong or the implementation. Since there is an easy workaround for it I would guess the solution is to adjust the documentation. Please create a bug report for it and post the bug number here.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            T 1 Reply Last reply 31 Mar 2023, 13:47
            2
            • J Offline
              J Offline
              JonB
              wrote on 31 Mar 2023, 08:11 last edited by JonB
              #15

              If this is what @Christian-Ehrlicher says, no wonder we could not figure it yesterday! :)

              C 1 Reply Last reply 31 Mar 2023, 08:15
              0
              • J JonB
                31 Mar 2023, 08:11

                If this is what @Christian-Ehrlicher says, no wonder we could not figure it yesterday! :)

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 31 Mar 2023, 08:15 last edited by
                #16

                A plain function does not work but a std::function<> since the template magic expects a callable:

                std::function<const char* ()>  resp = &responseFunction;
                server.route("/", resp);
                

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                J 1 Reply Last reply 31 Mar 2023, 08:39
                2
                • C Christian Ehrlicher
                  31 Mar 2023, 08:15

                  A plain function does not work but a std::function<> since the template magic expects a callable:

                  std::function<const char* ()>  resp = &responseFunction;
                  server.route("/", resp);
                  
                  J Offline
                  J Offline
                  JonB
                  wrote on 31 Mar 2023, 08:39 last edited by JonB
                  #17

                  @Christian-Ehrlicher
                  I am whistling in the dark here, but I'm a student of C++ so I am interested. I am not as expert as you. The issue is "a callable". I do not have Qt6 so cannot test QHttpServer::route() If you don't want to use a lambda or std::function here, could you use something like:

                  struct callable
                  {
                    const char *operator()() const
                    {
                      std::cout << "Hello World from a callable!\n";
                      return "Hello World!";
                    }
                  };
                  
                  const callable responseFunction;
                  server.route("/", &responseFunction);
                  

                  Maybe this is the sort of thing a lambda or std::function() does which is different from a plain function?

                  1 Reply Last reply
                  0
                  • C Christian Ehrlicher
                    31 Mar 2023, 08:04

                    From my pov a plain function is currently not possible so either the documentation is wrong or the implementation. Since there is an easy workaround for it I would guess the solution is to adjust the documentation. Please create a bug report for it and post the bug number here.

                    T Offline
                    T Offline
                    Tarae
                    wrote on 31 Mar 2023, 13:47 last edited by
                    #18

                    @Christian-Ehrlicher I've created a bug report: https://bugreports.qt.io/browse/QTBUG-112484

                    C 1 Reply Last reply 25 Apr 2023, 18:34
                    2
                    • T Tarae
                      31 Mar 2023, 13:47

                      @Christian-Ehrlicher I've created a bug report: https://bugreports.qt.io/browse/QTBUG-112484

                      C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 25 Apr 2023, 18:34 last edited by
                      #19

                      And fixed

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      0
                      • T Tarae has marked this topic as solved on 19 Jun 2023, 16:32

                      • Login

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