QHttpServer: use function pointer as ViewHandler
-
-
@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.
@Tarae said in QHttpServer: use function pointer as ViewHandler:
const char * responseFunction
Try without const:
char * responseFunction(...
-
@Tarae said in QHttpServer: use function pointer as ViewHandler:
const char * responseFunction
Try without const:
char * responseFunction(...
-
@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.
@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....
-
@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....
@JonB specifying defaults for
const QHttpServerRequest &
andQHttpServerResponder &&
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. -
@JonB specifying defaults for
const QHttpServerRequest &
andQHttpServerResponder &&
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.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.
-
If this is what @Christian-Ehrlicher says, no wonder we could not figure it yesterday! :)
-
If this is what @Christian-Ehrlicher says, no wonder we could not figure it yesterday! :)
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);
-
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);
@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 testQHttpServer::route()
If you don't want to use a lambda orstd::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? -
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.
@Christian-Ehrlicher I've created a bug report: https://bugreports.qt.io/browse/QTBUG-112484
-
@Christian-Ehrlicher I've created a bug report: https://bugreports.qt.io/browse/QTBUG-112484
And fixed
-