From d8481551d623f6f19e359805b5ad6fbd28356ed2 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 27 May 2018 18:39:41 +0200 Subject: Focus view when changing tabs --- src/mainwindow/widgets/tabwidget.cpp | 7 +++++++ src/mainwindow/window.cpp | 19 +++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/mainwindow/widgets/tabwidget.cpp b/src/mainwindow/widgets/tabwidget.cpp index 66182c3..97c2717 100644 --- a/src/mainwindow/widgets/tabwidget.cpp +++ b/src/mainwindow/widgets/tabwidget.cpp @@ -25,6 +25,13 @@ TabWidget::TabWidget(QWidget *parent) connect(this, &TabWidget::tabCloseRequested, this, &TabWidget::deleteTab); + // when changing tabs, give focus to the widget + // otherwise when closing tabs, the tabwidget will retain focus + connect(this, &TabWidget::currentChanged, this, [this](int index) { + if(widget(index)) + widget(index)->setFocus(); + }); + // context menu tabContextMenu = new QMenu(this); auto *closeTab = tabContextMenu->addAction(tr("Close Tab")); diff --git a/src/mainwindow/window.cpp b/src/mainwindow/window.cpp index 055d945..0c28663 100644 --- a/src/mainwindow/window.cpp +++ b/src/mainwindow/window.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -55,26 +56,20 @@ Window::Window(const QHash &config, QWidget *parent, Qt::Windo tabWidget->setCornerWidget(newTab_button, Qt::TopRightCorner); // general actions - auto *closeTab_action = new QAction(this); - closeTab_action->setShortcut(QKeySequence(config.value("window.shortcuts.close"))); - connect(closeTab_action, &QAction::triggered, this, [=]() { + auto *closeTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.close")), this); + connect(closeTab_shortcut, &QShortcut::activated, this, [=]() { tabWidget->deleteTab(tabWidget->currentIndex()); }); - addAction(closeTab_action); - auto *leftTab_action = new QAction(this); - leftTab_action->setShortcut(QKeySequence(config.value("window.shortcuts.left"))); - connect(leftTab_action, &QAction::triggered, this, [=]() { + auto *leftTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.left")), this); + connect(leftTab_shortcut, &QShortcut::activated, this, [=]() { tabWidget->setCurrentIndex(qMax(0, tabWidget->currentIndex() - 1)); }); - addAction(leftTab_action); - auto *rightTab_action = new QAction(this); - rightTab_action->setShortcut(QKeySequence(config.value("window.shortcuts.right"))); - connect(rightTab_action, &QAction::triggered, this, [=]() { + auto *rightTab_shortcut = new QShortcut(QKeySequence(config.value("window.shortcuts.right")), this); + connect(rightTab_shortcut, &QShortcut::activated, this, [=]() { tabWidget->setCurrentIndex(qMin(tabWidget->currentIndex() + 1, tabWidget->count() - 1)); }); - addAction(rightTab_action); connect(tabWidget, &TabWidget::currentChanged, [this](int index) { if(index < 0) { -- cgit v1.2.1