Get QWebView info from functiong running on external thread?



  • I'm trying to write a function which gets called soon as the page is loaded. This function runs on external thread, run a loop waiting for an amout of time to see if a brunch of links in the page show up in the HTML body document. The issue is: even when the links are present, the function for some reason can't see it and the count() returns 0. I I call same function from a button's click function, it does shows the count normally. So the issue is when running on external thread but I don't the reason. Any help, guesses, are very appreciated!
    My code look like this:
    function called like this, from QWebView finished event:

    QtConcurrent::run(this, loadAttachmentsOrMoveOn, this);
    

    and the function body:

    void myBrowserControl::loadAttachmentsOrMoveOn(myBrowserControl *browser)
    {
        const int TIME_OUT = 60*2; // 2m
        QTime dieTime = QTime::currentTime().addSecs(TIME_OUT);
    
        while (QTime::currentTime() < dieTime)
        {
            auto links = browser->getDownloadHTMLLinks();
            qDebug().nospace().noquote() << "[" << QTime::currentTime() << "]" << "links.count() = " << links.count();
            bool hasLinks = links.count() > 0;
            if(hasLinks)
            {
                browser->doSomethingWithAttachments();
                return;
            }
    
            Sleep(300);
        }
    
        browser->noAttachmentsFound();
    }
    

    It output links.count() = 0 until time run out even albeit there are links loaded in the page. But I call browser->getDownloadHTMLLinks().count() from a QPushButton (button on mainForm)'s click, it returns a nonzero, the correct number of links on page. What am I missing?
    Why does the browser, which is a pointer, isn't returning the actual state of the object? I hope this question is clear. (I am using concurrency but I have tried QThread, resulting in same behavior).


  • Moderators

    @Dr.-No You should never ever access GUI classes from other threads! This is not supported.
    Why do you think you need a thread for that?
    Just use a QTimer in same thread which calls a slot after some timeout which then checks the links.



  • @Dr.-No
    I do not know whether the following will help, I do my work from JS rather then the main app. But to be sure a page has fully finished loading --- until which time you cannot be sure that anything has been properly set up in the web page --- I hook to the http://doc.qt.io/archives/qt-5.5/qwebview.html#loadFinished signal.



  • @jsulm I was trying to make a function to wait to links read then do something without block the main thread. I'll try with a QTimer.



  • @JonB void QWebView::loadFinished(bool ok) doesn't work when the page load its contents by using Ajax. I have done some work to detect the HTML body changes with Javascript. I guess it's the way to go.



  • @Dr.-No
    Indeed it would not know about Ajax filling!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.