동시 필터링 및 필터 축소
QtConcurrent::filter(), QtConcurrent::filtered(), QtConcurrent::filteredReduced() 함수는 QList 와 같은 시퀀스의 항목을 병렬로 필터링합니다. QtConcurrent::filter()는 시퀀스를 제자리에서 수정하고, QtConcurrent::filtered()는 필터링된 콘텐츠를 포함하는 새 시퀀스를 반환하며, QtConcurrent::filteredReduced()는 단일 결과를 반환합니다.
이 함수들은 Qt Concurrent 프레임워크의 일부입니다.
위의 각 함수에는 QFuture 대신 최종 결과를 반환하는 블로킹 변형이 있습니다. 비동기 변형과 동일한 방식으로 사용합니다.
QStringList strings = ...; // each call blocks until the entire operation is finished QStringList lowerCaseStrings = QtConcurrent::blockingFiltered(strings, allLowerCase); QtConcurrent::blockingFilter(strings, allLowerCase); QSet<QString> dictionary = QtConcurrent::blockingFilteredReduced(strings, allLowerCase, addToDictionary);
위의 결과 유형은 QFuture 객체가 아니라 실제 결과 유형(이 경우 QStringList 및 QSet<QString>)이라는 점에 유의하세요.
동시 필터
QtConcurrent::filtered()는 입력 시퀀스와 필터 함수를 받습니다. 그런 다음 시퀀스의 각 항목에 대해 이 필터 함수가 호출되고 필터링된 값을 포함하는 새 시퀀스가 반환됩니다.
필터 함수는 다음과 같은 형식이어야 합니다:
bool function(const T &t);
T는 시퀀스에 저장된 유형과 일치해야 합니다. 이 함수는 항목을 유지해야 하는 경우 true
, 삭제해야 하는 경우 false를 반환합니다.
이 예는 QStringList 에서 모두 소문자인 문자열을 유지하는 방법을 보여줍니다:
bool allLowerCase(const QString &string) { return string.lowered() == string; } QStringList strings = ...; QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings, allLowerCase);
필터의 결과는 QFuture 을 통해 제공됩니다. 애플리케이션에서 QFuture 을 사용하는 방법에 대한 자세한 내용은 QFuture 및 QFutureWatcher 문서를 참조하세요.
시퀀스를 제자리에서 수정하려면 QtConcurrent::filter()를 사용합니다:
QStringList strings = ...; QFuture<void> future = QtConcurrent::filter(strings, allLowerCase);
시퀀스가 제자리에서 수정되기 때문에 QtConcurrent::filter()는 QFuture 를 통해 결과를 반환하지 않습니다. 하지만 QFuture 와 QFutureWatcher 를 사용하여 필터의 상태를 모니터링할 수 있습니다.
동시 필터-감소
QtConcurrent::filteredReduced()는 QtConcurrent::filtered()와 유사하지만 필터링된 결과가 포함된 시퀀스를 반환하는 대신 감소 함수를 사용하여 결과를 단일 값으로 결합합니다.
reduce 함수는 다음과 같은 형식이어야 합니다:
V function(T &result, const U &intermediate)
T는 최종 결과의 유형이고, U는 필터링되는 항목의 유형입니다. 축소 함수의 반환 값과 반환 유형은 사용되지 않는다는 점에 유의하세요.
QtConcurrent::filteredReduced()를 다음과 같이 호출합니다:
void addToDictionary(QSet<QString> &dictionary, const QString &string) { dictionary.insert(string); } QStringList strings = ...; QFuture<QSet<QString>> dictionary = QtConcurrent::filteredReduced(strings, allLowerCase, addToDictionary);
reduce 함수는 필터 함수에 의해 유지되는 각 결과에 대해 한 번씩 호출되며, 중간값을 결과 변수에 병합해야 합니다. QtConcurrent::filteredReduced()는 한 번에 하나의 스레드만 reduce를 호출하도록 보장하므로 결과 변수를 잠그기 위해 뮤텍스를 사용할 필요가 없습니다. QtConcurrent::ReduceOptions 열거형은 축소가 수행되는 순서를 제어할 수 있는 방법을 제공합니다.
추가 API 기능
시퀀스 대신 이터레이터 사용
위의 각 함수에는 시퀀스 대신 이터레이터 범위를 취하는 변형이 있습니다. 시퀀스 변형과 동일한 방식으로 사용할 수 있습니다:
QStringList strings = ...; QFuture<QString> lowerCaseStrings = QtConcurrent::filtered(strings.constBegin(), strings.constEnd(), allLowerCase); // filter in-place only works on non-const iterators QFuture<void> future = QtConcurrent::filter(strings.begin(), strings.end(), allLowerCase); QFuture<QSet<QString>> dictionary = QtConcurrent::filteredReduced(strings.constBegin(), strings.constEnd(), allLowerCase, addToDictionary);
멤버 함수 사용하기
QtConcurrent::filter(), QtConcurrent::filtered(), QtConcurrent::filteredReduced()는 멤버 함수에 대한 포인터를 받아들입니다. 멤버 함수 클래스 유형은 시퀀스에 저장된 유형과 일치해야 합니다:
// 알파 채널이 있는 이미지만 유지QList<QImage> images =...;QFuture<void> alphaImages = QtConcurrent::filter(images, &QImage::hasAlphaChannel);// 그레이 스케일 이미지 검색QList<QImage> images = ...;QFuture<QImage> 회색조 이미지 = =... QtConcurrent::filtered(images, &QImage::isGrayscale);// 인쇄 가능한 모든 문자 집합을 생성합니다.QList<QChar> 문자 = ...;QFuture<QSet<QChar>> set = QtConcurrent::filteredReduced(characters, qOverload<>(&QChar::isPrint), qOverload<const QChar&>(&QSet<QChar>::insert));
qOverload 을 사용하면 여러 개의 과부하가 있는 메서드의 모호성을 해결할 수 있습니다.
또한 QtConcurrent::filteredReduced()를 사용할 때 일반 함수와 멤버 함수를 자유롭게 섞어서 사용할 수 있다는 점에 유의하세요:
// 일반 함수와 멤버 함수를 섞어서 사용할 수 있습니다 // 모든 소문자 문자열의 딕셔너리를 만듭니다extern bool allLowerCase(const QString &string);QStringList strings = ...;QFuture<QSet<QString>> lowerCase = QtConcurrent::filteredReduced(strings, allLowerCase, qOverload<const QString&>(&QSet<QString>::insert)); // 모든 그레이 스케일 이미지의 콜라주 생성외부 void addToCollage(QImage &collage, const QImage &grayscaleImage);QList<QImage> images = ...;QFuture<QImage> collage = QtConcurrent::filteredReduced(images, &QImage::isGrayscale, addToCollage);
함수 객체 사용하기
QtConcurrent::filter(), QtConcurrent::filtered(), QtConcurrent::filteredReduced()는 필터 함수에 대한 함수 객체를 받아들입니다. 이러한 함수 객체는 함수 호출에 상태를 추가하는 데 사용할 수 있습니다:
struct StartsWith { StartsWith(const QString &string) : m_string(string) { } bool operator()(const QString &testString) { return testString.startsWith(m_string); } QString m_string; }; QList<QString> strings = ...; QFuture<QString> fooString = QtConcurrent::filtered(strings, StartsWith(QLatin1String("Foo")));
함수 객체는 reduce 함수에 대해서도 지원됩니다:
struct StringTransform { void operator()(QString &result, const QString &value); }; QFuture<QString> fooString = QtConcurrent::filteredReduced(strings, StartsWith(QLatin1String("Foo")), StringTransform());
람다 표현식 사용
QtConcurrent::filter(), QtConcurrent::filtered(), QtConcurrent::filteredReduced()는 필터 및 감소 함수에 대한 람다 표현식을 허용합니다:
// keep only even integers QList<int> list { 1, 2, 3, 4 }; QtConcurrent::blockingFilter(list, [](int n) { return (n & 1) == 0; }); // retrieve only even integers QList<int> list2 { 1, 2, 3, 4 }; QFuture<int> future = QtConcurrent::filtered(list2, [](int x) { return (x & 1) == 0; }); QList<int> results = future.results(); // add up all even integers QList<int> list3 { 1, 2, 3, 4 }; QFuture<int> sum = QtConcurrent::filteredReduced(list3, [](int x) { return (x & 1) == 0; }, [](int &sum, int x) { sum += x; } );
QtConcurrent::filteredReduced() 또는 QtConcurrent::blockingFilteredReduced()를 사용할 때는 일반 함수, 멤버 함수, 람다 식을 자유롭게 섞어서 사용할 수 있습니다.
void intSumReduce(int &sum, int x) { sum += x; } QList<int> list { 1, 2, 3, 4 }; QFuture<int> sum = QtConcurrent::filteredReduced(list, [] (int x) { return (x & 1) == 0; }, intSumReduce );
람다를 리듀스 객체로 전달할 수도 있습니다:
bool keepEvenIntegers(int x) { return (x & 1) == 0; } QList<int> list { 1, 2, 3, 4 }; QFuture<int> sum = QtConcurrent::filteredReduced(list, keepEvenIntegers, [](int &sum, int x) { sum += x; } );
여러 인수를 받는 함수 래핑하기
인수가 두 개 이상인 필터 함수를 사용하려면 람다 함수 또는 std::bind()
을 사용하여 하나의 인수를 받는 함수로 변환할 수 있습니다.
예를 들어 QString::contains()를 사용합니다:
bool QString::contains(const QRegularExpression ®exp) const;
QString::contains()는 두 개의 인수("this" 포인터 포함)를 취하며, QtConcurrent::filtered()는 하나의 인수를 취하는 함수를 기대하기 때문에 직접 QtConcurrent::filtered()와 함께 사용할 수 없습니다. QString::contains ()를 QtConcurrent::filtered()와 함께 사용하려면 정규식 인자에 대한 값을 제공해야 합니다:
QStringList strings = ...; QFuture<QString> future = QtConcurrent::filtered(list, [](const QString &str) { return str.contains(QRegularExpression("^\\S+$")); // matches strings without whitespace });
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.