msl.qt.threading module

Base classes for starting a process in a new QThread.

class msl.qt.threading.Worker(*args, **kwargs)[source]

Bases: QObject

Process an expensive or blocking operation in a thread that is separate from the main thread.

You can access to the attributes of the Worker as though they are attributes of the Thread.

The *args and **kwargs parameters are passed to the Worker when the Thread.start() method is called.

Example

See SleepWorker for an example of a Worker.

process()[source]

The expensive or blocking operation to process.

Attention

You must override this method.

class msl.qt.threading.Thread(worker)[source]

Bases: QObject

Moves a Worker to a new QtCore.QThread.

Parameters

worker – A Worker subclass that has not been instantiated.

Example

See Sleep for an example of a Thread.

finished

A Signal that is emitted when the thread is finished (i.e., when Worker.process() finishes).

__getattr__(item)[source]

All other attributes are assumed to be those of the Worker.

error_handler(exception, traceback)[source]

If an exception is raised by the Worker then the default behaviour is to show the error message in a critical() dialog window.

You can override this method to implement your own error handler.

Parameters
is_finished()[source]

Whether the thread is finished.

Returns

boolTrue if the thread finished otherwise False.

is_running()[source]

Whether the thread is running.

Returns

boolTrue if the thread is running otherwise False.

quit()[source]

Tells the thread’s event loop to exit.

start(*args, **kwargs)[source]

Start processing the Worker in a QtCore.QThread.

The *args and **kwargs are passed to the constructor of the Worker class.

stop(milliseconds=None)[source]

Calls quit() then wait().

wait(milliseconds=None)[source]

Wait for the thread to either finish or timeout.

Parameters

milliseconds (int, optional) – The number of milliseconds to wait before a timeout occurs. If None then this method will never time out and the thread must return from its run method.

Returns

bool or NoneTrue if the thread finished, False if the thread timed out or None if the thread is not running.

worker_connect(signal, slot)[source]

Connect a Signal from the Worker to a Slot.

This method is intended to be called before a worker thread starts. Although, you can still call this method when a worker thread is running, it is easier (fewer characters to type) to access the attributes of a Worker as though they are attributes of the Thread.

Parameters
  • signal (str, Signal or SignalInstance) – The signal to connect the slot to. If a str, then either the name of a class attribute of the Worker or the name parameter that was used in the Signal constructor.

  • slot – A callable function to use as the Slot.

worker_disconnect(signal, slot)[source]

Disconnect a Slot from a Signal of the Worker.

This method is intended to be called before a worker thread is restarted. Although, you can still call this method when a worker thread is running, it is easier (fewer characters to type) to access the attributes of a Worker as though they are attributes of the Thread.

Parameters