From 7a7023b7b1693ceaafcbafb30d41fb3a4c820a90 Mon Sep 17 00:00:00 2001 From: Tang Haixiang Date: Mon, 24 Jan 2022 18:11:14 +0800 Subject: [PATCH] Chip example: fix an accidental bool->int conversion when using PMF connections The example was refactored to use the PMF connection syntax. However this introduced a problem: when connecting a QAbstractButton to a slot that has a default parameter, the semantics of the connection change between string-based connections and PMF. Specifically: when connecting the QAbstractButton::clicked(bool checked = false) signal to a slot like View::zoomIn(int level = 1) then a string-based connection like connect(button, SIGNAL(clicked()), this, SLOT(zoomIn())) would call zoomIn without carrying the signal's parameter over. In other words, zoomIn's parameter is defaulted. The same connection using PFM instead is connect(button, &QAbstractButton::clicked, this, &View::zoomIn) which would "connect" the arguments. This makes emissions that pass false as clicked's parameter result in a zoomIn by 0. Fix it by avoiding the default parameter of zoomIn -- just split the function in two (zoomIn and zoomInBy). Amends 8cf812231405e011b422a1505d9a219618fe5cee. Pick-to: 5.15 6.2 6.3 Fixes: QTBUG-100135 Done-with: Giuseppe D'Angelo Change-Id: I10c150c648034449e3154140108de2d64326d965 Reviewed-by: Giuseppe D'Angelo Reviewed-by: Volker Hilsheimer --- examples/widgets/graphicsview/chip/view.cpp | 18 ++++++++++++++---- examples/widgets/graphicsview/chip/view.h | 6 ++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/examples/widgets/graphicsview/chip/view.cpp b/examples/widgets/graphicsview/chip/view.cpp index 186b24655b5..736c9b65700 100644 --- a/examples/widgets/graphicsview/chip/view.cpp +++ b/examples/widgets/graphicsview/chip/view.cpp @@ -65,9 +65,9 @@ void GraphicsView::wheelEvent(QWheelEvent *e) { if (e->modifiers() & Qt::ControlModifier) { if (e->angleDelta().y() > 0) - view->zoomIn(6); + view->zoomInBy(6); else - view->zoomOut(6); + view->zoomOutBy(6); e->accept(); } else { QGraphicsView::wheelEvent(e); @@ -253,12 +253,22 @@ void View::print() #endif } -void View::zoomIn(int level) +void View::zoomIn() +{ + zoomSlider->setValue(zoomSlider->value() + 1); +} + +void View::zoomOut() +{ + zoomSlider->setValue(zoomSlider->value() - 1); +} + +void View::zoomInBy(int level) { zoomSlider->setValue(zoomSlider->value() + level); } -void View::zoomOut(int level) +void View::zoomOutBy(int level) { zoomSlider->setValue(zoomSlider->value() - level); } diff --git a/examples/widgets/graphicsview/chip/view.h b/examples/widgets/graphicsview/chip/view.h index b780702c348..5a200fa4f70 100644 --- a/examples/widgets/graphicsview/chip/view.h +++ b/examples/widgets/graphicsview/chip/view.h @@ -86,8 +86,10 @@ public: QGraphicsView *view() const; public slots: - void zoomIn(int level = 1); - void zoomOut(int level = 1); + void zoomIn(); + void zoomOut(); + void zoomInBy(int level); + void zoomOutBy(int level); private slots: void resetView();