Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QButtonGroup Class Reference

The QButtonGroup class provides a container to organize groups of button widgets. More...

#include <qbuttongroup.h>

+ Inheritance diagram for QButtonGroup:
+ Collaboration diagram for QButtonGroup:

Signals

void buttonClicked (QAbstractButton *)
 This signal is emitted when the given button is clicked.
 
void buttonPressed (QAbstractButton *)
 
void buttonReleased (QAbstractButton *)
 
void buttonToggled (QAbstractButton *, bool)
 
void idClicked (int)
 
void idPressed (int)
 
void idReleased (int)
 
void idToggled (int, bool)
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 

Public Member Functions

 QButtonGroup (QObject *parent=nullptr)
 Constructs a new, empty button group with the given parent.
 
 ~QButtonGroup ()
 Destroys the button group.
 
void setExclusive (bool)
 
bool exclusive () const
 
void addButton (QAbstractButton *, int id=-1)
 Adds the given button to the button group.
 
void removeButton (QAbstractButton *)
 Removes the given button from the button group.
 
QList< QAbstractButton * > buttons () const
 Returns the button group's list of buttons.
 
QAbstractButtoncheckedButton () const
 Returns the button group's checked button, or \nullptr if no buttons are checked.
 
QAbstractButtonbutton (int id) const
 
void setId (QAbstractButton *button, int id)
 
int id (QAbstractButton *button) const
 
int checkedId () const
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool event (QEvent *event)
 This virtual function receives events to an object and should return true if the event e was recognized and processed.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Properties

bool exclusive
 whether the button group is exclusive
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Friends

class QAbstractButton
 
class QAbstractButtonPrivate
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 

Detailed Description

The QButtonGroup class provides a container to organize groups of button widgets.

\inmodule QtWidgets

QButtonGroup provides an abstract container into which button widgets can be placed. It does not provide a visual representation of this container (see QGroupBox for a container widget), but instead manages the states of each of the buttons in the group.

An \l {QButtonGroup::exclusive} {exclusive} button group switches off all checkable (toggle) buttons except the one that has been clicked. By default, a button group is exclusive. The buttons in a button group are usually checkable \l{QPushButton}s, \l{QCheckBox}es (normally for non-exclusive button groups), or \l{QRadioButton}s. If you create an exclusive button group, you should ensure that one of the buttons in the group is initially checked; otherwise, the group will initially be in a state where no buttons are checked.

A button can be added to the group with addButton() and removed with removeButton(). If the group is exclusive, the currently checked button is available with checkedButton(). If a button is clicked, the buttonClicked() signal is emitted; for a checkable button in an exclusive group this means that the button has been checked. The list of buttons in the group is returned by buttons().

In addition, QButtonGroup can map between integers and buttons. You can assign an integer id to a button with setId(), and retrieve it with id(). The id of the currently checked button is available with checkedId(), and there is a signal idClicked() that emits the id of the button. The id {-1} is reserved by QButtonGroup to mean "no such button". The purpose of the mapping mechanism is to simplify the representation of enum values in a user interface.

See also
QGroupBox, QPushButton, QCheckBox, QRadioButton

Definition at line 18 of file qbuttongroup.h.

Constructor & Destructor Documentation

◆ QButtonGroup()

QButtonGroup::QButtonGroup ( QObject * parent = nullptr)
explicit

Constructs a new, empty button group with the given parent.

See also
addButton(), setExclusive()

Definition at line 74 of file qbuttongroup.cpp.

◆ ~QButtonGroup()

QButtonGroup::~QButtonGroup ( )

Destroys the button group.

Definition at line 82 of file qbuttongroup.cpp.

Member Function Documentation

◆ addButton()

void QButtonGroup::addButton ( QAbstractButton * button,
int id = -1 )

Adds the given button to the button group.

If id is -1, an id will be assigned to the button. Automatically assigned ids are guaranteed to be negative, starting with -2. If you are assigning your own ids, use positive values to avoid conflicts.

See also
removeButton(), buttons()

Definition at line 206 of file qbuttongroup.cpp.

◆ button()

QAbstractButton * QButtonGroup::button ( int id) const
Since
4.1

Returns the button with the specified id, or \nullptr if no such button exists.

Definition at line 274 of file qbuttongroup.cpp.

◆ buttonClicked

void QButtonGroup::buttonClicked ( QAbstractButton * button)
signal

This signal is emitted when the given button is clicked.

A button is clicked when it is first pressed and then released, when its shortcut key is typed, or when QAbstractButton::click() or QAbstractButton::animateClick() is programmatically called.

See also
checkedButton(), QAbstractButton::clicked()

◆ buttonPressed

void QButtonGroup::buttonPressed ( QAbstractButton * button)
signal
Since
4.2

This signal is emitted when the given button is pressed down.

See also
QAbstractButton::pressed()

◆ buttonReleased

void QButtonGroup::buttonReleased ( QAbstractButton * button)
signal
Since
4.2

This signal is emitted when the given button is released.

See also
QAbstractButton::released()

◆ buttons()

QList< QAbstractButton * > QButtonGroup::buttons ( ) const

Returns the button group's list of buttons.

This may be empty.

See also
addButton(), removeButton()

Definition at line 250 of file qbuttongroup.cpp.

◆ buttonToggled

void QButtonGroup::buttonToggled ( QAbstractButton * button,
bool checked )
signal
Since
5.2

This signal is emitted when the given button is toggled. checked is true if the button is checked, or false if the button is unchecked.

See also
QAbstractButton::toggled()

◆ checkedButton()

QAbstractButton * QButtonGroup::checkedButton ( ) const

Returns the button group's checked button, or \nullptr if no buttons are checked.

See also
buttonClicked()

Definition at line 262 of file qbuttongroup.cpp.

◆ checkedId()

int QButtonGroup::checkedId ( ) const
Since
4.1

Returns the id of the checkedButton(), or -1 if no button is checked.

See also
setId()

Definition at line 317 of file qbuttongroup.cpp.

◆ exclusive()

bool QButtonGroup::exclusive ( ) const

Definition at line 103 of file qbuttongroup.cpp.

◆ id()

int QButtonGroup::id ( QAbstractButton * button) const
Since
4.1

Returns the id for the specified button, or -1 if no such button exists.

See also
setId()

Definition at line 304 of file qbuttongroup.cpp.

◆ idClicked

void QButtonGroup::idClicked ( int id)
signal
Since
5.15

This signal is emitted when a button with the given id is clicked.

See also
checkedButton(), QAbstractButton::clicked()

◆ idPressed

void QButtonGroup::idPressed ( int id)
signal
Since
5.15

This signal is emitted when a button with the given id is pressed down.

See also
QAbstractButton::pressed()

◆ idReleased

void QButtonGroup::idReleased ( int id)
signal
Since
5.15

This signal is emitted when a button with the given id is released.

See also
QAbstractButton::released()

◆ idToggled

void QButtonGroup::idToggled ( int id,
bool checked )
signal
Since
5.15

This signal is emitted when a button with the given id is toggled. checked is true if the button is checked, or false if the button is unchecked.

See also
QAbstractButton::toggled()

◆ removeButton()

void QButtonGroup::removeButton ( QAbstractButton * button)

Removes the given button from the button group.

See also
addButton(), buttons()

Definition at line 232 of file qbuttongroup.cpp.

◆ setExclusive()

void QButtonGroup::setExclusive ( bool exclusive)

Definition at line 109 of file qbuttongroup.cpp.

◆ setId()

void QButtonGroup::setId ( QAbstractButton * button,
int id )
Since
4.1

Sets the id for the specified button. Note that id cannot be -1.

See also
id()

Definition at line 288 of file qbuttongroup.cpp.

Friends And Related Symbol Documentation

◆ QAbstractButton

friend class QAbstractButton
friend

Definition at line 56 of file qbuttongroup.h.

◆ QAbstractButtonPrivate

friend class QAbstractButtonPrivate
friend

Definition at line 57 of file qbuttongroup.h.

Property Documentation

◆ exclusive

bool QButtonGroup::exclusive
readwrite

whether the button group is exclusive

If this property is true, then only one button in the group can be checked at any given time. The user can click on any button to check it, and that button will replace the existing one as the checked button in the group.

In an exclusive group, the user cannot uncheck the currently checked button by clicking on it; instead, another button in the group must be clicked to set the new checked button for that group.

By default, this property is true.

Definition at line 22 of file qbuttongroup.h.


The documentation for this class was generated from the following files: