Skip to content

DAChip

DAChip is a closable tag/pill widget composed of a QFrame + label + close button. It supports qss styling and can be used independently.

Preview

DAChip: a read-only chip and a closable chip

Exported by --export-gallery: the first row is a non-closable read-only chip, the second is a chip with an × close button. Corner radius, background, and text color are all customizable through the DA--DAChip selector.

Key Features

  • ✅ Closable: Built-in close button that emits closeRequested on click
  • ✅ qss customizable: Selector DA--DAChip, sub-controls locatable by objectName
  • ✅ Independently usable: Can be used without DAChipsEdit
  • ✅ Click signal: Supports click interaction
  • ✅ Long-text elision (optional): setElideWidth() caps the width, … + tooltip shows the full text

Usage

1. Basic Usage

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

// Create a closable tag
auto *chip = new DA::DAChip("Tag text", parent);
chip->setClosable(true);

// Remove when the close button is clicked
QObject::connect(chip, &DA::DAChip::closeRequested, [chip]() {
    chip->deleteLater();
});

// Tag clicked
QObject::connect(chip, &DA::DAChip::clicked, []() {
    qDebug() << "Tag clicked";
});

2. qss Styling

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
DA--DAChip {
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    background: #f4f4f5;
    padding: 2px 8px;
}
DA--DAChip:hover {
    background: #e9e9eb;
}
/* Close button */
DA--DAChip #DAChipCloseButton {
    border: none;
    background: transparent;
    border-radius: 2px;
}
DA--DAChip #DAChipCloseButton:hover {
    background: #c0c4cc;
}
/* Tag text */
DA--DAChip #DAChipLabel {
    color: #606266;
    font-size: 12px;
}

3. Usage Inside DAChipsEdit

DAChipsEdit automatically creates and manages DAChip instances internally, so manual creation is usually unnecessary:

1
2
auto *edit = new DA::DAChipsEdit(parent);
edit->setTags({ "Tag1", "Tag2" });  // DAChip instances are created internally

4. Long-Text Elision (setElideWidth)

1
2
auto *chip = new DA::DAChip("a very long tag text", parent);
chip->setElideWidth(96);   // 0 (default) = no elision

The chip is capped at 96 px; the overflow ends with … and the full text is shown in a tooltip. text() and textChanged() always carry the full text. While elision is enabled the widget owns the tooltip (overriding any caller-set tooltip) and restores caller control when disabled. Keep the width at 64 px or more, otherwise the close button gets squeezed.

Sub-control Selectors

The close button uses objectName DAChipCloseButton and the label uses DAChipLabel. These can be targeted in qss for fine-grained styling.

Core Properties & Methods

Property / Method Type Description
text / setText() QString Tag text
closable / setClosable() bool Whether to show the close button
elideWidth / setElideWidth() int Maximum width in px; 0 = no elision

Signals

Signal Description
closeRequested() Close button was clicked
clicked() Tag was clicked
textChanged(QString) Text changed
closableChanged(bool) closable property changed

References