Wt: Wt::WComboBox Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions
Wt::WComboBox Class Reference
Model/view system

A widget that provides a drop-down combo-box control. More...

`#include <Wt/WComboBox>`

Inheritance diagram for Wt::WComboBox:
[legend]

List of all members.

## Public Member Functions
| WComboBox (WContainerWidget *parent=0)
| Creates an empty combo-box with optional parent.
void | addItem (const WString &text)
| Adds an option item.
int | count () const
| Returns the number of items.
int | currentIndex () const
| Returns the currently selected item.
void | insertItem (int index, const WString &text)
| Inserts an item at the specified position.
void | removeItem (int index)
| Removes the item at the specified position.
void | setCurrentIndex (int index)
| Changes the current selection.
void | setItemText (int index, const WString &text)
| Changes the text for a specified option.
const WString | currentText () const
| Returns the text of the currently selected item.
const WString | itemText (int index) const
| Returns the text of a particular item.
void | setModel (WAbstractItemModel *model)
| Sets the model to be used for the items.
void | setModelColumn (int index)
| Sets the column in the model to be used for the items.
WAbstractItemModel * | model () const
| Returns the data model.
int | findText (const WString &text, WFlags< MatchFlag > flags=MatchExactly|MatchCaseSensitive)
| Returns the index of the first item that matches a text.
virtual WString | valueText () const
| Returns the current value.
virtual void | setValueText (const WString &value)
| Sets the current value.
virtual void | refresh ()
| Refresh the widget.
void | clear ()
| Clears all items.
Signal< int > & | activated ()
| Signal emitted when the selection changed.
Signal< WString > & | sactivated ()
| Signal emitted when the selection changed.

## Detailed Description

A widget that provides a drop-down combo-box control.

A combo box provides the user with a set of options, from which one option may be selected.

WComboBox is an MVC view class, using a simple string list model by default. The model may be populated using addItem(const WString&) or insertItem(int, const WString&) and the contents can be cleared through clear(). These methods manipulate the underlying model().

To use the combo box with a custom model instead of the default WStringListModel, use setModel().

To react to selection events, connect to the changed(), activated() or sactivated() signals.

At all times, the current selection index is available through currentIndex() and the current selection text using currentText().

WComboBox does not have support for auto-completion, this behaviour can be found in the WSuggestionPopup.

Usage example:

     Wt::WComboBox *combo = new Wt::WComboBox(this);
     combo->addItem("Clint Eastwood");
     combo->addItem("Mick Jagger");
     combo->addItem("Johnny Depp");
     combo->addItem("Kate Winslet");

     combo->setCurrentIndex(2); // Johnny Depp
     combo->activated().connect(this, &MyWidget::comboChanged);

WComboBox is an inline widget.

### CSS

The widget corresponds to the HTML `<select>` tag and does not provide styling. It can be styled using inline or external CSS as appropriate.

## Member Function Documentation

Signal<int>& Wt::WComboBox::activated | ( |  | ) |

Signal emitted when the selection changed.

The newly selected item is passed as an argument.

See also:
  sactivated(), currentIndex()

void Wt::WComboBox::addItem | ( | const WString & | text | ) |

Adds an option item.

Equivalent to insertItem (count(), `text`).

void Wt::WComboBox::clear | ( |  | ) |

Clears all items.

Removes all items from the underlying model. This requires that the model() is editable.

int Wt::WComboBox::currentIndex | ( |  | ) | const

Returns the currently selected item.

If no item is currently selected, the method returns -1.

The default value is 0, unless the combo box is empty.

const WString Wt::WComboBox::currentText | ( |  | ) | const

Returns the text of the currently selected item.

See also:
  currentIndex(), itemText(int) const

void Wt::WComboBox::insertItem | ( | int | index,
|  | const WString & | text
| ) |  |

Inserts an item at the specified position.

The item is inserted in the underlying model at position `index`. This requires that the model() is editable.

See also:
  addItem(const WString&), removeItem(int)

const WString Wt::WComboBox::itemText | ( | int | index | ) | const

Returns the text of a particular item.

See also:
  setItemText(int, const WString&), currentText()

WAbstractItemModel* Wt::WComboBox::model | ( |  | ) | const

Returns the data model.

See also:
  setModel()

void Wt::WComboBox::refresh | ( |  | ) | `[virtual]`

Refresh the widget.

The refresh method is invoked when the locale is changed using WApplication::setLocale() or when the user hit the refresh button.

The widget must actualize its contents in response.

Reimplemented from Wt::WFormWidget.

void Wt::WComboBox::removeItem | ( | int | index | ) |

Removes the item at the specified position.

The item is removed from the underlying model. This requires that the model() is editable.

See also:
  insertItem(int index, const WString&), clear()

Signal<WString>& Wt::WComboBox::sactivated | ( |  | ) |

Signal emitted when the selection changed.

The newly selected text is passed as an argument.

See also:
  activated(), currentText()

void Wt::WComboBox::setCurrentIndex | ( | int | index | ) |

Changes the current selection.

Specify a value of -1 for `index` to clear the selection.

void Wt::WComboBox::setItemText | ( | int | index,
|  | const WString & | text
| ) |  |

Changes the text for a specified option.

The text for the item at position `index` is changed. This requires that the model() is editable.

void Wt::WComboBox::setModel | ( | WAbstractItemModel * | model | ) |

Sets the model to be used for the items.

The `model` may not be 0, and ownership of the model is not transferred.

The default value is a WStringListModel that is owned by the combo box.

See also:
  setModelColumn(int)

void Wt::WComboBox::setModelColumn | ( | int | index | ) |

Sets the column in the model to be used for the items.

The column `index` in the model will be used to retrieve data.

The default value is 0.

See also:
  setModel()

void Wt::WComboBox::setValueText | ( | const WString & | value | ) | `[virtual]`

Sets the current value.

Sets the current index to the item corresponding to `value`.

Implements Wt::WFormWidget.

WString Wt::WComboBox::valueText | ( |  | ) | const`[virtual]`

Returns the current value.

Returns currentText() converted from a WString to a string.

Implements Wt::WFormWidget.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator