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/Lab2/lab2-3-5_KeyEvent/mainwindow.cpp

33 lines
1.1 KiB
C++
Raw Normal View History

2022-04-19 07:56:42 +00:00
#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);
2022-04-19 10:03:16 +00:00
} else if (e->key() == Qt::Key_Down) {
ui->l->move(ui->l->pos().x(), ui->l->pos().y() - 20);
2022-04-19 07:56:42 +00:00
} else if (e->key() == Qt::Key_Left) {
ui->l->move(ui->l->pos().x() + 20, ui->l->pos().y());
2022-04-19 10:03:16 +00:00
} else if (e->key() == Qt::Key_Right) {
ui->l->move(ui->l->pos().x() - 20, ui->l->pos().y());
2022-04-19 07:56:42 +00:00
}
}