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.2k 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

    @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.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on 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
    0
    • jsulmJ jsulm

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

      const char * responseFunction

      Try without const:

      char * responseFunction(...
      
      T Offline
      T Offline
      Tarae
      wrote on 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

        @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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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
        0
        • JonBJ JonB

          @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 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.

          Christian EhrlicherC 1 Reply Last reply
          0
          • T Tarae

            @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.

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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
            2
            • JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #15

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

              Christian EhrlicherC 1 Reply Last reply
              0
              • JonBJ JonB

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

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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

                JonBJ 1 Reply Last reply
                2
                • Christian EhrlicherC Christian Ehrlicher

                  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);
                  
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on 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
                  • Christian EhrlicherC Christian Ehrlicher

                    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 last edited by
                    #18

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

                    Christian EhrlicherC 1 Reply Last reply
                    2
                    • T Tarae

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

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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