Now you have the running job and a user can see nice progressbar. However, there is no visible information what job is currently running and what it is doing. To change it we have to emit signal '''KJob.description''' in our method '''MyJob.doWork''':

The parameters of this signal are the sending job (anything else than '''self''' doesn't make sense here), job title, and two tuples consising of two strings arbitrary that can be used to denote some job details. (consider them as a key-value pair).

=== Conclusion ===

=== Conclusion ===

Here is the [[Development/Languages/Python/PyKDE_Knotify_Tutorial/MyJob.py|full code]] of this example.

Here is the [[Development/Languages/Python/PyKDE_Knotify_Tutorial/MyJob.py|full code]] of this example.

−

Is is very easy to create a Job and make it visible to normal users. Unfortunately I have problems with setting the heading and description, so this will be added in later versions.

The aim of this tutorial is to give a sample class to include Notifications and Jobs to your own python programms, so the user can see what is going on (for example a backup system, that tells you that is backuping right now). It assumes a basic working knowledge of Python and PyKDE4.
For now only the Job part is mentioned, 'cause that is the only part I've created right now.

from PyQt4.QtCoreimport QObject,QTimer,QString
from PyKDE4.kdecoreimport KJob
class MyJob(KJob):
def__init__(self,parent=QObject()):
KJob.__init__(self,parent)#We want to have a Suspenable and Killable Jobself.setCapabilities(KJob.Capabilities(KJob.Suspendable|KJob.Killable))#Just a maximum Variableself.max=25#indexself.i=0def start(self):
#initalizing for workself.i=0#start the actual work in another thread#this function has to terminate, before the work is done
QTimer().singleShot(0,self.doWork)def doWork(self):
#the actual work try:
#if we are killed or suspended just return ifself.error()orself.isSuspended():
returnexceptRuntimeError:
#if this class is killed before a RuntimeError will raisereturn#do a peace of hard workself.i+=1#fortunately we have made a peace of work #-> show this to everybody
KJob.setPercent(self,self.i*4)ifself.i==self.max:
#jeah we have done the bunch of work#send the result signal for showing, that we ended the workself.emitResult()return#just go to sleep for 1000ms than go on
QTimer().singleShot(1000,self.doWork)def doSuspend(self):
#the surounding function from KJob makes the isSuspended() become True#returns True for signaling that this class supports to suspendreturnTruedef doResume(self):
#start with work again
QTimer().singleShot(0,self.doWork)#return True for signaling that this class supports resumingreturnTruedef doKill(self):
#return True for signaling that we support killingreturnTrue

MyJob.__init__: First we create the class and initialize the counter i and the maximum max. To make is visible for others we set the Capacities, so this job is suspendable and killable.

MyJob.start: Here the pre working setting are made, we will se later for what we use this. After initalizing the Work we start the actual work by using a QTimer. The start method should start the asyncron.

MyJob.doWork: Do actual work and handle, if this job is killed or suspended.

MyJob.doSuspend/MyJob.doResume/MyJob.doKill: These function indicate that this feature is supported by this class.

Till now, a normal user doesn't see anything what is going on. To change it we have to register the Job to a JobTracker. Now a user will see a progressbar and a suspend and a stop button. So we modify the start method:

Now you have the running job and a user can see nice progressbar. However, there is no visible information what job is currently running and what it is doing. To change it we have to emit signal KJob.description in our method MyJob.doWork:

def doWork(self):
#the actual work try:
#if we are killed or suspended just return ifself.error()orself.isSuspended():
returnexceptRuntimeError:
#if this class is killed before a RuntimeError will raisereturn#do a peace of hard workself.i+=1#fortunately we have made a peace of work #-> show this to everybody
KJob.setPercent(self,self.i*4)self.description.emit(self,"Our Splendid Job",("Source","counter"),("Destination","dummy"))ifself.i==self.max:
#jeah we have done the bunch of work#send the result signal for showing, that we ended the workself.emitResult()return#just go to sleep for 1000ms than go on
QTimer().singleShot(1000,self.doWork)

The parameters of this signal are the sending job (anything else than self doesn't make sense here), job title, and two tuples consising of two strings arbitrary that can be used to denote some job details. (consider them as a key-value pair).