LittleSaints
LittleSaints

Reputation: 421

How to use TAB Key to focus one of two qpushbutton

In a widget I put two QPushButton (let's say "OK" at left and "EXIT" at right). They regularly work when I press them using the mouse. Suppose I want to switch from one to the other using TAB key: is it possible? And how can do this?

Upvotes: 1

Views: 2924

Answers (3)

Ronny Brendel
Ronny Brendel

Reputation: 4845

I tried it out on KDE/Ubuntu. It works automatically.

main.cpp

#include <QApplication>
#include "mainwindow.hpp"

int main(int argc, char** args) {
    QApplication app(argc, args);

    MainWindow m;
    m.show();

    return app.exec();
}

mainwindow.hpp

#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

#include <QMainWindow>

class MainWindow : public QMainWindow {
    Q_OBJECT
public:
    MainWindow();
};

#endif // MAINWINDOW_HPP

mainwindow.cpp

#include "mainwindow.hpp"

#include <QPushButton>
#include <QVBoxLayout>

MainWindow::MainWindow() : QMainWindow() {

    auto* w = new QWidget;
    auto* l = new QVBoxLayout;

    auto* p1 = new QPushButton("ok");
    auto* p2 = new QPushButton("exit");

    l->addWidget(p1);
    l->addWidget(p2);

    w->setLayout(l);
    setCentralWidget(w);
}

a.pro

TEMPLATE = app
TARGET = a
INCLUDEPATH += .

QT += widgets

HEADERS += mainwindow.hpp
SOURCES += main.cpp mainwindow.cpp

QMAKE_CXXFLAGS += -std=c++14

Edit: Apparently the buttons switch focus, but pressing enter does nothing. I guess you have to use focus-related mechanics (search for "focus" in the QWidget documentation) and implement it yourself. Or have a look at QDialog (as a replacement for QMainWindow in my example). It should have some meaningful default behavior for the enter and escape buttons.

Side note: Maybe you rather want to use the QDialogButtonBox for ok- and exit-buttons in your project. It's the cross-platform way of displaying OK/Cancel/Accept/Reject/... buttons because their arrangement differs between platforms. And this class can help you with that.

Upvotes: 3

Megasa3
Megasa3

Reputation: 764

It is easier than all that code. Just use setFocusPolicy with Tabfocus on both buttons like this:

 yourButtonOk->setFocusPolicy(Qt::TabFocus);
 yourButtonExit->setFocusPolicy(Qt::TabFocus);

Upvotes: 2

On some platforms, keyboard focus navigation among buttons is a default behavior, but on some it isn't.

If you wish keyboard navigation on all platforms, the buttons should have a Qt::StrongFocus policy set on them. Note that the shortcut used to trigger the buttons is also platform-specific. E.g. on OS X you'd use Space.

#include <QtWidgets>

int main(int argc, char ** argv) {
   QApplication app{argc, argv};
   QWidget w;
   QVBoxLayout layout{&w};
   // Individual Buttons
   QPushButton p1{"button1"}, p2{"button2"};
   for (auto p : {&p1, &p2}) {
      layout.addWidget(p);
      p->setFocusPolicy(Qt::StrongFocus);
   }
   // A button box
   QDialogButtonBox box;
   for (auto text : {"button3", "button4"})
      box.addButton(text, QDialogButtonBox::NoRole)->setFocusPolicy(Qt::StrongFocus);
   layout.addWidget(&box);

   w.show();
   return app.exec();
}

Upvotes: 5

Related Questions