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 (PySide). In doing so, it facilitates communication between the
main (GUI) thread and a dedicated QThreads as offered by Qt for Python.
Installation
Pqthreads comes as a package in the Python Package Index (PyPi) and can be installed using pip:
pip install pqthreads
Usage
In order to use pqhreads, you'll first need a GUI implementation (in Qt for Python (PySide)) whose interface you'd like to expose. Then you need to have a corresponding class that exposes (chosen) methods and attributes in another thread.
GUI implementation
The GUI implementation would be a class that derives from
QWidget, e.g.
QMainWindow.
A very basic example class called FigureWindow can be found in
window.py.
Worker class
In a class that inherits from
containers.WorkerItem
you then choose which methods and attributes are exposed. An examples of this is
the class FigureWorker as found in
worker.py.
Putting it all together
Using the GUI implementation FigureWindow and worker 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 will run 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 refs
def figure(*args, **kwargs):
""" Create, raise or modify FigureWorker objects """
container = refs.worker.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.
from pqthreads.examples import worker
@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 references (as is provided in the module
refs).
All interfaces are provided as weak references. As soon as the decorated
function is exited, the weak references will invalidate and can't be used
anymore. Therefore, it's recommended to decorate the function that encapsulates
the whole python program in question. This assures that different parts of your
own program run in different threads.
The module refs also comes with an object called gui
references that stores weak references to the GUI objects. These also will
invalidate when the decorated function is exited.
Finally, one should not access the worker references from the (main) GUI
thread and also not access gui references 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.
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.
Pre-commit hooks
This repository comes with pre-commit hooks, which are stored in
.hooks. To enable the hooks issue:
git config --local core.hooksPath .hooks/
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pqthreads-0.6.0.tar.gz.
File metadata
- Download URL: pqthreads-0.6.0.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4eca24ad58b621a1fdcc313acdefd9392f3026c2665720d9ce95badfbb1f384e
|
|
| MD5 |
fe2f7d4ae50e4a64f148e0ef551b9588
|
|
| BLAKE2b-256 |
ca983710143761e68fd10efdc24df208f4c6fda063ecbcef0c13b4c63af3a280
|
Provenance
The following attestation bundles were made for pqthreads-0.6.0.tar.gz:
Publisher:
push.yaml on swvanbuuren/pqthreads
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pqthreads-0.6.0.tar.gz -
Subject digest:
4eca24ad58b621a1fdcc313acdefd9392f3026c2665720d9ce95badfbb1f384e - Sigstore transparency entry: 837500047
- Sigstore integration time:
-
Permalink:
swvanbuuren/pqthreads@e720a2d257ca68884e298735a8a90a9a71a2c0d4 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/swvanbuuren
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
push.yaml@e720a2d257ca68884e298735a8a90a9a71a2c0d4 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pqthreads-0.6.0-py3-none-any.whl.
File metadata
- Download URL: pqthreads-0.6.0-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a65d0527f0c0fea78c549ccba7fd26c9b5a948c5ee877262a5f3e69b3ef35dd
|
|
| MD5 |
9a774779f3c1e1404505fcca5abdb7d0
|
|
| BLAKE2b-256 |
fbea7cac8e13b1027874f40c82b2a6a8d86e77e0cce8776c2c9a368505bf69cf
|
Provenance
The following attestation bundles were made for pqthreads-0.6.0-py3-none-any.whl:
Publisher:
push.yaml on swvanbuuren/pqthreads
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pqthreads-0.6.0-py3-none-any.whl -
Subject digest:
8a65d0527f0c0fea78c549ccba7fd26c9b5a948c5ee877262a5f3e69b3ef35dd - Sigstore transparency entry: 837500082
- Sigstore integration time:
-
Permalink:
swvanbuuren/pqthreads@e720a2d257ca68884e298735a8a90a9a71a2c0d4 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/swvanbuuren
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
push.yaml@e720a2d257ca68884e298735a8a90a9a71a2c0d4 -
Trigger Event:
push
-
Statement type: