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. Get QWebView info from functiong running on external thread?
Qt 6.11 is out! See what's new in the release blog

Get QWebView info from functiong running on external thread?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 2.3k 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.
  • D Offline
    D Offline
    Dr. No
    wrote on last edited by
    #1

    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).

    jsulmJ JonBJ 2 Replies Last reply
    0
    • D Dr. No

      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).

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      0
      • D Dr. No

        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).

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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.

        D 1 Reply Last reply
        0
        • jsulmJ jsulm

          @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.

          D Offline
          D Offline
          Dr. No
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          0
          • JonBJ JonB

            @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.

            D Offline
            D Offline
            Dr. No
            wrote on last edited by
            #5

            @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.

            JonBJ 1 Reply Last reply
            1
            • D Dr. No

              @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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

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