---
title: 'Adding a custom widget to a QGIS Processing script'
url: 'https://www.faunalia.eu/en/blog/2019-07-02-custom-processing-widget'
markdown: 'https://www.faunalia.eu/en/blog/2019-07-02-custom-processing-widget.md'
lang: en
date: '2019-07-03'
description: 'Custom widget Processing QGIS'
taxonomy:
  category:
    - blog
  tag:
    - qgis
    - processing
    - python
  archives_month:
    - jul_2019
  archives_year:
    - '2019'
---

[ Faunalia ](https://www.faunalia.eu/en)     [ Blog ](https://www.faunalia.eu/en/blog)      Adding a custom widget to a QGIS Processing script    

## [Adding a custom widget to a QGIS Processing script](https://www.faunalia.eu/en/blog/2019-07-02-custom-processing-widget)

    3rd Jul 2019

  [qgis](https://www.faunalia.eu/en/blog/tag:qgis) [processing](https://www.faunalia.eu/en/blog/tag:processing) [python](https://www.faunalia.eu/en/blog/tag:python)  

# Adding Custom Widgets to a Processing script

You can write python script in Processing to write your own algorithm.

If you already know how to write Processing scripts, then you also know that you can add many parameter types

- numbers
- vectors
- rasters
- [here a complete list](https://docs.qgis.org/testing/en/docs/user_manual/processing/console.html#creating-scripts-and-running-them-from-the-toolbox)

But if you want to add another parameter type? How to add for example a `DateTime`like parameter with a pop-up calendar button?

In the following example I just took the template script and added the parameter: it is up to you to take that `DateTime` value and make whatever you want!

Let's proceed by steps:

0. Create a new script with the dedicated button:

    ![Processing script template](https://www.faunalia.eu/images/9/9/a/7/0/99a70f77e86944cd9b73b470797148973fd1d31b-img.png)
1. Some imports before to start creating the widget:

    ```python
    from processing.gui.wrappers import WidgetWrapper
    from qgis.PyQt.QtWidgets import QDateTimeEdit
    from qgis.PyQt.QtCore import QCoreApplication, QDate
    from qgis.core import (QgsProcessing,
                       QgsFeatureSink,
                       QgsProcessingException,
                       QgsProcessingAlgorithm,
                       QgsProcessingParameterString,
                       QgsProcessingParameterFeatureSource,
                       QgsProcessingParameterFeatureSink)
    ```
2. We have to create a class that must **inherit** from the `WidgetWrapper`class. Within this class we will specify all the widget settings, like the pop-up button, the default date, etc:

    ```python
    class DateTimeWidget(WidgetWrapper):
        """
        QDateTimeEdit widget with calendar pop up
        """

        def createWidget(self):
            self._combo = QDateTimeEdit()
            self._combo.setCalendarPopup(True)

            today = QDate.currentDate()
            self._combo.setDate(today)

            return self._combo

        def value(self):
            date_chosen = self._combo.dateTime()
            return date_chosen.toString(Qt.ISODate)
    ```

    the class has two methods: the first one, `createWidget`, creates the widget, the second one, `velue`, returns the value of the widget, in this case the datetime value
3. Within the method `initAlgorithm` of the main algorithm class, we must tell Processing how to build the UI with the widget:

    ```python
      class myAlgorithm(QgsProcessingAlgorithm):

          ....

          DATEINI = 'DATEINI'

          ....

        def initAlgorithm(self, config=None):

            param = QgsProcessingParameterString(self.DATEINI, 'Initial Date')
            param.setMetadata({
                'widget_wrapper': {
                    'class': DateTimeWidget}})

            self.addParameter(param)
    ```
4. Last step, within the `processAlgorithm` method we should retrieve the widget value and create a variable with the results to make whatever we want to do:

    ```python
      mydate = self.parameterAsString(
          parameters,
          self.INIDATE,
          context
      )

    ```

And here we go:

![Processing script UI](https://www.faunalia.eu/user/pages/08.blog/2019-07-02-custom-processing-widget/ui.png)

Happy Processing coding!

 [  Previous Post ](https://www.faunalia.eu/en/blog/2019-06-03_dataplotly_target_reached) [Next Post  ](https://www.faunalia.eu/en/blog/2019-09-03-qhurricane)

#### Related Posts

- 100 

     [QGIS Print Layouts Graphs and Charts — Beta Out Now!](https://www.faunalia.eu/en/blog/2019-09-26-dataplotly-beta "QGIS Print Layouts Graphs and Charts — Beta Out Now!")
- 100 

     [QGIS Print Layouts Graphs and Charts Campaign](https://www.faunalia.eu/en/blog/2019-03-13_dataplotly "QGIS Print Layouts Graphs and Charts Campaign")
- 90 

     [A Bug's Death](https://www.faunalia.eu/en/blog/2019-05-23_a_bug_story "A Bug's Death")
- 80 

     [DataPlotly target reached!](https://www.faunalia.eu/en/blog/2019-06-03_dataplotly_target_reached "DataPlotly target reached!")
- 75 

     [QQuake - An Earthquake Plugin for QGIS](https://www.faunalia.eu/en/blog/2021-03-01_qquake "QQuake - An Earthquake Plugin for QGIS")

#### Popular Tags

  [qgis](https://www.faunalia.eu/en/blog/tag:qgis) [dataplotly](https://www.faunalia.eu/en/blog/tag:dataplotly) [crowdfunding](https://www.faunalia.eu/en/blog/tag:crowdfunding) [python](https://www.faunalia.eu/en/blog/tag:python) [lrs](https://www.faunalia.eu/en/blog/tag:lrs) [ecw](https://www.faunalia.eu/en/blog/tag:ecw) [bug](https://www.faunalia.eu/en/blog/tag:bug) [processing](https://www.faunalia.eu/en/blog/tag:processing) [grib](https://www.faunalia.eu/en/blog/tag:grib) [plotly](https://www.faunalia.eu/en/blog/tag:plotly) [flask](https://www.faunalia.eu/en/blog/tag:flask) [dash](https://www.faunalia.eu/en/blog/tag:dash) [remote](https://www.faunalia.eu/en/blog/tag:remote) [open source](https://www.faunalia.eu/en/blog/tag:open%20source) [online](https://www.faunalia.eu/en/blog/tag:online) [plugin](https://www.faunalia.eu/en/blog/tag:plugin)  

#### Archives

- [ 1 August 2022  ](https://www.faunalia.eu/en/blog/archives_month:aug_2022)
- [ 1 February 2021  ](https://www.faunalia.eu/en/blog/archives_month:feb_2021)
- [ 1 December 2020  ](https://www.faunalia.eu/en/blog/archives_month:dec_2020)
- [ 1 April 2020  ](https://www.faunalia.eu/en/blog/archives_month:apr_2020)
- [ 1 March 2020  ](https://www.faunalia.eu/en/blog/archives_month:mar_2020)
- [ 1 January 2020  ](https://www.faunalia.eu/en/blog/archives_month:jan_2020)
- [ 1 October 2019  ](https://www.faunalia.eu/en/blog/archives_month:oct_2019)
- [ 2 September 2019  ](https://www.faunalia.eu/en/blog/archives_month:sep_2019)
- [ 1 July 2019  ](https://www.faunalia.eu/en/blog/archives_month:jul_2019)
- [ 1 June 2019  ](https://www.faunalia.eu/en/blog/archives_month:jun_2019)
- [ 3 May 2019  ](https://www.faunalia.eu/en/blog/archives_month:may_2019)
- [ 1 April 2019  ](https://www.faunalia.eu/en/blog/archives_month:apr_2019)

---

## Navigation

- Parent: [Blog](https://www.faunalia.eu/en/blog.md)
- Previous: [Mesh data and QGIS](https://www.faunalia.eu/en/blog/2019-09-03-qhurricane.md)
- Next: [DataPlotly target reached!](https://www.faunalia.eu/en/blog/2019-06-03_dataplotly_target_reached.md)
