diff --git a/libsearch/appsearch/app-match.cpp b/libsearch/appsearch/app-match.cpp index 0e0269e..aca6503 100644 --- a/libsearch/appsearch/app-match.cpp +++ b/libsearch/appsearch/app-match.cpp @@ -46,6 +46,7 @@ AppMatch::AppMatch(QObject *parent) : QThread(parent) if(!m_interFace->isValid()) { qWarning() << qPrintable(QDBusConnection::sessionBus().lastError().message()); } + m_interFace->setTimeout(200); qDebug() << "AppMatch is new"; } @@ -277,10 +278,10 @@ void AppMatch::appNameMatch(QString appname, QMap &inst } void AppMatch::softWareCenterSearch(QMap &softwarereturn) { - if(m_interFace->timeout() != -1) { - qWarning() << "softWareCente Dbus is timeout !"; - return; - } +// if(m_interFace->timeout() != -1) { +// qWarning() << "softWareCente Dbus is timeout !"; +// return; +// } slotDBusCallFinished(softwarereturn); qDebug() << "softWareCenter match app is successful!"; } diff --git a/src/search-app-thread.cpp b/src/search-app-thread.cpp index aadea1e..c5d482b 100644 --- a/src/search-app-thread.cpp +++ b/src/search-app-thread.cpp @@ -5,21 +5,32 @@ size_t uniqueSymbol = 0; QMutex m_mutex; SearchAppThread::SearchAppThread(QObject *parent) : QObject(parent) { - m_pool.setMaxThreadCount(1); + m_pool.setMaxThreadCount(2); m_pool.setExpiryTimeout(1000); } void SearchAppThread::startSearch(const QString & keyword) { + m_mutex.lock(); + ++uniqueSymbol; + m_mutex.unlock(); SearchApp *appsearch; - appsearch = new SearchApp(keyword); + appsearch = new SearchApp(keyword,uniqueSymbol,this); // appsearch->setKeyword(keyword); - connect(appsearch, &SearchApp::searchResultApp, this, &SearchAppThread::searchResultApp); +// connect(appsearch, &SearchApp::searchResultApp, this, &SearchAppThread::searchResultApp); m_pool.start(appsearch); } +void SearchAppThread::sendResult(const QVector result) +{ + Q_EMIT this->searchResultApp(result); +} -SearchApp::SearchApp(const QString& keyword, QObject * parent) : QObject(parent) { + +SearchApp::SearchApp(const QString& keyword, size_t uniqueSymbol, QObject * parent) : QObject(parent) { + this->setAutoDelete(true); + m_searchappThread = qobject_cast(parent); m_keyword = keyword; + m_uniqueSymbol = uniqueSymbol; } SearchApp::~SearchApp() { @@ -35,11 +46,6 @@ SearchApp::~SearchApp() { //} void SearchApp::run() { - m_mutex.lock(); - size_t tmp_uniqueSymbol; - uniqueSymbol++; - tmp_uniqueSymbol = uniqueSymbol; - m_mutex.unlock(); //nameList:应用名,pathList:已安装的是.desktop路径,未安装为空,iconList:已安装的是图标名,未安装的是图标路径 QStringList nameList, pathList, iconList, descList; QVector appVector; @@ -70,8 +76,9 @@ void SearchApp::run() { appVector.append(iconList); appVector.append(descList); m_mutex.lock(); - if (tmp_uniqueSymbol == uniqueSymbol) { - Q_EMIT this->searchResultApp(appVector); + if (m_uniqueSymbol == uniqueSymbol) { + QMetaObject::invokeMethod(m_searchappThread, "sendResult", Q_ARG(const QVector, appVector)); +// Q_EMIT this->searchResultApp(appVector); } m_mutex.unlock(); m_installed_apps.clear(); diff --git a/src/search-app-thread.h b/src/search-app-thread.h index 46cf491..9497ee5 100644 --- a/src/search-app-thread.h +++ b/src/search-app-thread.h @@ -13,27 +13,30 @@ public: SearchAppThread(QObject * parent = nullptr); ~SearchAppThread() = default; void startSearch(const QString&); + Q_INVOKABLE void sendResult(const QVector result); private: QThreadPool m_pool; Q_SIGNALS: - void searchResultApp(const QVector&); + void searchResultApp(const QVector); }; class SearchApp : public QObject, public QRunnable { Q_OBJECT public: - SearchApp(const QString& keyword, QObject * parent = nullptr); + SearchApp(const QString& keyword, size_t uniqueSymbol, QObject * parent = nullptr); ~SearchApp(); // void setKeyword(const QString&); protected: void run() override; private: + SearchAppThread *m_searchappThread = nullptr; QString m_keyword; + size_t m_uniqueSymbol; QMap m_installed_apps; QMap m_uninstalled_apps; -Q_SIGNALS: - void searchResultApp(const QVector&); +//Q_SIGNALS: +// void searchResultApp(const QVector&); }; }