void Widget::keyPressEvent(QKeyEvent* event) { if(event->key() == Qt::Key_S) { QPoint point = ui->label->pos(); if(point.y() < this->height() - 10) { point.setY(point.y() + 5); } else { point.setY(0); } ui->label->move(point); } if(event->key() == Qt::Key_W) { QPoint point = ui->label->pos(); if(point.y() > 10) { point.setY(point.y() - 5); } else { point.setY(this->height() - 10); } ui->label->move(point); }模仿以上代码写出"A","D"键实现左右移动
时间: 2024-02-14 08:08:33 浏览: 69
ScreenSnaper.rar_QT 截屏_Qt右键菜单_qt QQ截图_qt 右键菜单
void Widget::keyPressEvent(QKeyEvent* event) {
if(event->key() == Qt::Key_S)
{
QPoint point = ui->label->pos();
if(point.y() < this->height() - 10)
{
point.setY(point.y() + 5);
}
else
{
point.setY(0);
}
ui->label->move(point);
}
if(event->key() == Qt::Key_W)
{
QPoint point = ui->label->pos();
if(point.y() > 10)
{
point.setY(point.y() - 5);
}
else
{
point.setY(this->height() - 10);
}
ui->label->move(point);
}
if(event->key() == Qt::Key_A)
{
QPoint point = ui->label->pos();
if(point.x() > 10)
{
point.setX(point.x() - 5);
}
else
{
point.setX(this->width() - 10);
}
ui->label->move(point);
}
if(event->key() == Qt::Key_D)
{
QPoint point = ui->label->pos();
if(point.x() < this->width() - 10)
{
point.setX(point.x() + 5);
}
else
{
point.setX(0);
}
ui->label->move(point);
}
}
阅读全文