1、键盘事件
#include <QKeyEvent>
void keyPressEvent(QKeyEvent *event);
//键盘事件
//按键会触发此函数
void Widget::keyPressEvent(QKeyEvent *event)
{
//按下A执行的动作
if(event->key() == Qt::Key_A)
{
qDebug() << this->lb1->pos().x() << this->lb1->pos().y() ;
this->lb1->move(this->lb1->pos().x() - 40, this->lb1->pos().y());
}
//按下D执行的动作
if(event->key() == Qt::Key_D)
{
qDebug() << this->lb1->pos().x() << this->lb1->pos().y() ;
this->lb1->move(this->lb1->pos().x() + 40, this->lb1->pos().y());
}
if(event->key() == Qt::Key_W)
{
qDebug() << this->lb1->pos().x() << this->lb1->pos().y() ;
this->lb1->move(this->lb1->pos().x(), this->lb1->pos().y()-40);
}
if(event->key() == Qt::Key_S)
{
qDebug() << this->lb1->pos().x() << this->lb1->pos().y() ;
this->lb1->move(this->lb1->pos().x(), this->lb1->pos().y()+40);
}
}
2、鼠标事件
#include <QMouseEvent>
void mouseMoveEvent(QMouseEvent *event);
//鼠标事件
//鼠标只要移动就会触发此函数
void Widget::mouseMoveEvent(QMouseEvent *event)
{
this->lb2->move(event->pos());
}