Chinaunix首页 | 论坛 | 博客
  • 博客访问: 424307
  • 博文数量: 71
  • 博客积分: 1525
  • 博客等级: 上尉
  • 技术积分: 605
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-05 17:28
文章分类

全部博文(71)

文章存档

2012年(21)

2011年(50)

分类: LINUX

2012-02-27 10:57:23

http://blog.sina.com.cn/s/blog_8b97b05e0100v6kk.html
1、QMouseEvent中的坐标
QMouseEvent中保存了两个坐标,一个是全局坐标,当然另外一个是局部坐标。
全局坐标(globalPos())即是桌面屏幕坐标(screen coordinates),这个跟windows下的调用getCursorPos函数得到的结果一致。
局部坐标(pos())即是相对当前active widget的坐标,左上角坐标为(0, 0)。

补充一个公式:
this->mapFromGlobal(this->cursor().pos()) = event.pos()

2、鼠标跟踪
在qt中,鼠标跟踪对应函数mouseMoveEvent。但是,默认情况下他并不能如期象你想象的那样响应鼠标的移动。此时,你只需在合适的位置调用一下函数setMouseTracking(true)即可。
If mouse tracking is switched off, mouse move events only occur if a mouse button is pressed while the mouse is being moved.
If mouse tracking is switched on, mouse move events occur even if no mouse button is pressed.
默认情况下,mouseMoveEvent响应你按下鼠标的某个键(拖动,但不局限于左键拖动)的鼠标移动。

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 ...
}
}
参考教材实例,增加了注释和新功能,方便大家交流学习
// mouseevent.cpp
#include "mouseevent.h"
MouseEvent::MouseEvent( ):


QMainWindow()
{
    setWindowTitle(tr("Get Mouse Event"));
    setCursor(Qt::CrossCursor); //设置鼠标为十字星
    labelStatus = new QLabel();
    labelStatus->setMinimumSize(500,20);
    labelStatus->setFrameShape(QFrame::WinPanel);//设置标签形状
    labelStatus->setFrameShadow(QFrame::Sunken); //设置标签阴影;
    labelStatus->setText(tr("Mouse Position:"));
    labelStatus -> setFixedWidth (100);
    labelMousePos = new QLabel();
    labelMousePos->setText(tr(""));
    labelMousePos -> setFixedWidth (100);
    statusBar()->addPermanentWidget(labelStatus);//给状态栏添加永久的部件
    statusBar()->addPermanentWidget(labelMousePos);
    this->setMouseTracking ( true);//自动跟踪鼠标(默认情况下,mouseMoveEvent响应你按下鼠标的某个键的鼠标移动。)
  }
  
MouseEvent::~MouseEvent(){


}


void MouseEvent::mouseMoveEvent ( QMouseEvent * e )//鼠标移动事件响应
   
    //1.获取局部鼠标位置
     labelMousePos ->setText("("+QString::number(e->x())+","+QString::number(e->y())+")");
    //2.获取全局鼠标位置
    QPoint coursePoint;
    coursePoint = QCursor::pos();//获取当前光标的位置
    labelMousePos ->setText("("+QString::number(coursePoint.x())+","+QString::number(coursePoint.y())+")");
}


void MouseEvent::mousePressEvent ( QMouseEvent * e )//鼠标单击事件响应
{
   QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
   if(e->button()==Qt::LeftButton){
      statusBar()->showMessage (tr("Mouse Left Button Pressed:")+str);//显示临时信息
   }
   else if(e->button()==Qt::RightButton){
                statusBar()->showMessage (tr("Mouse Right Button Pressed:")+str);
           }
          else if(e->button()==Qt::MidButton){
                       statusBar()->showMessage (tr("Mouse Middle Button Pressed:")+str);
         }
   }


void MouseEvent::mouseDoubleClickEvent( QMouseEvent * e )//鼠标双击事件响应
      QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
         if(e->button()==Qt::LeftButton){
               statusBar()->showMessage (tr("Mouse Left Button Double Clicked:")+str);
          }
         else if(e->button()==Qt::RightButton){
                       statusBar()->showMessage (tr("Mouse Right Button Double Clicked:")+str);
                }
               else if(e->button()==Qt::MidButton){
                             statusBar()->showMessage (tr("Mouse Middle Button Double Clicked:")+str);
                      }
}


void MouseEvent::mouseReleaseEvent ( QMouseEvent * e )//鼠标松开事件响应
{
        QString str="("+QString::number(e->x())+","+QString::number(e->y())+")";
         statusBar()->showMessage (tr("Mouser Released:")+str,3000);
}


-------------//  mouseevent.h
#ifndef MOUSEEVENT_H
#define MOUSEEVENT_H


#include
class MouseEvent : public QMainWindow
                   Q_OBJECT
            public:MouseEvent();
           ~MouseEvent();
private:
      QLabel *labelStatus;
      QLabel *labelMousePos;
      protected :
          void mouseMoveEvent ( QMouseEvent * e );
          void mousePressEvent ( QMouseEvent * e );
          void mouseReleaseEvent ( QMouseEvent * e );
          void mouseDoubleClickEvent( QMouseEvent * e );
};


#endif


//main.cpp


#include

#include "mouseevent.h"
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MouseEvent w;w.show();
    return app.exec();
}
阅读(47381) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~