Skip to main content

Expose class interfaces from the main GUI Thread in another QThread in Qt for Python

Project description

Pqthreads

Pqthreads exposes class interfaces from the main GUI Thread in another QThread in Qt for Python. In doing so, it facilitates communication between the main (GUI) thread and a dedicated QThreads as offered by Qt for Python (PySide).

Usage

In order to use pqhreads, you'll first need a GUI implementation (with Qt for Python) whose interface you'd like to expose. This usually would be a class that derives from e.g. QMainWindow. A very basic example class called FigureWindow can be found in window.py.

Using a class that inherites from containers.WorkerItem you then choose which methods and attributes are exposed. An examples of this would be the class FigureWorker in worker.py.

Using the GUI implementation FigureWindow and worker threads exposure class FigureWorker the utilities from decorator.py can be used to create a custom decorator:

from pqthreads import decorator

DecoratorCore = decorator.DecoratorCore
DecoratorCore.add_agent('figure', window.FigureWindow, FigureWorker)
decorator_example = decorator.Decorator(DecoratorCore)

Any decorated function now runs in the worker thread, while all GUI elements run in the (main) GUI thread.

To simplify access to worker class interfaces, a helper function is useful. This also illustrates how to create and access new GUI elements:

from pqthreads import decorator

def figure(*args, **kwargs):
    """ Create, raise or modify FigureWorker objects """
    container = controllers.worker_refs.get('figure')
    if not args:
        return container.create(**kwargs)
    figure_worker = args[0]
    container.current = figure_worker
    return figure_worker

This can finally be used to to expose GUI implementation in an existing python program that will run in another worker thread.

@decorator_example
def main():
    fig = worker.figure(title='Initial title')
    fig.change_title(title='Another title')
    fig.close()

Pittfalls

As illustrated in the previous section worker class interfaces are accessed through the so-called worker_refs, which is provided in the module controllers. All interfaces are provided as weak references. As soon as the decorated function is exited, the weak references will invalidate! Therefore, it's recommended to decorate the function that encompases the whole python program in question.

The module controllers also comes with an object called ``gui_refs` that stores weak references to the GUI objects. These also will invalidate when the decorated function is left.

Finally, one should not access the worker_refs from the (main) GUI thread and also not access gui_refs from the worker thread. This can lead to trace errors and all sorts of undefined behavior. You have been warned!

Design

Pqthreads separates the GUI elements from all programming elements in QThreads. Since Qt demands that the main thread is used for GUI elements, all other programming functionalities are moved into a dedicated QThread. This shall be called the worker threads.

The following schematic depicts this design.

Pqthreads design

Communication between the GUI and worker threads is solely done using Signal/Slot connections. This is facilitated by the GUIAgents and WorkerAgents.

The interface of a GUI Object is exposed by means of a Worker Object in the Worker Thread.

It is possible expose the interface of multiple types of GUI Objects (in the shown schematic FigureWindow and GraphWindow), which requires multiple Worker- and GUIAgents. These are held the GUIAgency and WorkerAgency respectively.

For each type of GUI/Worker object pair, it's also possible to instantiate multiple objects (of the same type). GUI and Worker objects are held in the GUIItemContainer and WorkerItemContainer respectively.

License

An MIT style license applies for pqthreads, see the LICENSE file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pqthreads-0.1.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

pqthreads-0.1-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file pqthreads-0.1.tar.gz.

File metadata

  • Download URL: pqthreads-0.1.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for pqthreads-0.1.tar.gz
Algorithm Hash digest
SHA256 5b0ffb74fd7e4f7952c97c977942c213bebd6480b4a47ec6eb87d7eae4106505
MD5 6c24809caaa75b13837c6adb85e3fd44
BLAKE2b-256 96ff55d7d8423076b3ee41f168a6ffb290a24dfd997b45081defc09209bf62a5

See more details on using hashes here.

Provenance

File details

Details for the file pqthreads-0.1-py3-none-any.whl.

File metadata

  • Download URL: pqthreads-0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for pqthreads-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4557f5f884cbaa20cdc79e4ff4edc57cd2a137ba626f8a47ab7b6a751c4a77ce
MD5 210fb0daf4a34e36a0df12df5271b689
BLAKE2b-256 a2bc3d841dfade042fb4b083e89fa46bc3db90f52955138e0b656ad4f5ed1852

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page