脚本宝典收集整理的这篇文章主要介绍了QT中的鼠标移动事件,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
关注
QMouseEvent类其中还有Press事件、release事件、move事件.......为了练习move事件,做了一个按钮跟随鼠标小程序首先我们可以查阅文档:mouSEMoveEvent事件,[virtual PRotected] void QWindow::mouseMoveEvent(QMouseEvent *ev);这是一个虚函数,可以用来重载的我们就在自己的函数中
重写虚函数:
pBT.move(event->x(),event->y()); //设置按钮跟随坐标
在按钮创建需要在类中进行.建立成员函数的方式创建
设置PushButton
//直接打开鼠标事件
setMouseTracking(true);-------------------------------------------------------------------------------------------------------
#include "widget.h"
#include "ui_widget.h"
#include <QMouseEvent>
#include <QDebug>
int i = 0;
int j =0 ;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->SETUPUi(this);
resize(800,600);
//直接打开鼠标事件
setMouseTracking(true);
pbt.setParent(this);
pbt.setText("欢迎");
pbt.move(350,300);
pbt.resize(80,50);
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
QString str = QString("鼠标移动了 x = %1 y =%2 ").arg(event->x()).arg(event->y());
qDebug() <<str;
//按钮移动
pbt.move(event->x(),event->y());
}
Widget::~Widget()
{
delete ui;
}----------------
还有
event->button() ==Qt::LeftButton; //左键;判断按键
event->buttons() &Qt::LeftButton; //判断联合按钮
以上是脚本宝典为你收集整理的QT中的鼠标移动事件全部内容,希望文章能够帮你解决QT中的鼠标移动事件所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。