This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/SoftwareDesign/Code/2-3-5/lab2-3-5_KeyEvent/mainwindow.cpp
2022-04-19 21:45:49 +08:00

32 lines
1.1 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
}
MainWindow::~MainWindow() { delete ui; }
void MainWindow::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Up) {
ui->l->move(ui->l->pos().x(), ui->l->pos().y() - 20);
} else if (event->key() == Qt::Key_Down) {
ui->l->move(ui->l->pos().x(), ui->l->pos().y() + 20);
} else if (event->key() == Qt::Key_Left) {
ui->l->move(ui->l->pos().x() - 20, ui->l->pos().y());
} else if (event->key() == Qt::Key_Right) {
ui->l->move(ui->l->pos().x() + 20, ui->l->pos().y());
}
}
void MainWindow::keyReleaseEvent(QKeyEvent *e) {
if (e->key() == Qt::Key_Up) {
ui->l->move(ui->l->pos().x(), ui->l->pos().y() - 20);
} else if (e->key() == Qt::Key_Down) {
ui->l->move(ui->l->pos().x(), ui->l->pos().y() + 20);
} else if (e->key() == Qt::Key_Left) {
ui->l->move(ui->l->pos().x() - 20, ui->l->pos().y());
} else if (e->key() == Qt::Key_Right) {
ui->l->move(ui->l->pos().x() + 20, ui->l->pos().y());
}
}