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. Load HTML file in QWebView by QTcpServer
Forum Updated to NodeBB v4.3 + New Features

Load HTML file in QWebView by QTcpServer

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    fant
    wrote on 7 Oct 2019, 02:18 last edited by fant 10 Jul 2019, 02:21
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 7 Oct 2019, 19:32 last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • F Offline
        F Offline
        fant
        wrote on 7 Oct 2019, 22:24 last edited by
        #3

        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 Reply Last reply
        1

        1/3

        7 Oct 2019, 02:18

        • Login

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