Load HTML file in QWebView by QTcpServer
-
wrote on 7 Oct 2019, 02:18 last edited by fant 10 Jul 2019, 02:21
Hi,
currently I writing a desktop application for playing YouTube playlists. Therefore a HTML file will be loaded from Qt resources into QML WebView.
WebView { anchors.centerIn: parent anchors.fill: parent id: webView opacity: 1 url: "qrc:/View/Player/player.html?" + youTubeId; }
In the HTML document the YouTube iFrame is included. So I have the opportunity to control the video by YouTube iFrame API.
But on this way the video can't be played, because the HTML document will be loaded as local file. I suppose that the webkit engine blocks the streaming request. For test I put the HTML file in a root directory of running Apache HTTP server and replaced the WebView url with localhost address: http://localhost:8080/player.html. As expected, it works then.
My next idea was writing a simple web server that is able deliver the HTML file by localhost address. I would like to implement it in my application, instead using an external server as Apache.
That's the point where my problem occurs: I can't get it. My server implementation doesn't respond to any HTTP request. Probably I'm forgot something very important or I have a very bad fallacy.At first I wrote a custom server class:
class Server : public QTcpServer { Q_OBJECT /* ... */ protected: void incomingConnection(qintptr socketDescriptor); };
Inside of incomingConnection method a debug message will be printed to console. My main method looks like this:
int main(int argc, char *argv[]) { Controller controller; controller.operate(""); QCoreApplication app(argc, argv); return app.exec(); }
Controller class is copied from Qt documentation: Look here.
This is my implementation of doWork method from Worker class:void Worker::doWork(const QString &) { Server server; server.listen(QHostAddress::LocalHost, 8080); emit resultReady(QString("ready")); }
When app is running, these lines will be executed, I checked it by debugger. The server is running, too (according to isListening method).
Now, when server is running (my Apache webserver does not, port is free^^) and I request this url: http://localhost:8080 in my web browser, the browser tab is loading long time, but won't complete and on console I get no response (debug message in incomingConnection).
When I stop the server, the browser stops loading immediately.Some additional information that could be useful: I running my application in QtCreator with administrator rights. At first run my OS shows a dialogue to access network communication - I still approve it.
Does somebody know what I did wrong / how I could solve my problem or how I could take my next steps to analyze where it fails?
Thanks in advance.
-
Hi,
First thing: QXXXApplication should be the first object created.
The your server object only lives in the method since it's a local variable. Thus as soon as the emit is done, the function ends and the object is destroyed.
-
wrote on 7 Oct 2019, 22:24 last edited by
Oh My God, and it works. It really works. Of course. Not sure if I'm a fool. 😄
I'll continue with some other tests and adjustments in the next hours or days then I'll update this thread to fixed state.
Thank you for your help!
1/3