QMouseEvent 鼠标事件

来源:互联网 发布:淘宝店铺品牌授权书 编辑:程序博客网 时间:2024/05/17 07:27
1、QMouseEvent中的坐标[喝小酒的网摘]http://blog.hehehehehe.cn/a/17083.htm
QMouseEvent中保存了两个坐标,一个是全局坐标,当然另外一个是局部坐标。
全局坐标(globalPos())即是桌面屏幕坐标(screen coordinates),这个跟windows下的调用getCursorPos函数得到的结果一致。
局部坐标(pos())即是相对当前active widget的坐标,左上角坐标为(0, 0)。

补充一个公式:

this->mapFromGlobal(this->cursor().pos()) = event.pos()


event.pos()返回的是局部坐标,该值等于 mapFromGlobal( event.globalPos()  );


3、鼠标左键拖动和左键点击的判断
鼠标左键点击很容易判断,一般就是在重写mousePressEvent函数,示例如下:
void XXXWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
       // todo ...
}

}


左键拖动的判断一般放在mouseMoveEvent函数中,但是你不能向上例一样来判断,因为该函数的event参数总是返回Qt::NoButton。你可以这样做:
void XXXWidget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() & Qt::LeftButton)
{
       // todo ...
}

}

0 0
原创粉丝点击