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.
This commit is contained in:
Veloman Yunkan 2024-03-12 16:42:45 +04:00
parent b39597b501
commit dcc41e2bc2

View File

@ -52,25 +52,13 @@ TabBar::TabBar(QWidget *parent) :
for (int i = 0 ; i <= 9 ; i++) { for (int i = 0 ; i <= 9 ; i++) {
QAction *a = new QAction(this); QAction *a = new QAction(this);
a->setData(QVariant::fromValue(i)); a->setShortcut(QKeySequence(Qt::ALT | (Qt::Key_0 + i)));
QKeySequence ks(Qt::ALT | (Qt::Key_0 + i));
a->setShortcut(ks);
addAction(a); addAction(a);
connect(a, &QAction::triggered, this, [=](){ connect(a, &QAction::triggered, this, [=](){
QAction *a = qobject_cast<QAction*>(QObject::sender()); const int tabIndex = i == 0 ? 9 : i - 1;
if (!a) if (tabIndex < realTabCount()) {
return; setCurrentIndex(tabIndex);
}
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);
}); });
} }