Workspace 6.21.5
Configuring widget properties for Workspace editor

This tutorial provides a working examples that will show you how to set properties for your widgets inside the Workspace editor.




Tutorial contents




Setting default widget properties

For this tutorial, let's pretend we are developing an operation that will read in some CSV data from disk. This operation will have a QString input to for the file path like so.

SimpleInput<QString> fileName_;

When this operation is clicked on in the Workspace editor, this input's edit field in the Operation editor will most likely default to a simple line edit.

Default line edit widget

To make life easier for our end-users, we can write the following code in our Operation's constructor.

fileName_.input_.setPreferredWidget("CSIRO::Widgets::FileNameWidget");

The above code will ensure that a file name widget will be visible for this input field in Operation editor. This widget will allow users to bring up a file browser dialog to select a file on disk.

Default widget changed to FileNameWidget

We can further improve this by setting a file filter to only show files with the .csv extension. We can do this by setting the widget's nameFilters property.

fileName_.input_.setWidgetPropertyValue("CSIRO::Widgets::FileNameWidget", "nameFilters", QStringList({ "*.csv" }));
QStringList
Definition: vectornumbertostringlistadaptor.cpp:133
File dialog now lists only .csv files

Any of your own widgets can be modified to expose additional properties which you can then set defaults for in the same way. Please consult the Qt documentiation about widget properties to learn how.

Remembering widget property values

Whenever you click on an Operation in the Workspace editor, new widgets are created to display the Operation's data objects within the Operation editor. For this tutorial, let's have a look at the QStringList data object and the StringListWidget

Displaying a QStringList data object in StringListWidget

Notice that this widget has controls to specify the index range which you can change which you can change. Try doing this now, then deselect and reselect the operation. Did you notice that the range you set is remembered? These two range values are exposed as properties on the StringListWidget which have been programmed to remember their values on the particular Input or Output that it is attached to. Here is the source code of the StringListWidgetConnector's constructor.

StringListWidgetConnector::StringListWidgetConnector(QWidget& widget, const NamePath& namePath) :
QWidgetConnector(widget, namePath)
{
StringListWidget* stringListWidget = qobject_cast<StringListWidget*>(&widget);
assert(stringListWidget);
bool ok = false;
ok = connect(stringListWidget, &StringListWidget::requestUpdateData,
this, &StringListWidgetConnector::requestUpdateData);
ok = connect(stringListWidget, &StringListWidget::requestUpdateWidget,
this, &StringListWidgetConnector::requestUpdateWidget);
ok = connect(stringListWidget, &StringListWidget::minimumChanged,
&createWidgetPropertyMonitor("minimum"), &WidgetPropertyMonitor::handlePropertyChange);
ok = connect(stringListWidget, &StringListWidget::maximumChanged,
&createWidgetPropertyMonitor("maximum"), &WidgetPropertyMonitor::handlePropertyChange);
Q_UNUSED(ok);
}
#define WS_ASSERT_RUNTIME(cond)
Definition: errorchecks.h:146

The two sections of this function that we're insterested in are below

ok = connect(stringListWidget, &StringListWidget::minimumChanged,
&createWidgetPropertyMonitor("minimum"), &WidgetPropertyMonitor::handlePropertyChange);
ok = connect(stringListWidget, &StringListWidget::maximumChanged,
&createWidgetPropertyMonitor("maximum"), &WidgetPropertyMonitor::handlePropertyChange);

In the connect function call, we specify four parameters

  1. A pointer to the StringListWidget object
  2. A function pointer to the signal that StringListWidget emits when its property changes
  3. A pointer to a WidgetPropertyMonitor object. We use the createWidgetPropertyMonitor function to instantiate one of these objects which requires us to specify the name of the widget property that we wish to monitor
  4. A function pointer to the WidgetPropertyMonitor's slot that will handle the property change event

That's all you need to do to remember a widget property's value! Note that these values will only be remembered for as long workflow is loaded in system memory. The widget property values are currently not saved to disc with the workflow. Please consult the Qt documentiation about widget properties to learn how to add new properties to your widget.