# Python bindings for custom PV widgets

**URL:** https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921
**Category:** Development
**Tags:** python
**Created:** [November 6, 2019, 4:17pm UTC](https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921 "2019-11-06T16:17:33Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![siver](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siver](https://discourse.paraview.org/u/siver)
#### Post date: [November 6, 2019, 4:17pm UTC](https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921/1 "2019-11-06T16:17:33Z")

</div>

Dear PV devs,

I hope you could assist with the following challenge:

We have conjured a small xml/C++ plugin with custom QT widgets and not all of them are represented in the XML file. Now we would like to manipulate the widget values via a python script, similar to how “trace” and “paraview.simple” would do it.

Could you give some directions on how such PV-friendly python bindings could be implemented?

My sincere gratitude,  
Art

---

<div class="post-metadata">

### Author: ![siver](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siver](https://discourse.paraview.org/u/siver)
#### Post date: [November 14, 2019, 12:08pm UTC](https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921/2 "2019-11-14T12:08:20Z")

</div>

We have figured it out! The answer was right in front of us, i.e. Q\_PROPERTY.

Many thanks,  
Art

---

<div class="post-metadata">

### Author: ![siver](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siver](https://discourse.paraview.org/u/siver)
#### Post date: [January 9, 2020, 4:05pm UTC](https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921/3 "2020-01-09T16:05:03Z")

</div>

Dear all,

Our success with Q\_PROPERTY was somewhat limited, hence I ask for assistance once again.

To recap, we have a C++ plugin with internal variables A, B, where A is a float and B is a string. We would like to set these variables within ParaView’s python console as

```
plugin.A = 2.5
plugin.B = "string"

```

Currently, the connection to python is done via this construct

```
Q_PROPERTY(QList<QVariant> Values READ GetValues WRITE SetValues)

```

which enables the above python calls. However, the Q\_PROPERTY approach turned out to have a notable flaw, namely **QList\<QVariance\>** in C++ translates into python as **list of floats** _only_, although QVariance can contain numbers, strings and more.

Is there any other approach to connect C++ plugin to a python console in ParaView? Is the Q\_PROPERTY approach not meant to be used this way in the first place?

Any expertise on the matter is appreciated. Many thanks,  
Art

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [January 9, 2020, 4:27pm UTC](https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921/4 "2020-01-09T16:27:21Z")

</div>

Q\_PROPERTY is definitely meant to be used in a custom property widget.

See `Plugins/LagrangianParticleTracker/pqIntegrationModelSeedHelperWidget.h` for an example.

---

<div class="post-metadata">

### Author: ![siver](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siver](https://discourse.paraview.org/u/siver)
#### Post date: [January 10, 2020, 9:14am UTC](https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921/5 "2020-01-10T09:14:28Z")

</div>

Hello Mathieu,

Thank you for a prompt reply.

Our implementation is very similiar to your example. Specifically, here is our C++ part:

```
(plugin.h) 
Q_PROPERTY(QList<QVariant> Values READ GetValues WRITE SetValues)

```

and

```
(plugin.cpp)
void plugin::SetValues(const QList<QVariant>& values)
{
    if(values.count() == 0)
        return;

    std::cout << "values[0] = " << values[0].toString().toStdString() << std::endl;
    std::cout << "values[1] = " << values[1].toString().toStdString() << std::endl;
}

```

When updated from a python console as `plugin.values = [1, 3.5]`, the (correct) output is

```
values[0] = 1
values[1] = 3.5

```

But this update `plugin.values = [1, 'a']` (with a char/string) raises an error:

```
../paraview/servermanager.py", line 820, in SetData
    self.SMProperty.SetElement(idx, val)
TypeError: SetElement argument 2:

```

which, if I understand correctly is an index error. Yet, the number of elements in the array is the same. I can’t figure out what I’m doing wrong here.

Thanks a lot,  
Art

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [January 10, 2020, 9:15am UTC](https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921/6 "2020-01-10T09:15:20Z")

</div>

Are you using a StringVectorProperty in your XML file ?

---

<div class="post-metadata">

### Author: ![siver](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siver](https://discourse.paraview.org/u/siver)
#### Post date: [January 10, 2020, 9:23am UTC](https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921/7 "2020-01-10T09:23:59Z")

</div>

That is it!

It was a \<DoubleVectorProperty\>, changing to \<StringVectorProperty\> removed the error.

Two more questions:  
(1) Is there a way to have a mixture of doubles and strings?  
(2) Can I have nested structures like lists inside lists?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [January 10, 2020, 9:27am UTC](https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921/8 "2020-01-10T09:27:04Z")

</div>

1. StringVectorProperty allows you to specify the element types, int double or string. Search for `element_types`

2. No, but you can have _sort of_ two dimensional vectors by using `number_elements_per_command`

---

<div class="post-metadata">

### Author: ![siver](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@siver](https://discourse.paraview.org/u/siver)
#### Post date: [January 10, 2020, 9:31am UTC](https://discourse.paraview.org/t/python-bindings-for-custom-pv-widgets/2921/9 "2020-01-10T09:31:07Z")

</div>

I will look into `element_types`, thanks a lot.

I also saw in your example that you emulate nested structures by splitting strings via a separator `;`, we might try that as well.

Thanks a lot for all the help.

Best,  
Art
