How do I add a badge to a Qt list view?

Table of Contents

The badge resolution architecture — how BadgeCache, code_domain, badge_definition, and badge_mapping fit together — is in Badge system wiring.

Question

How do I render a new field as a DB-driven pill badge in a Qt list view?

Answer

Four steps; all are required.

1. Add a code domain to dq_badge_system_populate.sql:

PERFORM ores_dq_code_domains_upsert_fn(ores_utility_system_tenant_id_fn(),
    'my_domain', 'My Domain', 'Description of the domain.', <next_display_order>);

2. Add badge definitions — one per distinct visual variant:

PERFORM ores_dq_badge_definitions_upsert_fn(ores_utility_system_tenant_id_fn(),
    'my_def_code', 'Label', 'Tooltip description.',
    '#rrggbb', '#ffffff', 'severity_code', 'badge bg-severity', <next_display_order>);

Severity codes: secondary, info, success, warning, danger, primary.

3. Add badge mappings — one per entity code value:

PERFORM ores_dq_badge_mappings_upsert_fn(ores_utility_system_tenant_id_fn(),
    'my_domain', 'entity_code_value', 'my_def_code');

The entity code must match exactly what the Qt model returns for Qt::DisplayRole on that column (raw DB code or translated display string).

4. Wire the delegate in the entity's ItemDelegate:

  • paint(): add Column::MyField to the badge column guard; call badgeCache_->resolve("my_domain", text.toStdString()) and apply the returned colours; fall back to default gray on nullptr.
  • sizeHint(): add the same column guard; set minimum height 24 px and a minimum width appropriate for the longest label.

Script

No script — changes are to dq_badge_system_populate.sql and the entity's ItemDelegate. See Badge system wiring for the full paint() code template.

Tested by

Manual: re-run the population script against a dev database and open the entity list view to confirm badge colours appear.

See also

Emacs 29.1 (Org mode 9.6.6)