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

    @jsulm Ok, here it is:

    14:38:56: Running steps for project httptest...
    14:38:56: Configuration unchanged, skipping qmake step.
    14:38:56: Starting: "/usr/bin/make" -j8
    /home/user/Qt/6.4.3/gcc_64/bin/qmake -o Makefile ../httptest/httptest.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug
    g++ -c -pipe -g -std=gnu++1z -Wall -Wextra -fPIC -D_REENTRANT -DQT_QML_DEBUG -DQT_HTTPSERVER_LIB -DQT_WEBSOCKETS_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../httptest -I. -I/home/user/Qt/6.4.3/gcc_64/include -I/home/user/Qt/6.4.3/gcc_64/include/QtHttpServer -I/home/user/Qt/6.4.3/gcc_64/include/QtWebSockets -I/home/user/Qt/6.4.3/gcc_64/include/QtNetwork -I/home/user/Qt/6.4.3/gcc_64/include/QtCore -I. -I/home/user/Qt/6.4.3/gcc_64/mkspecs/linux-g++ -o main.o ../httptest/main.cpp
    In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:8,
                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:8,
                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:9,
                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                     from ../httptest/main.cpp:2:
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h: In instantiation of ‘struct QtPrivate::FunctionTraits<const char* (* const&)()>’:
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h:65:11:   required from ‘struct QtPrivate::ViewTraits<const char* (* const&)(), false>’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:18:8:   required from ‘struct QtPrivate::RouterViewTraitsHelper<const char* (* const&)(), false>’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:122:11:   required from ‘struct QHttpServerRouterViewTraits<const char* (* const&)(), false>’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39:   required by substitution of ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const [with ViewHandler = const char* (* const&)(); ViewTraits = QHttpServerRouterViewTraits<const char* (* const&)(), false>]’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59:   required from ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
    ../httptest/main.cpp:16:15:   required from here
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h:42:56: error: ‘operator()’ is not a member of ‘std::remove_cv<const char* (* const)()>::type’ {aka ‘const char* (*)()’}
       42 | struct FunctionTraits : public FunctionTraits<decltype(&q20::remove_cvref_t<T>::operator())>{};
          |                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h: In instantiation of ‘struct QtPrivate::ViewTraits<const char* (* const&)(), false>’:
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:18:8:   required from ‘struct QtPrivate::RouterViewTraitsHelper<const char* (* const&)(), false>’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:122:11:   required from ‘struct QHttpServerRouterViewTraits<const char* (* const&)(), false>’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39:   required by substitution of ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const [with ViewHandler = const char* (* const&)(); ViewTraits = QHttpServerRouterViewTraits<const char* (* const&)(), false>]’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59:   required from ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
    ../httptest/main.cpp:16:15:   required from here
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverviewtraits_impl.h:65:11: error: ‘ArgumentCount’ is not a member of ‘QtPrivate::FunctionTraits<const char* (* const&)()>’
       65 |     using ArgumentIndexes = typename Indexes<FTraits::ArgumentCount>::Value;
          |           ^~~~~~~~~~~~~~~
    In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:8,
                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:9,
                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                     from ../httptest/main.cpp:2:
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h: In instantiation of ‘struct QHttpServerRouterViewTraits<const char* (* const&)(), false>’:
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39:   required by substitution of ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const [with ViewHandler = const char* (* const&)(); ViewTraits = QHttpServerRouterViewTraits<const char* (* const&)(), false>]’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59:   required from ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
    ../httptest/main.cpp:16:15:   required from here
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouterviewtraits.h:122:11: error: no type named ‘ReturnType’ in ‘using FunctionTraits = using FTraits = struct QtPrivate::FunctionTraits<const char* (* const&)()>’ {aka ‘struct QtPrivate::FunctionTraits<const char* (* const&)()>’}
      122 |     using ReturnType = typename Helpers::FunctionTraits::ReturnType;
          |           ^~~~~~~~~~
    In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                     from ../httptest/main.cpp:2:
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h: In instantiation of ‘bool QHttpServer::routeImpl(Args&& ..., ViewHandler&&) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; Args = {const char (&)[2]}]’:
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:125:71:   required from ‘bool QHttpServer::routeHelper(QtPrivate::IndexesList<I ...>, Args&& ...) [with Rule = QHttpServerRouterRule; ViewHandler = const char* (*)(); ViewTraits = QHttpServerRouterViewTraits<const char* (*)(), false>; int ...I = {0}; Args = {const char (&)[2], const char* (*)()}]’
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:69:58:   required from ‘bool QHttpServer::route(Args&& ...) [with Rule = QHttpServerRouterRule; Args = {const char (&)[2], const char* (*)()}]’
    ../httptest/main.cpp:16:15:   required from here
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:135:59: error: no matching function for call to ‘QHttpServerRouter::bindCaptured(const char* (* const&)(), const QRegularExpressionMatch&)’
      135 |             auto boundViewHandler = router()->bindCaptured(viewHandler, match);
          |                                     ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
    In file included from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserver.h:9,
                     from /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/QHttpServer:1,
                     from ../httptest/main.cpp:2:
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39: note: candidate: ‘template<class ViewHandler, class ViewTraits> typename ViewTraits::BindableType QHttpServerRouter::bindCaptured(ViewHandler&&, const QRegularExpressionMatch&) const’
       57 |     typename ViewTraits::BindableType bindCaptured(ViewHandler &&handler,
          |                                       ^~~~~~~~~~~~
    /home/user/Qt/6.4.3/gcc_64/include/QtHttpServer/qhttpserverrouter.h:57:39: note:   substitution of deduced template arguments resulted in errors seen above
    make: *** [Makefile:834: main.o] Error 1
    14:38:56: The process "/usr/bin/make" exited with code 2.
    Error while building/deploying project httptest (kit: Desktop Qt 6.4.3 GCC 64bit)
    When executing step "Make"
    14:38:56: Elapsed time: 00:01.
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #8

    @Tarae
    Yep, that's pretty complex!

    Wait for @jsulm's better reply, but maybe you are required to put in the two optional parameters for this free/C function to be acceptable? Worth a try?

    T 1 Reply Last reply
    0
    • JonBJ JonB

      @Tarae
      Yep, that's pretty complex!

      Wait for @jsulm's better reply, but maybe you are required to put in the two optional parameters for this free/C function to be acceptable? Worth a try?

      T Offline
      T Offline
      Tarae
      wrote on last edited by
      #9

      @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 JonBJ 2 Replies 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.

        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 Offline
                Christian EhrlicherC Offline
                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 Offline
                    Christian EhrlicherC Offline
                    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 Offline
                          Christian EhrlicherC Offline
                          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