diff options
author | Michael Weghorn <m.weghorn@posteo.de> | 2024-12-13 22:55:50 +0100 |
---|---|---|
committer | Michael Weghorn <m.weghorn@posteo.de> | 2024-12-14 01:03:50 +0100 |
commit | e32d069ba74229916c459842d0c27c5a89b82168 (patch) | |
tree | 652f1cd56f2f4cdcf6d1cc3f5bb06c2294eceb03 /vcl | |
parent | 7f64114f90c58599c459b9b5ce622e4d97bf9388 (diff) |
tdf#130857 qt weld: Evaluate radio button groups
Add support for radio button groups by implementing
QtBuilder::setRadioButtonGroup.
Qt provides QButtonGroup [1] to explicitly group
buttons. Quoting from the QRadioButton doc [2]:
> Radio buttons are autoExclusive by default. If auto-exclusive is
> enabled, radio buttons that belong to the same parent widget behave as
> if they were part of the same exclusive button group. If you need
> multiple exclusive button groups for radio buttons that belong to the
> same parent widget, put them into a QButtonGroup.
To make sure exactly one button group exists and it gets
freed automatically, make the radio button whose ID is set for
the GtkRadioButton::group property [3] the parent (and thus the
owner) of the group.
Set a pointer of the group as a property for that button,
so it can be retrieved directly from the button without requiring
another map/... to keep track.
With this in place, the radio buttons in Writer's
"Tools" -> "Options" -> "LibreOfficeDev Writer"
-> "Mail Merge Email" -> "Server Authentication"
dialog are now grouped into 2 groups as expected in
a WIP branch, rather than all of them being in one group (because
they all have the same grid as a parent).
Support for that dialog will be declared in an
upcoming commit.
[1] https://doc.qt.io/qt-6/qbuttongroup.html
[2] https://doc.qt.io/qt-6/qradiobutton.html
[3] https://docs.gtk.org/gtk3/property.RadioButton.group.html
Change-Id: I60e8b5b525411c299ac50a8712361cc8572cc403
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/178444
Tested-by: Jenkins
Reviewed-by: Michael Weghorn <m.weghorn@posteo.de>
Diffstat (limited to 'vcl')
-rw-r--r-- | vcl/qt5/QtBuilder.cxx | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/vcl/qt5/QtBuilder.cxx b/vcl/qt5/QtBuilder.cxx index 963d8e71e861..7d78acb281f8 100644 --- a/vcl/qt5/QtBuilder.cxx +++ b/vcl/qt5/QtBuilder.cxx @@ -20,6 +20,7 @@ #include <vcl/qt/QtUtils.hxx> #include <QtGui/QStandardItemModel> +#include <QtWidgets/QButtonGroup> #include <QtWidgets/QCheckBox> #include <QtWidgets/QComboBox> #include <QtWidgets/QDialog> @@ -275,6 +276,7 @@ QObject* QtBuilder::makeObject(QObject* pParent, std::u16string_view sName, std: else if (sName == u"GtkRadioButton") { pObject = new QRadioButton(pParentWidget); + extractRadioButtonGroup(sID, rMap); } else if (sName == u"GtkScrolledWindow") { @@ -473,9 +475,30 @@ void QtBuilder::setMnemonicWidget(const OUString& rLabelId, const OUString& rMne pLabel->setBuddy(static_cast<QWidget*>(pBuddy)); } -void QtBuilder::setRadioButtonGroup(const OUString&, const OUString&) +void QtBuilder::setRadioButtonGroup(const OUString& rRadioButtonId, const OUString& rRadioGroupId) { - SAL_WARN("vcl.qt", "Not implemented yet"); + // insert all buttons into a button group owned by button whose matches the group's + QRadioButton* pGroupOwner = get<QRadioButton>(rRadioGroupId); + assert(pGroupOwner && "No radio button with the given group name"); + + QButtonGroup* pButtonGroup = nullptr; + static const char* const pPropertyKey = "PROPERTY_BUTTONGROUP"; + QVariant aVariant = pGroupOwner->property(pPropertyKey); + if (aVariant.canConvert<QButtonGroup*>()) + { + pButtonGroup = aVariant.value<QButtonGroup*>(); + } + else + { + pButtonGroup = new QButtonGroup(pGroupOwner); + pButtonGroup->addButton(pGroupOwner); + } + + QRadioButton* pRadioButton = get<QRadioButton>(rRadioButtonId); + assert(pRadioButton && "No radio button with given ID"); + pButtonGroup->addButton(pRadioButton); + + pGroupOwner->setProperty(pPropertyKey, QVariant::fromValue(pButtonGroup)); } void QtBuilder::setPriority(QObject*, int) { SAL_WARN("vcl.qt", "Ignoring priority"); } |