vcl/inc/qt5/QtBuilder.hxx |    3 ++-
 vcl/qt5/QtBuilder.cxx     |   43 ++++++++++++++++++++++++-------------------
 2 files changed, 26 insertions(+), 20 deletions(-)

New commits:
commit 26ebdd88a66d8b8e983572822ee8694f1fadd4d5
Author:     Michael Weghorn <m.wegh...@posteo.de>
AuthorDate: Wed Jan 8 18:39:32 2025 +0100
Commit:     Michael Weghorn <m.wegh...@posteo.de>
CommitDate: Thu Jan 9 09:35:58 2025 +0100

    tdf#130857 qt weld: Move button prop logic to helper method
    
    Move existing logic to set push button properties
    from QtBuilder::setProperties to a separate helper
    method QtBuilder::setButtonProperties and call it right
    after creating the QPushButton.
    
    This simplifies QtBuilder::setProperties and gets rid
    of one qobject_cast there.
    
    Change-Id: If7c8ae2cc6e53923f0efd196916f847535596e93
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/179966
    Reviewed-by: Michael Weghorn <m.wegh...@posteo.de>
    Tested-by: Jenkins

diff --git a/vcl/inc/qt5/QtBuilder.hxx b/vcl/inc/qt5/QtBuilder.hxx
index 0aefd50cd5ab..bb5ba7ad6278 100644
--- a/vcl/inc/qt5/QtBuilder.hxx
+++ b/vcl/inc/qt5/QtBuilder.hxx
@@ -95,7 +95,8 @@ private:
     static void deleteObject(QObject* pObject);
     // remove pOldWidget from the widget hierarchy and set (child widget) 
pNewWidget in its place
     static void replaceWidget(QWidget* pOldWidget, QWidget* pNewWidget);
-    void setProperties(QObject* obj, stringmap& rProps);
+    static void setProperties(QObject* obj, stringmap& rProps);
+    void setButtonProperties(QPushButton& rButton, stringmap& rProps);
     static void setLabelProperties(QLabel& rLabel, stringmap& rProps);
     void setScaleProperties(QSlider& rSlider, stringmap& rProps);
     void setSpinButtonProperties(QDoubleSpinBox& rSpinBox, stringmap& rProps);
diff --git a/vcl/qt5/QtBuilder.cxx b/vcl/qt5/QtBuilder.cxx
index c3b283a9c1e8..8b1b4293c8ff 100644
--- a/vcl/qt5/QtBuilder.cxx
+++ b/vcl/qt5/QtBuilder.cxx
@@ -184,9 +184,10 @@ QObject* QtBuilder::makeObject(QObject* pParent, 
std::u16string_view sName, std:
     }
     else if (sName == u"GtkButton")
     {
+        QPushButton* pButton = nullptr;
         if (QDialogButtonBox* pButtonBox = 
qobject_cast<QDialogButtonBox*>(pParentWidget))
         {
-            pObject = pButtonBox->addButton("", QDialogButtonBox::NoRole);
+            pButton = pButtonBox->addButton("", QDialogButtonBox::NoRole);
 
             // for message boxes, avoid implicit standard buttons in addition 
to those explicitly added
             if (QMessageBox* pMessageBox = 
qobject_cast<QMessageBox*>(pParentWidget->window()))
@@ -194,8 +195,11 @@ QObject* QtBuilder::makeObject(QObject* pParent, 
std::u16string_view sName, std:
         }
         else
         {
-            pObject = new QPushButton(pParentWidget);
+            pButton = new QPushButton(pParentWidget);
         }
+
+        setButtonProperties(*pButton, rMap);
+        pObject = pButton;
     }
     else if (sName == u"GtkCheckButton")
     {
@@ -801,28 +805,29 @@ void QtBuilder::setProperties(QObject* pObject, 
stringmap& rProps)
                 pTextEdit->setTabChangesFocus(!toBool(rValue));
         }
     }
-    else if (QPushButton* pButton = qobject_cast<QPushButton*>(pObject))
+}
+
+void QtBuilder::setButtonProperties(QPushButton& rButton, stringmap& rProps)
+{
+    for (auto const & [ rKey, rValue ] : rProps)
     {
-        for (auto const & [ rKey, rValue ] : rProps)
+        if (rKey == u"image")
         {
-            if (rKey == u"image")
-            {
-                QLabel* pImageLabel = get<QLabel>(rValue);
-                assert(pImageLabel && "Button has non-existent image set");
+            QLabel* pImageLabel = get<QLabel>(rValue);
+            assert(pImageLabel && "Button has non-existent image set");
 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
-                pButton->setIcon(QIcon(pImageLabel->pixmap()));
+            rButton.setIcon(QIcon(pImageLabel->pixmap()));
 #else
-                
pButton->setIcon(QIcon(pImageLabel->pixmap(Qt::ReturnByValue)));
+            rButton.setIcon(QIcon(pImageLabel->pixmap(Qt::ReturnByValue)));
 #endif
-                // parentless GtkImage in .ui file is only used for setting 
button
-                // image, so the object is no longer needed after doing so
-                if (!pImageLabel->parent())
-                    deleteObject(pImageLabel);
-            }
-            else if (rKey == u"label")
-            {
-                pButton->setText(convertAccelerator(rValue));
-            }
+            // parentless GtkImage in .ui file is only used for setting button
+            // image, so the object is no longer needed after doing so
+            if (!pImageLabel->parent())
+                deleteObject(pImageLabel);
+        }
+        else if (rKey == u"label")
+        {
+            rButton.setText(convertAccelerator(rValue));
         }
     }
 }

Reply via email to