Gui: add button group that allows to uncheck all buttons in exclusive mode

This commit is contained in:
wmayer
2021-11-22 23:00:30 +01:00
parent f5b880563e
commit b787f05d66
2 changed files with 50 additions and 0 deletions

View File

@@ -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"

View File

@@ -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