Q3ButtonGroup Class Reference

#include <q3buttongroup.h>

Inheritance diagram for Q3ButtonGroup:

Inheritance graph
[legend]
Collaboration diagram for Q3ButtonGroup:

Collaboration graph
[legend]
List of all members.

Detailed Description

The Q3ButtonGroup widget organizes QAbstractButton widgets in a group.

A button group widget makes it easier to deal with groups of buttons. Each button in a button group has a unique identifier. The button group emits a clicked() signal with this identifier when a button in the group is clicked. This makes a button group particularly useful when you have several similar buttons and want to connect all their clicked() signals to a single slot.

An exclusive button group switches off all toggle buttons except the one that was clicked. A button group is, by default, non-exclusive. Note that all radio buttons that are inserted into a button group are mutually exclusive even if the button group is non-exclusive. (See setRadioButtonExclusive().)

There are two ways of using a button group: The button group is the parent widget of a number of buttons, i.e. the button group is the parent argument in the button constructor. The buttons are assigned identifiers 0, 1, 2, etc., in the order they are created. A Q3ButtonGroup can display a frame and a title because it inherits Q3GroupBox. The button group is an invisible widget and the contained buttons have some other parent widget. In this usage, each button must be manually inserted, using insert(), into the button group and given an identifier.

A button can be removed from the group with remove(). A pointer to a button with a given id can be obtained using find(). The id of a button is available using id(). A button can be set on with setButton(). The number of buttons in the group is returned by count().

See also:
QPushButton, QCheckBox, QRadioButton

Definition at line 37 of file q3buttongroup.h.

Signals

void pressed (int id)
void released (int id)
void clicked (int id)

Public Member Functions

 Q3ButtonGroup (QWidget *parent=0, const char *name=0)
 Q3ButtonGroup (const QString &title, QWidget *parent=0, const char *name=0)
 Q3ButtonGroup (int columns, Qt::Orientation o, QWidget *parent=0, const char *name=0)
 Q3ButtonGroup (int columns, Qt::Orientation o, const QString &title, QWidget *parent=0, const char *name=0)
 ~Q3ButtonGroup ()
bool isExclusive () const
bool isRadioButtonExclusive () const
void setExclusive (bool)
void setRadioButtonExclusive (bool)
int insert (QAbstractButton *, int id=-1)
void remove (QAbstractButton *)
QAbstractButtonfind (int id) const
int id (QAbstractButton *) const
int count () const
void setButton (int id)
QAbstractButtonselected () const
int selectedId () const

Protected Slots

void buttonPressed ()
void buttonReleased ()
void buttonClicked ()

Protected Member Functions

bool event (QEvent *e)

Private Member Functions

void init ()
void fixChildren () const
int insert_helper (QAbstractButton *, int id=-1)

Private Attributes

bool excl_grp
bool radio_excl
QMap< int, QAbstractButton * > buttonIds
QButtonGroup group


Constructor & Destructor Documentation

Q3ButtonGroup::Q3ButtonGroup ( QWidget parent = 0,
const char *  name = 0 
)

Constructs a button group with no title.

The parent and name arguments are passed to the QWidget constructor.

Definition at line 100 of file q3buttongroup.cpp.

References init().

00101     : Q3GroupBox(parent, name)
00102 {
00103     init();
00104 }

Here is the call graph for this function:

Q3ButtonGroup::Q3ButtonGroup ( const QString title,
QWidget parent = 0,
const char *  name = 0 
)

Constructs a button group with the title title.

The parent and name arguments are passed to the QWidget constructor.

Definition at line 113 of file q3buttongroup.cpp.

References init().

00115     : Q3GroupBox(title, parent, name)
00116 {
00117     init();
00118 }

Here is the call graph for this function:

Q3ButtonGroup::Q3ButtonGroup ( int  strips,
Qt::Orientation  orientation,
QWidget parent = 0,
const char *  name = 0 
)

Constructs a button group with no title. Child widgets will be arranged in strips rows or columns (depending on orientation).

The parent and name arguments are passed to the QWidget constructor.

Definition at line 129 of file q3buttongroup.cpp.

References init().

00131     : Q3GroupBox(strips, orientation, parent, name)
00132 {
00133     init();
00134 }

Here is the call graph for this function:

Q3ButtonGroup::Q3ButtonGroup ( int  strips,
Qt::Orientation  orientation,
const QString title,
QWidget parent = 0,
const char *  name = 0 
)

Constructs a button group with title title. Child widgets will be arranged in strips rows or columns (depending on orientation).

The parent and name arguments are passed to the QWidget constructor.

Definition at line 145 of file q3buttongroup.cpp.

References init().

00148     : Q3GroupBox(strips, orientation, title, parent, name)
00149 {
00150     init();
00151 }

Here is the call graph for this function:

Q3ButtonGroup::~Q3ButtonGroup (  ) 

Destructor.

Definition at line 165 of file q3buttongroup.cpp.

00166 {
00167 }


Member Function Documentation

bool Q3ButtonGroup::isExclusive (  )  const

Definition at line 169 of file q3buttongroup.cpp.

References QButtonGroup::exclusive(), and group.

Referenced by insert_helper().

00170 {
00171     return group.exclusive();
00172 }

Here is the call graph for this function:

bool Q3ButtonGroup::isRadioButtonExclusive (  )  const [inline]

Definition at line 55 of file q3buttongroup.h.

00055 { return radio_excl; }

void Q3ButtonGroup::setExclusive ( bool   ) 

Definition at line 174 of file q3buttongroup.cpp.

References group, and QButtonGroup::setExclusive().

Referenced by Q3FileDialog::init().

00175 {
00176     group.setExclusive(enable);
00177 }

Here is the call graph for this function:

void Q3ButtonGroup::setRadioButtonExclusive ( bool   ) 

Definition at line 349 of file q3buttongroup.cpp.

References radio_excl.

00350 {
00351     radio_excl = on;
00352 }

int Q3ButtonGroup::insert ( QAbstractButton button,
int  id = -1 
)

Inserts the button with the identifier id into the button group. Returns the button identifier.

Buttons are normally inserted into a button group automatically by passing the button group as the parent when the button is constructed. So it is not necessary to manually insert buttons that have this button group as their parent widget. An exception is when you want custom identifiers instead of the default 0, 1, 2, etc., or if you want the buttons to have some other parent.

The button is assigned the identifier id or an automatically generated identifier. It works as follows: If id >= 0, this identifier is assigned. If id == -1 (default), the identifier is equal to the number of buttons in the group. If id is any other negative integer, for instance -2, a unique identifier (negative integer <= -2) is generated. No button has an id of -1.

See also:
find(), remove(), setExclusive()

Definition at line 201 of file q3buttongroup.cpp.

References insert_helper(), and remove().

Referenced by event(), and Q3FileDialog::init().

00202 {
00203     remove(button);
00204     return insert_helper(button, id);
00205 }

Here is the call graph for this function:

void Q3ButtonGroup::remove ( QAbstractButton button  ) 

Removes the button from the button group.

See also:
insert()

Definition at line 239 of file q3buttongroup.cpp.

References QMap< Key, T >::begin(), buttonIds, QObject::disconnect(), QMap< Key, T >::end(), QMap< Key, T >::erase(), group, QButtonGroup::removeButton(), and QMap< Key, T >::value().

Referenced by insert().

00240 {
00241     QMap<int, QAbstractButton*>::Iterator it = buttonIds.begin();
00242     while (it != buttonIds.end()) {
00243         if (it.value() == button) {
00244             buttonIds.erase(it);
00245             button->disconnect(this);
00246             group.removeButton(button);
00247             break;
00248         }
00249         ++it;
00250     }
00251 }

Here is the call graph for this function:

QAbstractButton * Q3ButtonGroup::find ( int  id  )  const

Returns the button with the specified identifier id, or 0 if the button was not found.

Definition at line 259 of file q3buttongroup.cpp.

References buttonIds, fixChildren(), and QMap< Key, T >::value().

Referenced by Q3FileDialog::changeMode(), and setButton().

00260 {
00261     fixChildren();
00262     return buttonIds.value(id);
00263 }

Here is the call graph for this function:

int Q3ButtonGroup::id ( QAbstractButton button  )  const

Returns the id of button, or -1 if button is not a member of this group.

See also:
selectedId()

Definition at line 404 of file q3buttongroup.cpp.

References buttonIds, QMap< Key, T >::constBegin(), QMap< Key, T >::constEnd(), fixChildren(), QMap< Key, T >::key(), and QMap< Key, T >::value().

Referenced by buttonClicked(), buttonPressed(), buttonReleased(), selectedId(), and Q3FileDialog::setPreviewMode().

00405 {
00406     fixChildren();
00407     QMap<int, QAbstractButton*>::ConstIterator it = buttonIds.constBegin();
00408     while (it != buttonIds.constEnd()) {
00409         if (it.value() == button)
00410             return it.key();
00411         ++it;
00412     }
00413     return -1;
00414 }

Here is the call graph for this function:

int Q3ButtonGroup::count (  )  const

Returns the number of buttons in the group.

Definition at line 227 of file q3buttongroup.cpp.

References buttonIds, QMap< Key, T >::count(), and fixChildren().

Referenced by fixChildren().

00228 {
00229     fixChildren();
00230     return buttonIds.count();
00231 }

Here is the call graph for this function:

void Q3ButtonGroup::setButton ( int  id  ) 

Definition at line 342 of file q3buttongroup.cpp.

References b, and find().

00343 {
00344     QAbstractButton *b = find(id);
00345     if (b)
00346         b->setOn(true);
00347 }

Here is the call graph for this function:

QAbstractButton * Q3ButtonGroup::selected (  )  const

Returns the selected toggle button if exactly one is selected; otherwise returns 0.

See also:
selectedId()

Definition at line 362 of file q3buttongroup.cpp.

References buttonIds, QMap< Key, T >::constBegin(), QMap< Key, T >::constEnd(), fixChildren(), QAbstractButton::isCheckable(), QAbstractButton::isChecked(), and QMap< Key, T >::value().

Referenced by selectedId().

00363 {
00364     fixChildren();
00365     QAbstractButton *candidate = 0;
00366     QMap<int, QAbstractButton*>::ConstIterator it = buttonIds.constBegin();
00367     while (it != buttonIds.constEnd()) {
00368         if (it.value()->isCheckable() && it.value()->isChecked()) {
00369             if (candidate)
00370                 return 0;
00371             candidate = it.value();
00372         }
00373         ++it;
00374     }
00375     return candidate;
00376 }

Here is the call graph for this function:

int Q3ButtonGroup::selectedId (  )  const

Definition at line 391 of file q3buttongroup.cpp.

References id(), and selected().

00392 {
00393     return id(selected());
00394 }

Here is the call graph for this function:

void Q3ButtonGroup::pressed ( int  id  )  [signal]

This signal is emitted when a button in the group is pressed. The id argument is the button's identifier.

See also:
insert()

Referenced by buttonPressed(), and insert_helper().

void Q3ButtonGroup::released ( int  id  )  [signal]

This signal is emitted when a button in the group is released. The id argument is the button's identifier.

See also:
insert()

Referenced by buttonReleased(), and insert_helper().

void Q3ButtonGroup::clicked ( int  id  )  [signal]

This signal is emitted when a button in the group is clicked. The id argument is the button's identifier.

See also:
insert()

Referenced by buttonClicked(), and insert_helper().

void Q3ButtonGroup::buttonPressed (  )  [protected, slot]

Definition at line 303 of file q3buttongroup.cpp.

References emit, id(), pressed(), and QObject::sender().

Referenced by insert_helper().

00304 {
00305     QAbstractButton *senderButton = ::qobject_cast<QAbstractButton *>(sender());
00306     Q_ASSERT(senderButton);
00307     int senderId = id(senderButton);
00308     if (senderId != -1)
00309         emit pressed(senderId);
00310 }

void Q3ButtonGroup::buttonReleased (  )  [protected, slot]

Definition at line 318 of file q3buttongroup.cpp.

References emit, id(), released(), and QObject::sender().

Referenced by insert_helper().

00319 {
00320     QAbstractButton *senderButton = ::qobject_cast<QAbstractButton *>(sender());
00321     Q_ASSERT(senderButton);
00322     int senderId = id(senderButton);
00323     if (senderId != -1)
00324         emit released(senderId);
00325 }

void Q3ButtonGroup::buttonClicked (  )  [protected, slot]

Definition at line 333 of file q3buttongroup.cpp.

References clicked(), emit, id(), and QObject::sender().

Referenced by insert_helper().

00334 {
00335     QAbstractButton *senderButton = ::qobject_cast<QAbstractButton *>(sender());
00336     Q_ASSERT(senderButton);
00337     int senderId = id(senderButton);
00338     if (senderId != -1)
00339         emit clicked(senderId);
00340 }

bool Q3ButtonGroup::event ( QEvent e  )  [protected, virtual]

Reimplemented from QGroupBox.

Definition at line 420 of file q3buttongroup.cpp.

References buttonIds, QMap< Key, T >::constBegin(), QMap< Key, T >::constEnd(), QGroupBox::event(), QButtonGroup::exclusive(), group, insert(), QEvent::type(), and QMap< Key, T >::value().

00421 {
00422     if (e->type() == QEvent::ChildInserted) {
00423         QChildEvent * ce = (QChildEvent *) e;
00424         if (QAbstractButton *button = qobject_cast<QAbstractButton*>(ce->child())) {
00425             button->setAutoExclusive(false);
00426             if (group.exclusive() || qobject_cast<QRadioButton*>(button)) {
00427                 button->setAutoExclusive(true);
00428                 QMap<int, QAbstractButton*>::ConstIterator it = buttonIds.constBegin();
00429                 while (it != buttonIds.constEnd()) {
00430                     if (it.value() == button)
00431                         return Q3GroupBox::event(e);
00432                     ++it;
00433                 }
00434                 insert(button);
00435             }
00436         }
00437     }
00438     return Q3GroupBox::event(e);
00439 }

Here is the call graph for this function:

void Q3ButtonGroup::init (  )  [private]

Initializes the button group.

Reimplemented from Q3GroupBox.

Definition at line 157 of file q3buttongroup.cpp.

References group, radio_excl, and QButtonGroup::setExclusive().

Referenced by Q3ButtonGroup().

00158 {
00159     group.setExclusive(false);
00160     radio_excl = true;
00161 }

Here is the call graph for this function:

void Q3ButtonGroup::fixChildren (  )  const [private]

Definition at line 441 of file q3buttongroup.cpp.

References QList< T >::at(), buttonIds, QObject::children(), QMap< Key, T >::constBegin(), QMap< Key, T >::constEnd(), QList< T >::count(), QMap< Key, T >::count(), count(), i, insert_helper(), and set().

Referenced by count(), find(), id(), and selected().

00442 {
00443     if (children().count() == buttonIds.count())
00444         return; // small optimization, all our children have ids.
00445 
00446     QList<QAbstractButton *> list = ::qFindChildren<QAbstractButton*>(this);
00447     QSet<QAbstractButton*> set;
00448     for (QMap<int, QAbstractButton*>::ConstIterator it = buttonIds.constBegin();
00449             it != buttonIds.constEnd(); ++it)
00450         set.insert(*it);
00451     for (int i = 0; i < list.count(); ++i)
00452         if (!set.contains(list.at(i)))
00453             const_cast<Q3ButtonGroup*>(this)->insert_helper(list.at(i));
00454 }

Here is the call graph for this function:

int Q3ButtonGroup::insert_helper ( QAbstractButton ,
int  id = -1 
) [private]

Definition at line 207 of file q3buttongroup.cpp.

References QButtonGroup::addButton(), buttonClicked(), buttonIds, buttonPressed(), buttonReleased(), clicked(), QObject::connect(), QMap< Key, T >::count(), group, QMap< Key, T >::insert(), isExclusive(), pressed(), released(), SIGNAL, and SLOT.

Referenced by fixChildren(), and insert().

00208 {
00209     if (isExclusive() || !qobject_cast<QRadioButton*>(button))
00210         group.addButton(button);
00211 
00212     static int seq_no = -2;
00213     if (id < -1)
00214         id = seq_no--;
00215     else if (id == -1)
00216         id = buttonIds.count();
00217     buttonIds.insert(id, button);
00218     connect(button, SIGNAL(pressed()) , SLOT(buttonPressed()));
00219     connect(button, SIGNAL(released()), SLOT(buttonReleased()));
00220     connect(button, SIGNAL(clicked()) , SLOT(buttonClicked()));
00221     return id;
00222 }

Here is the call graph for this function:


Member Data Documentation

bool Q3ButtonGroup::excl_grp [private]

Definition at line 91 of file q3buttongroup.h.

bool Q3ButtonGroup::radio_excl [private]

Definition at line 92 of file q3buttongroup.h.

Referenced by init(), and setRadioButtonExclusive().

QMap<int, QAbstractButton*> Q3ButtonGroup::buttonIds [private]

Definition at line 93 of file q3buttongroup.h.

Referenced by count(), event(), find(), fixChildren(), id(), insert_helper(), remove(), and selected().

QButtonGroup Q3ButtonGroup::group [private]

Definition at line 94 of file q3buttongroup.h.

Referenced by event(), init(), insert_helper(), isExclusive(), remove(), and setExclusive().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 15:41:50 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1