Home

QtService Class Reference

The QtService is a convenient template class that allows you to create a service for a particular application type. More...

 #include <QtService>

Inherits QtServiceBase.

Public Functions

Protected Functions

Additional Inherited Members


Detailed Description

The QtService is a convenient template class that allows you to create a service for a particular application type.

A Windows service or Unix daemon (a "service"), is a program that runs "in the background" independently of whether a user is logged in or not. A service is often set up to start when the machine boots up, and will typically run continuously as long as the machine is on.

Services are usually non-interactive console applications. User interaction, if required, is usually implemented in a separate, normal GUI application that communicates with the service through an IPC channel. For simple communication, QtServiceController::sendCommand() and QtService::processCommand() may be used, possibly in combination with a shared settings file. For more complex, interactive communication, a custom IPC channel should be used, e.g. based on Qt's networking classes. (In certain circumstances, a service may provide a GUI itself, ref. the "interactive" example documentation).

Note: On Unix systems, this class relies on facilities provided by the QtNetwork module, provided as part of the Qt Open Source Edition and certain Qt Commercial Editions.

The QtService class functionality is inherited from QtServiceBase, but in addition the QtService class binds an instance of QtServiceBase with an application type.

Typically, you will create a service by subclassing the QtService template class. For example:

 class MyService : public QtService<QApplication>
 {
 public:
     MyService(int argc, char **argv);
     ~MyService();

 protected:
     void start();
     void stop();
     void pause();
     void resume();
     void processCommand(int code);
 };

The application type can be QCoreApplication for services without GUI, QApplication for services with GUI or you can use your own custom application type.

You must reimplement the QtServiceBase::start() function to perform the service's work. Usually you create some main object on the heap which is the heart of your service.

In addition, you might want to reimplement the QtServiceBase::pause(), QtServiceBase::processCommand(), QtServiceBase::resume() and QtServiceBase::stop() to intervene the service's process on controller requests. You can control any given service using an instance of the QtServiceController class which also allows you to control services from separate applications. The mentioned functions are all virtual and won't do anything unless they are reimplemented.

Your custom service is typically instantiated in the application's main function. Then the main function will call your service's exec() function, and return the result of that call. For example:

     int main(int argc, char **argv)
     {
         MyService service(argc, argv);
         return service.exec();
     }

When the exec() function is called, it will parse the service specific arguments passed in argv, perform the required actions, and exit.

If none of the arguments is recognized as service specific, exec() will first call the createApplication() function, then executeApplication() and finally the start() function. In the end, exec() returns while the service continues in its own process waiting for commands from the service controller.

See also QtServiceBase and QtServiceController.


Member Function Documentation

QtService::QtService ( int argc, char ** argv, const QString & name )

Constructs a QtService object called name. The argc and argv parameters are parsed after the exec() function has been called. Then they are passed to the application's constructor.

There can only be one QtService object in a process.

See also QtServiceBase().

QtService::~QtService ()

Destroys the service object.

Application * QtService::application () const   [protected]

Returns a pointer to the application object.


Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Solutions