Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Custom ListView UI update with long running QNetworkAccessManager data fetching
Qt 6.12 LTS is out! See what's new in the release blog

Custom ListView UI update with long running QNetworkAccessManager data fetching

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
7 Posts 4 Posters 259 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.
  • stasiozS Online
    stasiozS Online
    stasioz
    wrote last edited by stasioz
    #1

    List 1: list of items I fetched using QNetworkAccessManager asynchronously with manager's
    "finished" signal connected to a slot.

    When I have a long list, I want to be able to click on the individual item and get me a detailed item description or another list, while still loading the List 1 in the background.

    At first, I put List 1 as initialItem of a StackView, when I click on an item in the list, it will push a DetailedPage into the StackView. However, the unfinished List 1 cannot seem to finish properly. Or is It suppoed to resume fetching only after I pop() the detailed page? The List 1 also cannot seem to finish when the list is cropped inside a ScrollView.

    I thought I can work around this by using z property instead of StackView. meaning I thought with z view I could continue loading the items in List 1 in the background. But this method doesn't work either.

    Anyone care to show me to the right guide? I am still trying to understand the single UI thread thingey.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi,

      How is your model built ?
      When do you create it exactly ?

      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
      0
      • stasiozS Online
        stasiozS Online
        stasioz
        wrote last edited by stasioz
        #3

        Hi SGaist,

        class List1 : public QAbstractListModel
        {
            Q_OBJECT
        
            QML_SINGLETON
            QML_ELEMENT
        
        .....
        }
        
        

        It's created in Main.qml

        1st

        Window {
        
        StackView{
          id: stackView
          initialItem: list1component
        }
        Component {
          id: list1component
          List1 {
                 onCustomclick: {
                    stackView.push(detailedPage)
                 }
          }
        }
        
        Component {
          id: detailedPage
          DetailedPage {
          }
        }
        }
        
        

        2nd

        Window {
        List1 {
          id: list1
          z: 1
          onClicked: {
            stackView.push(detailedPage)
            stackView.z = 2
          }
        }
        StackView {
        id: stackView
        z:0
        }
        Component {
          id: detailedPage
          DetailedPage {
          }
        }
        }
        

        in the 2nd way,

        I guess I want something like a Youtube Live Chat with a frame of ListView of incoming messages in one, while I could still do tasks in another.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MatiF
          wrote last edited by
          #4

          This is most likely not a threading problem. QNetworkAccessManager is asynchronous and doesn't block the UI thread, so pushing a page onto the StackView shouldn't stop it.

          My guess is your model loads through canFetchMore()/fetchMore(), which are normally called by the view. When StackView pushes the detail page, the ListView underneath becomes invisible and may stop calling fetchMore(), so loading stalls. In a ScrollView, the ListView can also get the wrong height, which can disrupt fetching too.

          My suggestion:

          Start the fetch from the model (or a separate C++ object that owns it), not from the view. Request the next page in your finished slot, and call beginInsertRows()/endInsertRows() around the code that adds each page's rows.
          Create the model once and expose it to QML. The ListView then only displays its data, so the view's visibility doesn't affect loading.

          1 Reply Last reply
          0
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote last edited by
            #5

            What do you mean by "The List 1 also cannot seem to finish"?

            stasiozS 1 Reply Last reply
            0
            • stasiozS Online
              stasiozS Online
              stasioz
              wrote last edited by
              #6

              My List1 addItem code:

              sorry for my convoluted partially copy patsed from Google AI example code,

              get xml from server and save to file:

              void FeedList::onNetworkQueryFinished()
              {
                  auto *reply = qobject_cast<QNetworkReply*>(sender());
                  if (!reply) return;
                  QVariant fromCache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute);
                  qDebug() << "page from cache?" << fromCache.toBool();
              
                  QString href;
                  QString feedName;
                  QByteArray responseBody;
                  QString fileName;
              
                  if (reply->error() == QNetworkReply::NoError) {
                      href = reply->request().url().toString();
                      responseBody = reply->readAll();
                      fileName = QString(href).replace("/","_").replace(":","_");
              
                      MyUtil::getRSSFeedChannelInfo(&responseBody, &feedName);
                      MyUtil::writeXmlToFile(&fileName, &responseBody);
              
                      qDebug() << feedName;
                      qDebug() << fileName;
              
                      addFeed(FeedList::counter, feedName, href);
                      FeedList::counter++;
                  }
              
                  reply->deleteLater();
              
              }
              

              addFeed() definition:

              void FeedList::addFeed(const int &index, const QString &feedName, const QString &href)
              {
                  beginInsertRows(QModelIndex(), m_feeds.length(), m_feeds.length());
              
                  Feed *feed = new Feed(this);
              
                  feed->setFeedIndex(index);
                  feed->setFeedName(feedName);
                  feed->setHref(href);
              
                  m_feeds << feed;
              
                  endInsertRows();
                  //new QAbstractItemModelTester(this,
                  //                             QAbstractItemModelTester::FailureReportingMode::Fatal, this);
              }
              
              1 Reply Last reply
              0
              • GrecKoG GrecKo

                What do you mean by "The List 1 also cannot seem to finish"?

                stasiozS Online
                stasiozS Online
                stasioz
                wrote last edited by
                #7

                @GrecKo said in Custom ListView UI update with long running QNetworkAccessManager data fetching:

                What do you mean by "The List 1 also cannot seem to finish"?

                I have 10 urls in config file but only loaded 7.

                Perhaps just some error notification issue when a feed failed to fetch.
                Sometimes, the Feed widget (which contains 2 Text fields, only displayed one of them).

                Oh well I need to rewrite and simplify maybe. Part of learning.

                I just found out I might be able to use a Q_INVOKABLE QVariantList fetchItems() instead of using beginInsertRows/EndInsertRows

                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
                • Unsolved