AutoParam System
The AutoParam system provides a mechanism to automatically generate Qt-based configuration forms from C++ structs. It leverages compile-time reflection (via reflect-cpp) to create a runtime schema, which is then used to build the UI dynamically.
Source Location
AutoParamWidget lives in its own static library at src/WhiskerToolbox/AutoParamWidget/. It depends only on Qt6::Widgets and ParameterSchema — it has no dependency on TransformsV2 or any other domain library, making it reusable by any widget that needs schema-driven parameter forms (e.g., TransformsV2_Widget, TriageSession_Widget).
The CMake target is AutoParamWidget. Link it directly when you need the form widget:
target_link_libraries(MyWidget PUBLIC AutoParamWidget)Include Path
#include "AutoParamWidget/AutoParamWidget.hpp"Core Components
1. Parameter Structs
The foundation of the system is a simple C++ struct that defines your parameters.
struct MyTransformParams {
float threshold = 0.5f;
int iterations = 10;
std::string mode = "Fast";
bool enable_logging = true;
};2. ParameterSchema
At runtime, we need a description of this struct to build the UI. ParameterSchema is a data structure that holds this information:
- Fields: Name, type, default value
- Constraints: Min/max values for numbers
- UI Hints: Tooltips, display names, grouping
This schema is extracted automatically using reflect-cpp.
3. AutoParamWidget
The AutoParamWidget is a generic Qt widget that takes a ParameterSchema and builds a form:
float/double→QDoubleSpinBoxint→QSpinBoxbool→QCheckBoxstd::string→QLineEditorQComboBox(ifallowed_valuesordynamic_combois set)std::string+path_field_kind→QLineEdit+ Browse button (file or directory dialog)enum class→QComboBox(enumerator names auto-populated byextractParameterSchema)std::vector<T>→ multi-selectQListWidgetor CSVQLineEdit(see ParameterSchema)rfl::TaggedUnion→ discriminatorQComboBox+QStackedWidgetsub-form per alternativestd::optional<T>→ checkbox-gated widget
Fields with ParameterFieldDescriptor::is_hidden == true (set at runtime on the schema, e.g. by applyDynamicInputSlotSchemaConstraints) are not added to the form layout but still have backing widgets so toJson() / fromJson() match reflect-cpp structs.
Enum Class Fields
enum class fields (including std::optional<enum class>) require no ParameterUIHints specialization. extractParameterSchema sets type_name = "enum" and populates allowed_values with enumerator names; AutoParamWidget renders them as combo boxes and serializes to/from JSON strings (matching reflect-cpp).
enum class BoundaryMode { clamp, bounce, wrap };
struct MyParams {
BoundaryMode boundary_mode = BoundaryMode::clamp;
std::optional<BoundaryMode> alt_mode;
};Place enums in a named namespace — anonymous namespaces break reflect-cpp’s compile-time name extraction. See ParameterSchema § Enum Class for limitations.
Tagged Variant Fields
rfl::TaggedUnion fields are rendered as a discriminator combo box above a QStackedWidget. Each page is a sub-form built from that alternative’s recursively extracted ParameterSchema. toJson() / fromJson() read and write the nested JSON object (discriminator field plus active alternative’s fields).
struct LinearParams { float velocity_x = 1.0f; };
struct SinusoidalParams { float amplitude = 0.0f; };
using MotionVariant = rfl::TaggedUnion<"model", LinearParams, SinusoidalParams>;
struct GeneratorParams {
MotionVariant motion = LinearParams{}; // default initializer required
};rfl::TaggedUnion has no default constructor — the variant field must have a default member initializer. See ParameterSchema § Tagged Variant for schema details and JSON shape.
Customization
You can customize the generated UI without changing the AutoParamWidget code by using ParameterUIHints.
ParameterUIHints
Specialize the ParameterUIHints template for your struct to provide additional metadata.
template<>
struct ParameterUIHints<MyTransformParams> {
static void annotate(ParameterSchema& schema) {
// Add a tooltip
if (auto* f = schema.field("threshold")) {
f->tooltip = "Signal detection threshold (0.0 to 1.0)";
f->min_value = 0.0;
f->max_value = 1.0;
}
// Define allowed values for a string (creates a ComboBox)
if (auto* f = schema.field("mode")) {
f->allowed_values = {"Fast", "Accurate", "Debug"};
}
}
};Custom Widgets
If the standard widgets aren’t enough, you can register a completely custom widget definition for your parameter type using the ParamWidgetRegistry.
ParamWidgetRegistry::instance().registerCustomWidget<MyComplexParams>(
[](QWidget* parent) { return new MyCustomEditor(parent); }
);If a custom widget is registered, the AutoParamWidget is bypassed entirely for that type.
Runtime Field Updates
AutoParamWidget supports fields whose combo items or variant alternatives are not fully known at schema-extraction time but are populated or filtered at runtime (e.g., data keys from DataManager, or encoder types valid for the current model).
Dynamic Combo Fields
Set dynamic_combo = true on a ParameterFieldDescriptor to create a QComboBox even when allowed_values is empty. Optionally set include_none_sentinel = true to prepend a "(None)" entry that maps to an empty string in JSON.
template<>
struct ParameterUIHints<MySlotParams> {
static void annotate(ParameterSchema& schema) {
if (auto* f = schema.field("source")) {
f->dynamic_combo = true;
f->include_none_sentinel = true;
}
}
};updateAllowedValues()
Dynamically repopulates a string/enum combo at runtime:
auto keys = getKeysFromDataManager();
widget->updateAllowedValues("source", keys);- Preserves the current selection if it still exists in the new values.
- Falls back to the first item (or sentinel) if the previous selection is gone.
- Emits
parametersChangedonly if the selection actually changes. - Works on both top-level fields and variant sub-row fields.
updateVariantAlternatives()
Shows or hides variant alternatives by tag name:
widget->updateVariantAlternatives("encoder", {"ImageEncoderParams", "MaskEncoderParams"});- Filters the variant combo to show only the specified tags.
- If the current selection is hidden, switches to the first visible alternative.
- Preserves the combo → stacked-widget index mapping via
Qt::UserRoleitem data. - Emits
parametersChangedonly if the active alternative changes.
Path Fields (File / Directory Browse)
Set path_field_kind to FilePath or DirectoryPath on a std::string field to render a line edit with a Browse… button. Use file_dialog_id to key session path memory in AppFileDialog (same ids as hand-written widgets, e.g. transformv2_pipeline_open).
if (auto* f = schema.field("pipeline_path")) {
f->path_field_kind = PathFieldKind::FilePath;
f->file_dialog_id = "transformv2_pipeline_open";
}Fallback directories (when no session memory exists) are not stored in the schema. Register them once at app startup:
AutoParamWidget::setFileDialogOpener([](QWidget* parent, QString id, QString caption,
QString filter, QString fallback, bool pick_dir) {
if (pick_dir) {
return AppFileDialog::getExistingDirectory(parent, id, caption, fallback);
}
return AppFileDialog::getOpenFileName(parent, id, caption, filter, fallback);
});
AutoParamWidget::registerDialogFallback("transformv2_pipeline_open", [] {
return QString::fromStdString(defaultUserPipelineDirectory(configDir).string());
});Without app wiring, AutoParamWidget falls back to QFileDialog directly (sufficient for unit tests).
First consumer: RunTransformsV2PipelineAtTime.pipeline_path in TriageSession. TransformsV2Properties_Widget still uses hand-written Load/Library buttons; consolidation is optional in a later IO migration step.
See also
- Schema-Driven Forms — end-to-end walkthrough from reflect-cpp structs through TransformsV2 pipeline composition, with a PostEncoderWidget comparison
- ParameterSchema Library — schema extraction API
- Widget Development Guide § ParameterSchema