Skip to content

Instantly share code, notes, and snippets.

@Atsushi4
Created April 28, 2020 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Atsushi4/86cd97fcfebf46a1208c60f33c232591 to your computer and use it in GitHub Desktop.
Save Atsushi4/86cd97fcfebf46a1208c60f33c232591 to your computer and use it in GitHub Desktop.
[Qt] TabキーをkeyPressEventで受け取る方法
#include <QtWidgets/QWidget>
#include <QtWidgets/QApplication>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QPlainTextEdit>
#include <QtWidgets/QLineEdit>
#include <QtCore/QDebug>
class MyWidget : public QWidget {
Q_OBJECT
public:
MyWidget(QWidget *parent = nullptr) : QWidget(parent) {
setFocusPolicy(Qt::WheelFocus);
}
protected:
void keyPressEvent(QKeyEvent *event) override {
qDebug() << event->key();
}
bool focusNextPrevChild(bool /*next*/) override {
return false;
}
};
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QWidget w;
auto h = new QHBoxLayout();
auto v = new QVBoxLayout();
auto target = new MyWidget();
target->setMinimumSize(100, 100);
h->addWidget(target);
h->addLayout(v);
v->addWidget(new QLineEdit());
v->addWidget(new QTextEdit());
v->addSpacerItem(new QSpacerItem(0, 100));
w.setLayout(h);
w.show();
return a.exec();
}
#include "grab_tabkey.moc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment