Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Learn BlackBerry 10 App Development
< Prev   CONTENTS   Next >

QThread

It is very important not to block the main UI thread when developing Cascades applications.

You should therefore always execute long-running operations in a secondary thread so that the main UI thread stays as responsive as possible. A thread is simply an independent execution flow within your application. In other words, threads can share your application's data but simply run independently (a thread is also often called a lightweight process). In Qt, a thread is managed by an instance of the QThread class. This section shows you how to effectively execute a long-running operation using a QThread object. As with many things in Qt, it is mostly achieved using signals and slots.

Before starting a new thread, you need to package your workload as a worker object (see Listing 3-33).

Listing 3-33. Worker.h

class Worker : public QObject{

Q_OBJECT

public:

Worker();

virtual ~Worker(); public slots:

void doWork(); // do the processing here signals:

void finished(double result); void error(QString error);

};

The worker declares a Worker::doWork() that will be called to start the processing and a finished() signal that will be emitted once the workload has been completed (in other words, the finished() signal will be emitted at the end of Worker::doWork(); see Listing 3-34).

Listing 3-34. Worker.cpp

Worker::doWork(){

// do the long processing here emit finished(result);

}

Assuming that the application delegate is responsible for launching the new thread, it needs to move the Worker object to the QThread object and start the new thread to perform the workload (see Listing 3-35).

Listing 3-35. ApplicationUI.cpp

voidApplicationUI::doWorkAsynch() { QThread* thread = new QThread; Worker* worker = new Worker;

worker->moveToThread(thread);

connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString))); connect(thread, SIGNAL(started()), worker, SLOT(doWork())); connect(worker, SIGNAL(finished(double)), this, SLOT(finished(double))); connect(worker, SIGNAL(finished(double)), worker, SLOT(deleteLater())); connect(worker, SIGNAL(finished(double)), thread, SLOT(quit())); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

thread->start();

}

As illustrated in the Listing 3-35, the Worker::doWork() method is called when the thread's started() signal is emitted (the signal is emitted when QThread::start() is called). When the worker object has completed the long-running task, it emits the finished() signal, which could be used to pass

a result back to the application delegate, for example. Note also that the Worker::finished() and QThread::finished() signals are also used to handle cleanup and make sure dynamically allocated memory is reclaimed (in both cases QObject::deleteLater() is used to schedule the objects for deletion).

Summary

Congratulations! By now you know enough to start designing complex applications using QML, JavaScript, Qt, and C++. This chapter has been quite dense, so let's do a quick recap.

C++ is a complex language, but we got to the essentials for building object-oriented programs.

In C++, you can override a function in a child class if it has been declared as virtual in the parent class. Having a pure virtual function in a class will effectively make that class abstract. Polymorphism is achieved in C++ through references or pointers to objects. C++ also makes the distinction between value types and references types, which you don't find in languages such as Java, where everything is a reference (except primitives types such int, double, float, boolean, etc.).

By using the MVC pattern, you discovered how to organize your application objects with clearly defined boundaries and responsibilities. This will help you cope with complexity and accommodate change as your application design evolves. The following chapters will build on the foundations presented here and show you how to design beautiful UIs using the Cascades framework. You will master the Cascades core controls, as well as the more advanced ones, integrate with platform services, use the device sensors—and there are many more exciting things to come. From now on, the truly fun topics begin…

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel