From 28bb88c6d7a2b58f10f0102afc084a9496056fb3 Mon Sep 17 00:00:00 2001 From: Rishabh Soni Date: Sun, 7 Mar 2021 12:25:57 +0530 Subject: [PATCH] Added the feature Alt + to switch between tabs using QAction::data() (#567) --- src/tabbar.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/tabbar.cpp b/src/tabbar.cpp index f9af3c0..4417709 100644 --- a/src/tabbar.cpp +++ b/src/tabbar.cpp @@ -71,6 +71,30 @@ TabBar::TabBar(QWidget *parent) : setCurrentIndex(index); }); + 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); + 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); + }); + } + // the slot relies the connection will be direct to reverting back the tab connect(this, SIGNAL(tabMoved(int,int)), this, SLOT(onTabMoved(int,int)), Qt::DirectConnection);