Gui: add button group that allows to uncheck all buttons in exclusive mode
This commit is contained in:
@@ -1664,4 +1664,34 @@ void ExpLineEdit::keyPressEvent(QKeyEvent *event)
|
||||
QLineEdit::keyPressEvent(event);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------
|
||||
|
||||
ButtonGroup::ButtonGroup(QObject *parent)
|
||||
: QButtonGroup(parent)
|
||||
, _exclusive(true)
|
||||
{
|
||||
QButtonGroup::setExclusive(false);
|
||||
|
||||
connect(this, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked),
|
||||
[=](QAbstractButton *button) {
|
||||
if (exclusive()) {
|
||||
for (auto btn : buttons()) {
|
||||
if (btn && btn != button && btn->isCheckable())
|
||||
btn->setChecked(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void ButtonGroup::setExclusive(bool on)
|
||||
{
|
||||
_exclusive = on;
|
||||
}
|
||||
|
||||
bool ButtonGroup::exclusive() const
|
||||
{
|
||||
return _exclusive;
|
||||
}
|
||||
|
||||
|
||||
#include "moc_Widgets.cpp"
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#ifndef GUI_WIDGETS_H
|
||||
#define GUI_WIDGETS_H
|
||||
|
||||
#include <QButtonGroup>
|
||||
#include <QDialog>
|
||||
#include <QListWidget>
|
||||
#include <QLabel>
|
||||
@@ -572,6 +573,25 @@ private:
|
||||
bool autoClose;
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief The ButtonGroup class
|
||||
* Unlike Qt's QButtonGroup this class allows it that in exclusive mode
|
||||
* all buttons can be unchecked.
|
||||
*/
|
||||
class GuiExport ButtonGroup : public QButtonGroup
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ButtonGroup(QObject *parent = nullptr);
|
||||
|
||||
void setExclusive(bool on);
|
||||
bool exclusive() const;
|
||||
|
||||
private:
|
||||
bool _exclusive;
|
||||
};
|
||||
|
||||
} // namespace Gui
|
||||
|
||||
#endif // GUI_WIDGETS_H
|
||||
|
||||
Reference in New Issue
Block a user