<QtConcurrentRun> - Asynchronous RunThe <QtConcurrentRun> header provides a way to run a function in a separate thread. More... Functions
This function is a part of the Qt Concurrent framework. The QtConcurrent::run() function runs a function in a separate thread. The return value of the function is made available through the QFuture API. Running a Function in a Separate ThreadTo run a function in another thread, use QtConcurrent::run(): extern void aFunction(); QFuture<void> future = QtConcurrent::run(aFunction); This will run aFunction in a separate thread obtained from the default QThreadPool. You can use the QFuture and QFutureWatcher classes to monitor the status of the function. Passing Arguments to the FunctionPassing arguments to the function is done by adding them to the QtConcurrent::run() call immediately after the function name. For example: extern void aFunctionWithArguments(int arg1, double arg2, const QString &string); int integer = ...; double floatingPoint = ...; QString string = ...; QFuture<void> future = QtConcurrent::run(aFunctionWithArguments, integer, floatingPoint, string); A copy of each argument is made at the point where QtConcurrent::run() is called, and these values are passed to the thread when it begins executing the function. Changes made to the arguments after calling QtConcurrent::run() are not visible to the thread. Returning Values from the FunctionAny return value from the function is available via QFuture: extern QString functionReturningAString(); QFuture<QString> future = QtConcurrent::run(functionReturningAString); ... QString result = future.result(); As documented above, passing arguments is done like this: extern QString someFunction(const QByteArray &input); QByteArray bytearray = ...; QFuture<QString> future = QtConcurrent::run(someFunction, bytearray); ... QString result = future.result(); Note that the QFuture::result() function blocks and waits for the result to become available. Use QFutureWatcher to get notification when the function has finished execution and the result is available. Additional API FeaturesUsing Member FunctionsQtConcurrent::run() also accepts pointers to member functions. The first argument must be either a const reference or a pointer to an instance of the class. Passing by const reference is useful when calling const member functions; passing by pointer is useful for calling non-const member functions that modify the instance. For example, calling QByteArray::split() (a const member function) in a separate thread is done like this: // call 'QList<QByteArray> QByteArray::split(char sep) const' in a separate thread QByteArray bytearray = "hello world"; QFuture<QList<QByteArray> > future = QtConcurrent::run(bytearray, &QByteArray::split, ','); ... QList<QByteArray> result = future.result(); Calling a non-const member function is done like this: // call 'void QImage::invertPixels(InvertMode mode)' in a separate thread QImage image = ...; QFuture<void> future = QtConcurrent::run(&image, &QImage::invertPixels, QImage::InvertRgba); ... future.waitForFinished(); // At this point, the pixels in 'image' have been inverted Using Bound Function ArgumentsNote that Qt does not provide support for bound functions. This is provided by 3rd party libraries like Boost or C++ TR1 Library Extensions. You can use boost::bind() or std::tr1::bind() to bind a number of arguments to a function when called. There are number of reasons for doing this:
See the documentation for the relevant functions for details on how to use the bind API. Calling a bound function is done like this: void someFunction(int arg1, double arg2); QFuture<void> future = QtConcurrent::run(boost::bind(someFunction, 1, 2.0)); ... Function Documentation
|
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.8 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com