Added the feature Alt + <digit> to switch between tabs using QAction::data() (#567)

This commit is contained in:
Rishabh Soni 2021-03-07 12:25:57 +05:30 committed by GitHub
parent 13aa27d4fb
commit 28bb88c6d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -71,6 +71,30 @@ TabBar::TabBar(QWidget *parent) :
setCurrentIndex(index); 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<QAction*>(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 // the slot relies the connection will be direct to reverting back the tab
connect(this, SIGNAL(tabMoved(int,int)), connect(this, SIGNAL(tabMoved(int,int)),
this, SLOT(onTabMoved(int,int)), Qt::DirectConnection); this, SLOT(onTabMoved(int,int)), Qt::DirectConnection);