Created
April 28, 2020 14:44
-
-
Save Atsushi4/86cd97fcfebf46a1208c60f33c232591 to your computer and use it in GitHub Desktop.
[Qt] TabキーをkeyPressEventで受け取る方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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