eqt


Nameeqt JSON
Version 1.0.2 PyPI version JSON
download
home_pageNone
SummaryA number of templates and tools to develop Qt GUIs with Python effectively
upload_time2024-12-18 16:52:17
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseApache-2.0
keywords qt
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # eqt: Qt Elements

[![Tests](https://img.shields.io/github/actions/workflow/status/TomographicImaging/eqt/test.yml?branch=main&label=Tests&logo=GitHub)](https://github.com/TomographicImaging/eqt/actions?query=branch%3Amain) [![PyPI](https://img.shields.io/pypi/v/eqt.svg?logo=pypi&logoColor=white)](https://pypi.org/project/eqt) [![Conda](https://img.shields.io/conda/v/conda-forge/eqt.svg?label=conda-forge&logo=conda-forge)](https://anaconda.org/conda-forge/eqt)

Templates & tools to develop Qt GUIs in Python.

One use case is accepting user input while running another task asynchronously (so that the UI is still responsive).

1. `UIFormWidget`: a class to help creating Qt forms programmatically, useable in `QDockWidgets` and `QWidget`
2. `FormDialog`: a `QDialog` with a form inside with <kbd>OK</kbd> and <kbd>Cancel</kbd> buttons
3. `Worker`: a class that defines a `QRunnable` to handle worker thread setup, signals and wrap up

## Installation

Via `pip`/`conda`/`mamba`, i.e. any of the following:

- `python -m pip install eqt`
- `conda install -c conda-forge eqt`
- `mamba install -c conda-forge eqt`

## Examples

See the [`examples`](examples) directory, e.g. how to launch a `QDialog` with a form inside using `eqt`'s [`QWidget`](examples/dialog_example.py) or [`FormDialog`](examples/dialog_example_2.py).

### Running asynchronous tasks

To run a function in a separate thread we use a `Worker` which is a subclass of a `QRunnable`.

For the `Worker` to work one needs to define:

1. the function that does what you need
2. Optional callback methods to get the status of the thread by means of `QtCore.QSignal`s

On [initialisation](https://github.com/TomographicImaging/eqt/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L32-L38) of the `Worker` the user needs to pass the function that has to run in the thread, i.e. `fn` below, and additional optional positional and keyword arguments, which will be passed on to the actual function that is run in the `QRunnable`.

```python
class Worker(QtCore.QRunnable):
    def __init__(self, fn, *args, **kwargs):
        self.fn = fn
        self.args = args
        self.kwargs = kwargs
        self.signals = WorkerSignals()
```

In practice the user will need to pass to the `Worker` as many parameters as there are listed in the [function](https://github.com/TomographicImaging/eqt/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L56) to be run.

```python
result = self.fn(*self.args, **self.kwargs)
```

But `Worker` will [add](https://github.com/TomographicImaging/eqt/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L41-L43) to the `**kwargs` the following `QSignal`.

```python
        # Add progress callback to kwargs
        self.kwargs['progress_callback'] = self.signals.progress
        self.kwargs['message_callback'] = self.signals.message
        self.kwargs['status_callback'] = self.signals.status
```

Therefore it is advisable to always have `**kwargs` in the function `fn` signature so that you can access the `QSignal` and emit the signal required. For instance one could emit a progress by:

```python
def fn(num_iter, **kwargs):
    progress_callback = kwargs.get('progress_callback', None)
    for i in range(num_iter):
        do_something
        if progress_callback is not None:
            progress_callback.emit( i )
```

### Passing a signal to a Worker

This is done just after one has defined the `Worker`:

```python
def handle_progress(num_iter):
    # do something with the progress
    print ("Current progress is ", num_iter)

worker = Worker(fn, 10)
worker.signals.progress.connect(handle_progress)
```

So, each time `fn` comes to `progress_callback.emit( i )` the function `handle_progress` will be called with the parameter `i` of its `for` loop.

### Signals available

The signals that are available in the `Worker` class are defined in [`WorkerSignal`](https://github.com/TomographicImaging/eqt/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L66) and are the following. Below you can also see the type of data that each signal can emit.

```python
finished = QtCore.Signal()
error = QtCore.Signal(tuple)
result = QtCore.Signal(object)

progress = QtCore.Signal(int)
message = QtCore.Signal(str)
status = QtCore.Signal(tuple)
```

Read more on [Qt signals and slots](https://doc.qt.io/qt-5/signalsandslots.html) and on how to use them in [PySide2](https://wiki.qt.io/Qt_for_Python_Signals_and_Slots).

## Developer Contribution Guide
See [CONTRIBUTING.md](./CONTRIBUTING.md).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "eqt",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "Casper da Costa-Luis <casper.dcl@physics.org>",
    "keywords": "Qt",
    "author": null,
    "author_email": "Edoardo Pasca <edoardo.pasca@stfc.ac.uk>, Laura Murgatroyd <laura.murgatroyd@stfc.ac.uk>",
    "download_url": "https://files.pythonhosted.org/packages/98/19/c1d68fe7087f7e563037f6d4ffbb25a2c77eeee1584dd803038976a9c97d/eqt-1.0.2.tar.gz",
    "platform": null,
    "description": "# eqt: Qt Elements\n\n[![Tests](https://img.shields.io/github/actions/workflow/status/TomographicImaging/eqt/test.yml?branch=main&label=Tests&logo=GitHub)](https://github.com/TomographicImaging/eqt/actions?query=branch%3Amain) [![PyPI](https://img.shields.io/pypi/v/eqt.svg?logo=pypi&logoColor=white)](https://pypi.org/project/eqt) [![Conda](https://img.shields.io/conda/v/conda-forge/eqt.svg?label=conda-forge&logo=conda-forge)](https://anaconda.org/conda-forge/eqt)\n\nTemplates & tools to develop Qt GUIs in Python.\n\nOne use case is accepting user input while running another task asynchronously (so that the UI is still responsive).\n\n1. `UIFormWidget`: a class to help creating Qt forms programmatically, useable in `QDockWidgets` and `QWidget`\n2. `FormDialog`: a `QDialog` with a form inside with <kbd>OK</kbd> and <kbd>Cancel</kbd> buttons\n3. `Worker`: a class that defines a `QRunnable` to handle worker thread setup, signals and wrap up\n\n## Installation\n\nVia `pip`/`conda`/`mamba`, i.e. any of the following:\n\n- `python -m pip install eqt`\n- `conda install -c conda-forge eqt`\n- `mamba install -c conda-forge eqt`\n\n## Examples\n\nSee the [`examples`](examples) directory, e.g. how to launch a `QDialog` with a form inside using `eqt`'s [`QWidget`](examples/dialog_example.py) or [`FormDialog`](examples/dialog_example_2.py).\n\n### Running asynchronous tasks\n\nTo run a function in a separate thread we use a `Worker` which is a subclass of a `QRunnable`.\n\nFor the `Worker` to work one needs to define:\n\n1. the function that does what you need\n2. Optional callback methods to get the status of the thread by means of `QtCore.QSignal`s\n\nOn [initialisation](https://github.com/TomographicImaging/eqt/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L32-L38) of the `Worker` the user needs to pass the function that has to run in the thread, i.e. `fn` below, and additional optional positional and keyword arguments, which will be passed on to the actual function that is run in the `QRunnable`.\n\n```python\nclass Worker(QtCore.QRunnable):\n    def __init__(self, fn, *args, **kwargs):\n        self.fn = fn\n        self.args = args\n        self.kwargs = kwargs\n        self.signals = WorkerSignals()\n```\n\nIn practice the user will need to pass to the `Worker` as many parameters as there are listed in the [function](https://github.com/TomographicImaging/eqt/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L56) to be run.\n\n```python\nresult = self.fn(*self.args, **self.kwargs)\n```\n\nBut `Worker` will [add](https://github.com/TomographicImaging/eqt/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L41-L43) to the `**kwargs` the following `QSignal`.\n\n```python\n        # Add progress callback to kwargs\n        self.kwargs['progress_callback'] = self.signals.progress\n        self.kwargs['message_callback'] = self.signals.message\n        self.kwargs['status_callback'] = self.signals.status\n```\n\nTherefore it is advisable to always have `**kwargs` in the function `fn` signature so that you can access the `QSignal` and emit the signal required. For instance one could emit a progress by:\n\n```python\ndef fn(num_iter, **kwargs):\n    progress_callback = kwargs.get('progress_callback', None)\n    for i in range(num_iter):\n        do_something\n        if progress_callback is not None:\n            progress_callback.emit( i )\n```\n\n### Passing a signal to a Worker\n\nThis is done just after one has defined the `Worker`:\n\n```python\ndef handle_progress(num_iter):\n    # do something with the progress\n    print (\"Current progress is \", num_iter)\n\nworker = Worker(fn, 10)\nworker.signals.progress.connect(handle_progress)\n```\n\nSo, each time `fn` comes to `progress_callback.emit( i )` the function `handle_progress` will be called with the parameter `i` of its `for` loop.\n\n### Signals available\n\nThe signals that are available in the `Worker` class are defined in [`WorkerSignal`](https://github.com/TomographicImaging/eqt/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L66) and are the following. Below you can also see the type of data that each signal can emit.\n\n```python\nfinished = QtCore.Signal()\nerror = QtCore.Signal(tuple)\nresult = QtCore.Signal(object)\n\nprogress = QtCore.Signal(int)\nmessage = QtCore.Signal(str)\nstatus = QtCore.Signal(tuple)\n```\n\nRead more on [Qt signals and slots](https://doc.qt.io/qt-5/signalsandslots.html) and on how to use them in [PySide2](https://wiki.qt.io/Qt_for_Python_Signals_and_Slots).\n\n## Developer Contribution Guide\nSee [CONTRIBUTING.md](./CONTRIBUTING.md).\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "A number of templates and tools to develop Qt GUIs with Python effectively",
    "version": "1.0.2",
    "project_urls": {
        "changelog": "https://github.com/TomographicImaging/eqt/releases",
        "documentation": "https://github.com/TomographicImaging/eqt#readme",
        "repository": "https://github.com/TomographicImaging/eqt"
    },
    "split_keywords": [
        "qt"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "96266f3e8dd47f42f98f636249b800b6f1ad2f81da6db932ce93d6a2c72fa608",
                "md5": "d0a74856026abe83ff74d04e29af195d",
                "sha256": "f6930c7cf7f7ad806e72ccb09e930ddc0bee885bbebe5993c86f155ba2927cec"
            },
            "downloads": -1,
            "filename": "eqt-1.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d0a74856026abe83ff74d04e29af195d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 31734,
            "upload_time": "2024-12-18T16:52:16",
            "upload_time_iso_8601": "2024-12-18T16:52:16.166476Z",
            "url": "https://files.pythonhosted.org/packages/96/26/6f3e8dd47f42f98f636249b800b6f1ad2f81da6db932ce93d6a2c72fa608/eqt-1.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9819c1d68fe7087f7e563037f6d4ffbb25a2c77eeee1584dd803038976a9c97d",
                "md5": "ded241420b925f09744193691d2513a6",
                "sha256": "11a94cac20ef84473551cb40cb42bf3420c2257087f32d5eb73de2369fa9a40d"
            },
            "downloads": -1,
            "filename": "eqt-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "ded241420b925f09744193691d2513a6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 51926,
            "upload_time": "2024-12-18T16:52:17",
            "upload_time_iso_8601": "2024-12-18T16:52:17.689938Z",
            "url": "https://files.pythonhosted.org/packages/98/19/c1d68fe7087f7e563037f6d4ffbb25a2c77eeee1584dd803038976a9c97d/eqt-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-18 16:52:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "TomographicImaging",
    "github_project": "eqt",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "eqt"
}
        
Elapsed time: 0.46753s