[Qt-creator] Getting results from network in LocatorFilter

Eike Ziller Eike.Ziller at qt.io
Tue Mar 9 10:04:17 CET 2021


You could have a look at the “spotlightlocatorfilter” src/plugins/coreplugin/locator/spotlightlocatorfilter.cpp from 4.15 branch or later:
https://code.qt.io/cgit/qt-creator/qt-creator.git/tree/src/plugins/coreplugin/locator/spotlightlocatorfilter.cpp

The SpotlightIterator class creates a QProcess in the main thread, processes its output in the main thread, posts that in a queue of results, and informs the locator thread with QWaitCondition::wakeAll() while the locator thread is waiting for new results with QWaitCondition::wait(…) (in SpotlightIterator::ensureNext(), which is called from the filter’s base class, BaseFileFilter::matchesFor).

The additional SpotlightIterator class is not necessary in your case - it is part of the spotlight filter’s superclass, BaseFileFilter, API. You’d probably start the request directly in your filter’s prepareSearch(…) method, and wait for results directly in matchesFor(…).

Hope that gives you some ideas,
Br, Eike

> On Mar 7, 2021, at 11:16, Mikhail Bryukhovets <push.uni at gmail.com> wrote:
> 
> Hello!
> 
> I am trying to add suggestions functionality to my QtCerator plugin: When user types search request in the locator there will be a displayed list of suggested search queries based on entered text. Similar as it is done in browsers or search engines. Suggestions are obtained from network as results of GET requests to remote server. For example:
> QNetworkRequest request;
> request.setUrl(QUrl::fromUserInput("https://example.com/user_input?action=suggest"));
> auto reply = network_manager_.get(request);
> QObject::connect(reply, &QNetworkReply::readyRead, [&](){
>     qDebug() << "reply:" << reply->readAll();
> });
> 
> So as I understand it I should make a network request inside matchesFor() function of my class derived from Core::ILocatorFilter. 
> The first issue is: How and where do I obtain a result of a request to return it to the user?
> The second is: It seems that QNetworkAccessManager can't process QNetworkRequest created in another thread (QObject: Cannot create children for a parent that is in a different thread.
> (Parent is QNetworkAccessManager(0x163c4d0), parent's thread is QThread(0x6cc0d0), current thread is QThread(0x28b5810))
> I didn't find similar cases in plugins provided with QtCreator so I hope someone can point me in the right direction.
> 
> Mikhail.
> _______________________________________________
> Qt-creator mailing list
> Qt-creator at qt-project.org
> https://lists.qt-project.org/listinfo/qt-creator



More information about the Qt-creator mailing list