Skip to content

DASelectComboBox

DASelectComboBox is a multi-tag dropdown selector that mirrors Element UI's el-select (multiple + filterable + allow-create). It inherits QComboBox, retaining the native arrow, border, and keyboard mnemonics, while replacing the edit field with DAChipsEdit.

Preview

DASelectComboBox: edit field with the Qt and C++ tags

DASelectComboBox: open candidate list with selected tags removed and the first item highlighted

DASelectComboBox: grouped options, empty state, clear button and collapsed tags

The first image shows the edit field: Qt and C++ rendered as chips, a placeholder input, and the native dropdown arrow. The second shows the open candidate list: already-selected tags are removed, the first item is highlighted (default-first-option), and typing filters the list live.

Key Features

  • ✅ Multi-tag selection: Selected items are displayed as chips in the edit field
  • ✅ Filterable: Type text to filter the candidate list (enabled by default)
  • ✅ Allow-create: Can create new tags outside the candidate list
  • ✅ Default first option: Automatically highlights the first matching item during filtering
  • ✅ Two height modes: Grow with tags / fixed height with scrolling
  • ✅ QComboBox compatible: Retains native arrow, border, and keyboard interaction
  • ✅ Grouped options (optional): Non-selectable group headers that disappear when the group has no match
  • ✅ Empty state and create (optional): Keeps the popup open with a clickable "Create" entry
  • ✅ Clear button (optional): × appears on hover and clears everything at once
  • ✅ Tag collapsing (optional): Shows +N beyond the threshold, expands on focus

Class Relationships

classDiagram
    class QComboBox {
        +showPopup()
    }
    class DASelectComboBox {
        +setOptions(QStringList)
        +setTags(QStringList)
        +setAllowCreate(bool)
        +setFilterable(bool)
        +setHeightMode(HeightMode)
    }
    class DAChipsEdit {
        +setTags(QStringList)
        +inputTextChanged()
    }

    QComboBox <|-- DASelectComboBox
    DASelectComboBox --> DAChipsEdit : embeds edit field

Usage

The corresponding example is located at: examples/all_widgets_demo (Selector/Tags tab).

1. Basic Usage

Create a multi-select dropdown, set the candidate list, and allow creating new tags:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include "DASelectComboBox.h"

auto *combo = new DA::DASelectComboBox(parent);

// Set candidate options
combo->setOptions({ "Apple", "Banana", "Orange", "Grape", "Watermelon" });

// Allow creating tags outside the list
combo->setAllowCreate(true);

// Set placeholder text
combo->setPlaceholder("Select or type a fruit...");

// Pre-select tags
combo->setTags({ "Apple", "Banana" });

2. Height Modes

DASelectComboBox provides two height modes to control behavior as tags increase:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
using namespace DA;

// Mode 1: Grow with tags (default)
// The control height increases as tags are added
combo->setHeightMode(DASelectComboBox::HeightMode::GrowWithTags);

// Mode 2: Fixed height with scrolling
// The control height stays fixed; the tag area scrolls
combo->setHeightMode(DASelectComboBox::HeightMode::FixedHeightScrollable);
combo->setFixedRowCount(3);  // Show exactly 3 rows
Mode Description Use Case
GrowWithTags Control grows taller as tags increase Few tags, layout allows growth
FixedHeightScrollable Fixed height, tag area scrolls Many tags possible, limited space

3. Signal Handling

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Tag list changed
QObject::connect(combo, &DA::DASelectComboBox::tagsChanged,
    [](const QStringList& tags) {
        qDebug() << "Current tags:" << tags;
    });

// User selected a candidate option
QObject::connect(combo, &DA::DASelectComboBox::optionSelected,
    [](const QString& option) {
        qDebug() << "Selected:" << option;
    });

// User created a new tag
QObject::connect(combo, &DA::DASelectComboBox::created,
    [](const QString& text) {
        qDebug() << "Created new tag:" << text;
    });

4. qss Styling

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
DA--DASelectComboBox {
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    padding: 2px 8px;
}
DA--DASelectComboBox::drop-down {
    width: 20px;
    border-left: 1px solid #dcdfe6;
}
/* Popup list item style */
QListView#DASelectPopup {
    border: 1px solid #e4e7ed;
}

qss Pitfall

Styling QComboBox::drop-down via qss without providing a ::down-arrow image will suppress the native arrow entirely. DAWidgets solves this by embedding an SVG arrow (:/DAWidgetsIcons/arrow-down.svg).

5. Grouped options and the empty state

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
using namespace DA;

combo->setGroupedOptions({
    DASelectGroup{ "Region", { "North", "East", "South" } },
    DASelectGroup{ "Channel", { "Direct", "Dealer", "Online" } },
});

// Keep the popup open with an empty state when nothing matches (default off)
combo->setEmptyStateEnabled(true);
combo->setNoMatchText("No matching items");

// Replace the built-in panel with a custom widget (ownership moves to this control)
combo->setEmptyWidget(customWidget);
  • options() still returns the flattened candidate text; setOptions() clears the groups, so the two APIs are mutually exclusive.
  • Group headers are not selectable and ↑/↓ skips them; a group whose candidates all filter out disappears together with its header.
  • The empty state only applies after setEmptyStateEnabled(true): when off, a no-match still closes the popup exactly as before.
  • The panel is an overlay on the popup viewport, never a placeholder item. With allowCreate and non-empty input, a "Create \"xxx\"" button appears; clicking it is equivalent to pressing Enter and emits created once.

6. Clear button and collapsing selected tags

1
2
3
4
5
6
7
combo->setClearable(true);        // × on hover clears everything
combo->setCollapseTags(true);     // collapse beyond the threshold
combo->setCollapseTagCount(2);    // default 2, minimum 1

QObject::connect(combo, &DA::DASelectComboBox::clearClicked, [] {
    qDebug() << "cleared";
});
  • With clearable off no child widget is created; when on, × only appears on hover while tags exist. A click emits tagsChanged once, then clearClicked once, and keeps the popup open.
  • Collapsing only hides the tags beyond the threshold and shows +N; tags() always returns every tag, and focusing the input or opening the popup restores them all.
  • Both features are off by default, so the visual result without them is unchanged.

Core Properties & Methods

Q_PROPERTY

Property Type Description
options QStringList Candidate option list
tags QStringList Selected tag list
allowCreate bool Whether creating tags outside the list is allowed
filterable bool Whether input filtering is enabled
placeholder QString Placeholder text
heightMode HeightMode Height mode
fixedRowCount int Number of visible rows in fixed-height mode

Core Methods

Method Description
setOptions(QStringList) Set the candidate option list
tags() / setTags(QStringList) Get/set selected tags
setAllowCreate(bool) Enable/disable tag creation
setFilterable(bool) Enable/disable filtering
setHeightMode(HeightMode) Switch height mode
clear() Clear all tags
setGroupedOptions(QVector<DASelectGroup>) / groupedOptions() Set/get grouped candidates
setEmptyStateEnabled(bool) / isEmptyStateEnabled() Keep the popup open with an empty state
setNoMatchText(QString) / noMatchText() Empty-state message
setEmptyWidget(QWidget*) / emptyWidget() Custom empty-state widget (takes ownership)
setClearable(bool) / isClearable() Show the clear button on hover
setCollapseTags(bool) / collapseTags() Collapse selected tags
setCollapseTagCount(int) / collapseTagCount() Number of tags kept when collapsed
clearClicked() Emitted once after clearing

References