class SearchThread : public QThread { Q_OBJECT public: QRegExp regexp; QList<Document*> list; protected: void run() { while (true) { if (list.isEmpty()) { break; } Document* d = list.takeFirst(); int pos = 0; while (pos != -1) { pos = regexp.indexIn(d->Text(), pos); if (pos >= 0) { emit Result(SearchResult(d, pos)); } } } } public: SearchThread(QObject* parent) {}; signals: void Result(SearchResult);};
class SearchResult { public: SearchResult(Document*, int) {...} Document* d; int pos p;};
class Mediator : public QObject {Q_OBJECT private: SearchThread thread; public: Mediator() { connect(&thread, SIGNAL(Result(SearchResult)), this, SIGNAL(Result(SearchResult)), Qt::DirectConnection); } void StartSearch() {thread.start();} signals: void Result(SearchResult);};
C++ (Qt)while (true) { if (list.isEmpty()) { emit Result(0, 0); break; }
class SearchThread : public QThread { Q_OBJECT public: QRegExp regexp; QList<Document*> list; QMutex m; protected: void run() { while (true) { m.lock(); if (list.isEmpty()) { m.unlock(); break; } Document* d = list.takeFirst(); int pos = 0; while (pos != -1) { pos = regexp.indexIn(d->Text(), pos); if (pos >= 0) { emit Result(SearchResult(d, pos)); } } m.unlock(); } } public: SearchThread(QObject* parent) {}; signals: void Result(SearchResult); public slots: void DeletingDocument(Document* d) { m.lock(); if (list.contains(d)) {list.removeAll(d);} m.unlock(); }};