eqt


Nameeqt JSON
Version 1.0.0 PyPI version JSON
download
home_pageNone
SummaryA number of templates and tools to develop Qt GUIs with Python effectively
upload_time2024-04-25 13:43:50
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/f8/03/a416014a217dce5b4b0caacedcc7b2981b412b3fa2c2f54292e9d4890340/eqt-1.0.0.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.0",
    "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": "eea65b255d8f4007adc765cef0d5fbbc8383b0f61aec938ad7baf53bd4eb7505",
                "md5": "1c4377500d6d52a836f3bac0f4a4aa77",
                "sha256": "2f13046a56b7cf5d9ed739eb9c23b22929706c2fb173ad3a49633159998301df"
            },
            "downloads": -1,
            "filename": "eqt-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1c4377500d6d52a836f3bac0f4a4aa77",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 30761,
            "upload_time": "2024-04-25T13:43:48",
            "upload_time_iso_8601": "2024-04-25T13:43:48.861571Z",
            "url": "https://files.pythonhosted.org/packages/ee/a6/5b255d8f4007adc765cef0d5fbbc8383b0f61aec938ad7baf53bd4eb7505/eqt-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f803a416014a217dce5b4b0caacedcc7b2981b412b3fa2c2f54292e9d4890340",
                "md5": "8ce4d7cf3ebbb35d14db7a4b0b0c2114",
                "sha256": "0f043612996c58f959d6b30206c73c5c8f3ff611551397a5fcefe49a11a2fc41"
            },
            "downloads": -1,
            "filename": "eqt-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "8ce4d7cf3ebbb35d14db7a4b0b0c2114",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 50151,
            "upload_time": "2024-04-25T13:43:50",
            "upload_time_iso_8601": "2024-04-25T13:43:50.704623Z",
            "url": "https://files.pythonhosted.org/packages/f8/03/a416014a217dce5b4b0caacedcc7b2981b412b3fa2c2f54292e9d4890340/eqt-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-25 13:43:50",
    "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.24117s