From dcc41e2bc2f7f4c2b5bf4e922a9aaf03b24eca10 Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 12 Mar 2024 16:42:45 +0400 Subject: [PATCH] More concise handling of switch-to-tab#N shortcuts Obtaining the value of the ALT+[0-9] shortcut from the action objects would be justified if they were connected to the same (shared) handler. But since handlers are distinct lambda objects the sought value can be obtained via a captured variable. --- src/tabbar.cpp | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/tabbar.cpp b/src/tabbar.cpp index 3523c20..39c3752 100644 --- a/src/tabbar.cpp +++ b/src/tabbar.cpp @@ -52,25 +52,13 @@ TabBar::TabBar(QWidget *parent) : for (int i = 0 ; i <= 9 ; i++) { QAction *a = new QAction(this); - a->setData(QVariant::fromValue(i)); - QKeySequence ks(Qt::ALT | (Qt::Key_0 + i)); - a->setShortcut(ks); + a->setShortcut(QKeySequence(Qt::ALT | (Qt::Key_0 + i))); addAction(a); connect(a, &QAction::triggered, this, [=](){ - QAction *a = qobject_cast(QObject::sender()); - if (!a) - return; - - bool ok; - int tab_n = a->data().toInt(&ok); - if (tab_n==0) - tab_n=10; - if (!ok) - return; - if (tab_n >= count()) - return; - - setCurrentIndex(tab_n-1); + const int tabIndex = i == 0 ? 9 : i - 1; + if (tabIndex < realTabCount()) { + setCurrentIndex(tabIndex); + } }); }