事件 mousePressEvent、paintEvent、closeEvent、keyPressEvent
鼠标样式的设置
按WSAD通过keyPressEvent事件移动按钮
通过事件mousePressEvent获取鼠标位置的相对位置,绝对位置
cusor 鼠标样式设置成十字星
.h
#ifndef DEFAULTHANDLEREXAMPLE_H
#define DEFAULTHANDLEREXAMPLE_H
#include <QWidget>
#include <QPaintEvent>
namespace Ui {
class DefaultHandlerExample;
}
class DefaultHandlerExample : public QWidget
{
Q_OBJECT
protected:
void paintEvent(QPaintEvent *event);
void closeEvent(QCloseEvent *event);
void keyPressEvent(QKeyEvent *event);
void showEvent(QShowEvent *event);
void hideEvent(QHideEvent *event);
void mousePressEvent(QMouseEvent *event);
public:
explicit DefaultHandlerExample(QWidget *parent = nullptr);
~DefaultHandlerExample();
private:
Ui::DefaultHandlerExample *ui;
};
#endif // DEFAULTHANDLEREXAMPLE_H
.cpp
#include "defaulthandlerexample.h"
#include "ui_defaulthandlerexample.h"
#include <QMessageBox>
#include <QPainter>
DefaultHandlerExample::DefaultHandlerExample(QWidget *parent)
: QWidget(parent)
, ui(new Ui::DefaultHandlerExample)
{
ui->setupUi(this);
this->setMouseTracking(false);//只有按下鼠标才会跟踪
}
DefaultHandlerExample::~DefaultHandlerExample()
{
delete ui;
}
void DefaultHandlerExample::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.drawPixmap(0,0,this->width(),this->height(),
QPixmap(":/toolbar/images/background.jpg"));
}
void DefaultHandlerExample::closeEvent(QCloseEvent *event)
{
QString dlgTitle ="提示";
QString strInfo ="确定要退出吗?";
QMessageBox::StandardButton result=
QMessageBox::question(this,dlgTitle,strInfo,QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
if(result== QMessageBox::Yes)
event->accept();//窗口确认关闭
else
event->ignore();//忽略
}
void DefaultHandlerExample::keyPressEvent(QKeyEvent *event)
{
QPoint pt = ui->btnMove->pos();
if((event->key()== Qt::Key_A)|| (event->key()==Qt::Key_Left))
ui->btnMove->move(pt.x()-20,pt.y());
if((event->key()== Qt::Key_D)|| (event->key()==Qt::Key_Right))
ui->btnMove->move(pt.x()+20,pt.y());
if((event->key()== Qt::Key_W)|| (event->key()==Qt::Key_Up))
ui->btnMove->move(pt.x(),pt.y()-20);
if((event->key()== Qt::Key_S)|| (event->key()==Qt::Key_Down))
ui->btnMove->move(pt.x(),pt.y()+20);
// this->setWindowTitle(event->ke);
}
void DefaultHandlerExample::showEvent(QShowEvent *event)
{
Q_UNUSED(event);
ui->label->setText("showEvent函数被触发");
}
void DefaultHandlerExample::hideEvent(QHideEvent *event)
{
Q_UNUSED(event);
ui->label->setText("hideEvent函数被触发");
}
void DefaultHandlerExample::mousePressEvent(QMouseEvent *event)
{
if(event->button()==Qt::LeftButton)
{
QPoint pt = event->pos(); //窗口上的相对坐标
QPointF relaPt = event->position(); //相对坐标
QPointF winPt = event->scenePosition();//相对坐标
QPointF globalPt = event->globalPosition();//屏幕上的绝对坐标
QString str = QString::asprintf("pos()=(%d,%d)",pt.x(),pt.y());
str+= QString::asprintf("\nposition()=%.0f,%.0f",relaPt.x(),relaPt.y());
str+= QString::asprintf("\nscenePosition()=%.0f,%.0f",winPt.x(),winPt.y());
str+= QString::asprintf("\nglobalPosition()=%.0f,%.0f",globalPt.x(),globalPt.y());
ui->label->setText(str);
ui->label->adjustSize();
ui->label->move(event->pos());//标签移动到鼠标光标处
}
QWidget::mousePressEvent(event);
}
效果