Wt: Wt::WButtonGroup Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Wt::WButtonGroup Class Reference

A class for grouping radio buttons logically together. More...

`#include <Wt/WButtonGroup.h>`

Inheritance diagram for Wt::WButtonGroup:
[legend]

## Public Member Functions
| WButtonGroup ()
| Creates a new empty button group.
void | addButton (WRadioButton *button, int id=-1)
| Adds a radio button to the group.
void | removeButton (WRadioButton *button)
| Removes a radio button from the group.
WRadioButton * | button (int id) const
| Returns the button for the given id.
int | id (WRadioButton *button) const
| Returns the id for the given button.
virtual const std::string | id () const override
| Returns the (unique) identifier for this object.
std::vector< WRadioButton * > | buttons () const
| Returns the buttons in this group.
int | count () const
| Returns the number of radiobuttons in this group.
int | checkedId () const
| Returns the id of the checked button.
void | setCheckedButton (WRadioButton *button)
| Sets the currently checked radiobutton.
WRadioButton * | checkedButton () const
| Returns the checked radiobutton.
void | setSelectedButtonIndex (int idx)
| Sets the currently checked radiobutton.
int | selectedButtonIndex () const
| Returns the index of the checked radiobutton.
Signal< WRadioButton * > & | checkedChanged ()
| Signal emitted when a button was checked.
Public Member Functions inherited from Wt::WObject
void | addChild (std::unique_ptr< WObject > child)
| Add a child WObject whose lifetime is determined by this WObject.
template<typename Child >
Child * | addChild (std::unique_ptr< Child > child)
| Add a child WObject, returning a raw pointer.
std::unique_ptr< WObject > | removeChild (WObject *child)
| Remove a child WObject, so its lifetime is no longer determined by this WObject.
template<typename Child >
std::unique_ptr< Child > | removeChild (Child *child)
| Remove a child WObject, so its lifetime is no longer determined by this WObject.
virtual void | setObjectName (const std::string &name)
| Sets an object name.
virtual std::string | objectName () const
| Returns the object name.
void | resetLearnedSlots ()
| Resets learned stateless slot implementations.
template<class T >
void | resetLearnedSlot (void(T::*method)())
| Resets a learned stateless slot implementation.
template<class T >
WStatelessSlot * | implementStateless (void(T::*method)())
| Declares a slot to be stateless and learn client-side behaviour on first invocation.
template<class T >
WStatelessSlot * | implementStateless (void(T::*method)(), void(T::*undoMethod)())
| Declares a slot to be stateless and learn client-side behaviour in advance.
void | isNotStateless ()
| Marks the current function as not stateless.
template<class T >
WStatelessSlot * | implementJavaScript (void(T::*method)(), const std::string &jsCode)
| Provides a JavaScript implementation for a method.
Public Member Functions inherited from Wt::Core::observable
| observable () noexcept
| Default constructor.
virtual | ~observable ()
| Destructor.
template<typename... Args, typename C >
auto | bindSafe (void(C::*method)(Args...)) noexcept
| Protects a method call against object destruction.
template<typename... Args, typename C >
auto | bindSafe (void(C::*method)(Args...) const) const noexcept
| Protects a const method call against object destruction.
template<typename Function >
auto | bindSafe (const Function &function) noexcept
| Protects a function against object destruction.

## Additional Inherited Members
Public Types inherited from Wt::WObject
typedef void(WObject::*) | Method()
| Typedef for a WObject method without arguments.
Static Public Member Functions inherited from Wt::WObject
Protected Member Functions inherited from Wt::WObject
virtual WStatelessSlot * | getStateless (Method method)
| On-demand stateless slot implementation.

## Detailed Description

A class for grouping radio buttons logically together.

A button group manages a set of radio buttons, making them exclusive of each other.

It is not a widget, but instead provides only a logical grouping. Radio buttons are aware of the group in which they have been added, see WRadioButton::group(). When a button is deleted, it is automatically removed its button group.

It allows you to associate id's with each button, which you may use to identify a particular button. The special value of -1 is reserved to indicate no button.

Usage example:

enum class Vote { Republican = 1, Democrat = 2, NoVote = 10 };
// use a group box as widget container for 3 radio buttons, with a title
auto container = std::make_unique<Wt::WGroupBox>("USA elections vote");
// use a button group to logically group the 3 options
auto group = std::make_shared<Wt::WButtonGroup>();
Wt::WRadioButton *button;
button = container->addWidget(
std::make_unique<Wt::WRadioButton>(
"I voted Republican"));
container->addWidget(std::make_unique<Wt::WBreak>());
group->addButton(button, Vote::Republican);
button = container->addWidget(
std::make_unique<Wt::WRadioButton>(
"I voted Democrat"));
container->addWidget(std::make_unique<Wt::WBreak>());
group->addButton(button, Vote::Democrat);
button = container->addWidget(
std::make_unique<Wt::WRadioButton>(
"I didn't vote"));
container->addWidget(std::make_unique<Wt::WBreak>());
group->addButton(button, Vote::NoVote);
group->setCheckedButton(group->button(Vote::NoVote));
Wt::WButtonGroup::button
WRadioButton * button(int id) const
Returns the button for the given id.
Wt::WRadioButton
A user control that represents a radio button.
Definition WRadioButton.h:95

See also
  WRadioButton

## Constructor & Destructor Documentation

## ◆ WButtonGroup()

Wt::WButtonGroup::WButtonGroup | ( |  | ) |

Creates a new empty button group.

Note
  The WRadioButtons associated with this WButtonGroup keep a shared_ptr to this WButtonGroup. Therefore, you should store a WButtonGroup in a shared_ptr (e.g. construct it with make_shared) before adding any radio buttons to it.

## Member Function Documentation

## ◆ addButton()

void Wt::WButtonGroup::addButton | ( | WRadioButton * | button,
|  | int | id = -1 )

Adds a radio button to the group.

You can assign an id to the button. If `id` is -1, then a unique id will be generated.

Note
  WButtonGroup should be owned by a shared_ptr before addButton is called on it!

See also
  removeButton(WRadioButton *)

## ◆ button()

WRadioButton * Wt::WButtonGroup::button | ( | int | id | ) | const

Returns the button for the given id.

Returns `nullptr` if no button exists for the given id.

See also
  id(), addButton()

## ◆ checkedButton()

WRadioButton * Wt::WButtonGroup::checkedButton | ( |  | ) | const

Returns the checked radiobutton.

If there is no radiobutton currently checked this function returns `nullptr`.

See also
  setCheckedButton(), selectedButtonIndex()

## ◆ checkedChanged()

Signal< WRadioButton * > & Wt::WButtonGroup::checkedChanged | ( |  | ) |

Signal emitted when a button was checked.

The argument passed is the new checkedButton().

## ◆ checkedId()

int Wt::WButtonGroup::checkedId | ( |  | ) | const

Returns the id of the checked button.

Returns the id of the currently checked button, or -1 if no button is currently checked.

## ◆ id() [1/2]

virtual const std::string Wt::WButtonGroup::id | ( |  | ) | const
| overridevirtual

Returns the (unique) identifier for this object.

For a WWidget, this corresponds to the id of the DOM element that represents the widget. This is not entirely unique, since a composite widget shares the same id as its implementation.

By default, the id is auto-generated, unless a custom id is set for a widget using WWidget::setId().

See also
  WWidget::jsRef()

Reimplemented from Wt::WObject.

## ◆ id() [2/2]

int Wt::WButtonGroup::id | ( | WRadioButton * | button | ) | const

Returns the id for the given button.

See also
  button(), addButton()

## ◆ removeButton()

void Wt::WButtonGroup::removeButton | ( | WRadioButton * | button | ) |

Removes a radio button from the group.

See also
  addButton(WRadioButton *, int)

## ◆ selectedButtonIndex()

int Wt::WButtonGroup::selectedButtonIndex | ( |  | ) | const

Returns the index of the checked radiobutton.

The index reflects the order in which the buttons have been added to the button group. Use checkedId() if you want to know the id of the button that is currently checked. If there is no radiobutton selected this function returns -1.

See also
  checkedId()

## ◆ setCheckedButton()

void Wt::WButtonGroup::setCheckedButton | ( | WRadioButton * | button | ) |

Sets the currently checked radiobutton.

The button `button` of this group is checked. A value of `0` will uncheck all radiobuttons.

Initially, no button is checked.

See also
  checkedId()

## ◆ setSelectedButtonIndex()

void Wt::WButtonGroup::setSelectedButtonIndex | ( | int | idx | ) |

Sets the currently checked radiobutton.

Sets the `idx'th` radiobutton checked. A value of -1 will uncheck all radiobuttons.

Initially, no button is checked.