diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm
index 399d2cb2586..e33802809c1 100644
--- a/src/plugins/styles/mac/qmacstyle_mac.mm
+++ b/src/plugins/styles/mac/qmacstyle_mac.mm
@@ -2821,6 +2821,9 @@ int QMacStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget *w
     case SH_TabBar_AllowWheelScrolling:
         ret = false;
         break;
+    case SH_BoxLayoutUseEffectiveMargins:
+        ret = true;
+        break;
     default:
         ret = QCommonStyle::styleHint(sh, opt, w, hret);
         break;
diff --git a/src/widgets/kernel/qboxlayout.cpp b/src/widgets/kernel/qboxlayout.cpp
index 00860f14ebc..5179bc098f7 100644
--- a/src/widgets/kernel/qboxlayout.cpp
+++ b/src/widgets/kernel/qboxlayout.cpp
@@ -113,89 +113,93 @@ void QBoxLayoutPrivate::effectiveMargins(int *left, int *top, int *right, int *b
     int b = bottomMargin;
 #ifdef Q_OS_MAC
     Q_Q(const QBoxLayout);
-    if (horz(dir)) {
-        QBoxLayoutItem *leftBox = nullptr;
-        QBoxLayoutItem *rightBox = nullptr;
-
-        if (left || right) {
-            leftBox = list.value(0);
-            rightBox = list.value(list.count() - 1);
-            if (dir == QBoxLayout::RightToLeft)
-                qSwap(leftBox, rightBox);
-
-            int leftDelta = 0;
-            int rightDelta = 0;
-            if (leftBox) {
-                QLayoutItem *itm = leftBox->item;
-                if (QWidget *w = itm->widget())
-                    leftDelta = itm->geometry().left() - w->geometry().left();
-            }
-            if (rightBox) {
-                QLayoutItem *itm = rightBox->item;
-                if (QWidget *w = itm->widget())
-                    rightDelta = w->geometry().right() - itm->geometry().right();
-            }
-            QWidget *w = q->parentWidget();
-            Qt::LayoutDirection layoutDirection = w ? w->layoutDirection() : QGuiApplication::layoutDirection();
-            if (layoutDirection == Qt::RightToLeft)
-                qSwap(leftDelta, rightDelta);
-
-            l = qMax(l, leftDelta);
-            r = qMax(r, rightDelta);
-        }
+    QWidget *parentWidget = q->parentWidget();
+    QStyle *style = parentWidget != nullptr ? parentWidget->style() : QApplication::style();
+    if (style->styleHint(QStyle::SH_BoxLayoutUseEffectiveMargins, nullptr, parentWidget)) {
+        if (horz(dir)) {
+            QBoxLayoutItem *leftBox = nullptr;
+            QBoxLayoutItem *rightBox = nullptr;
+
+            if (left || right) {
+                leftBox = list.value(0);
+                rightBox = list.value(list.count() - 1);
+                if (dir == QBoxLayout::RightToLeft)
+                    qSwap(leftBox, rightBox);
+
+                int leftDelta = 0;
+                int rightDelta = 0;
+                if (leftBox) {
+                    QLayoutItem *itm = leftBox->item;
+                    if (QWidget *w = itm->widget())
+                        leftDelta = itm->geometry().left() - w->geometry().left();
+                }
+                if (rightBox) {
+                    QLayoutItem *itm = rightBox->item;
+                    if (QWidget *w = itm->widget())
+                        rightDelta = w->geometry().right() - itm->geometry().right();
+                }
+                QWidget *w = q->parentWidget();
+                Qt::LayoutDirection layoutDirection = w ? w->layoutDirection() : QGuiApplication::layoutDirection();
+                if (layoutDirection == Qt::RightToLeft)
+                    qSwap(leftDelta, rightDelta);
 
-        int count = top || bottom ? list.count() : 0;
-        for (int i = 0; i < count; ++i) {
-            QBoxLayoutItem *box = list.at(i);
-            QLayoutItem *itm = box->item;
-            QWidget *w = itm->widget();
-            if (w) {
-                QRect lir = itm->geometry();
-                QRect wr = w->geometry();
-                if (top)
-                    t = qMax(t, lir.top() - wr.top());
-                if (bottom)
-                    b = qMax(b, wr.bottom() - lir.bottom());
-            }
-        }
-    } else {    // vertical layout
-        QBoxLayoutItem *topBox = nullptr;
-        QBoxLayoutItem *bottomBox = nullptr;
-
-        if (top || bottom) {
-            topBox = list.value(0);
-            bottomBox = list.value(list.count() - 1);
-            if (dir == QBoxLayout::BottomToTop) {
-                qSwap(topBox, bottomBox);
+                l = qMax(l, leftDelta);
+                r = qMax(r, rightDelta);
             }
 
-            if (top && topBox) {
-                QLayoutItem *itm = topBox->item;
+            int count = top || bottom ? list.count() : 0;
+            for (int i = 0; i < count; ++i) {
+                QBoxLayoutItem *box = list.at(i);
+                QLayoutItem *itm = box->item;
                 QWidget *w = itm->widget();
-                if (w)
-                    t = qMax(t, itm->geometry().top() - w->geometry().top());
+                if (w) {
+                    QRect lir = itm->geometry();
+                    QRect wr = w->geometry();
+                    if (top)
+                        t = qMax(t, lir.top() - wr.top());
+                    if (bottom)
+                        b = qMax(b, wr.bottom() - lir.bottom());
+                }
             }
+        } else {    // vertical layout
+            QBoxLayoutItem *topBox = nullptr;
+            QBoxLayoutItem *bottomBox = nullptr;
+
+            if (top || bottom) {
+                topBox = list.value(0);
+                bottomBox = list.value(list.count() - 1);
+                if (dir == QBoxLayout::BottomToTop) {
+                    qSwap(topBox, bottomBox);
+                }
 
-            if (bottom && bottomBox) {
-                QLayoutItem *itm = bottomBox->item;
-                QWidget *w = itm->widget();
-                if (w)
-                    b = qMax(b, w->geometry().bottom() - itm->geometry().bottom());
+                if (top && topBox) {
+                    QLayoutItem *itm = topBox->item;
+                    QWidget *w = itm->widget();
+                    if (w)
+                        t = qMax(t, itm->geometry().top() - w->geometry().top());
+                }
+
+                if (bottom && bottomBox) {
+                    QLayoutItem *itm = bottomBox->item;
+                    QWidget *w = itm->widget();
+                    if (w)
+                        b = qMax(b, w->geometry().bottom() - itm->geometry().bottom());
+                }
             }
-        }
 
-        int count = left || right ? list.count() : 0;
-        for (int i = 0; i < count; ++i) {
-            QBoxLayoutItem *box = list.at(i);
-            QLayoutItem *itm = box->item;
-            QWidget *w = itm->widget();
-            if (w) {
-                QRect lir = itm->geometry();
-                QRect wr = w->geometry();
-                if (left)
-                    l = qMax(l, lir.left() - wr.left());
-                if (right)
-                    r = qMax(r, wr.right() - lir.right());
+            int count = left || right ? list.count() : 0;
+            for (int i = 0; i < count; ++i) {
+                QBoxLayoutItem *box = list.at(i);
+                QLayoutItem *itm = box->item;
+                QWidget *w = itm->widget();
+                if (w) {
+                    QRect lir = itm->geometry();
+                    QRect wr = w->geometry();
+                    if (left)
+                        l = qMax(l, lir.left() - wr.left());
+                    if (right)
+                        r = qMax(r, wr.right() - lir.right());
+                }
             }
         }
     }
diff --git a/src/widgets/styles/qcommonstyle.cpp b/src/widgets/styles/qcommonstyle.cpp
index de3b1cd60ec..dcdd3cb74c9 100644
--- a/src/widgets/styles/qcommonstyle.cpp
+++ b/src/widgets/styles/qcommonstyle.cpp
@@ -5455,6 +5455,7 @@ int QCommonStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget
     case SH_ItemView_PaintAlternatingRowColorsForEmptyArea:
     case SH_ComboBox_UseNativePopup:
     case SH_Table_AlwaysDrawLeftTopGridLines:
+    case SH_BoxLayoutUseEffectiveMargins:
         ret = false;
         break;
     case SH_MenuBar_AltKeyNavigation:
diff --git a/src/widgets/styles/qstyle.cpp b/src/widgets/styles/qstyle.cpp
index 6bb0728cfd4..ac2cc5dbf7c 100644
--- a/src/widgets/styles/qstyle.cpp
+++ b/src/widgets/styles/qstyle.cpp
@@ -1984,6 +1984,9 @@ void QStyle::drawItemPixmap(QPainter *painter, const QRect &rect, int alignment,
            not when the header is hidden. Defaults to false.
            This enum value has been introduced in Qt 6.3.
 
+    \value SH_BoxLayoutUseEffectiveMargins
+           Determines if the effective margins can be used in a QBoxLayout.
+
     \sa styleHint()
 */
 
diff --git a/src/widgets/styles/qstyle.h b/src/widgets/styles/qstyle.h
index db09eedc70d..4920def94ef 100644
--- a/src/widgets/styles/qstyle.h
+++ b/src/widgets/styles/qstyle.h
@@ -704,6 +704,7 @@ public:
         SH_TabBar_AllowWheelScrolling,
         SH_Table_AlwaysDrawLeftTopGridLines,
         SH_SpinBox_SelectOnStep,
+        SH_BoxLayoutUseEffectiveMargins,
         // Add new style hint values here
 
         SH_CustomBase = 0xf0000000
