• 2264阅读
  • 0回复

[提问]鼠标移动时间获取的pos和控件的不一样 [复制链接]

上一主题 下一主题
离线foxgod
 

只看楼主 倒序阅读 楼主  发表于: 2018-06-25
请教一个问题,我创建一个窗口的工程,然后在这个界面上在拖入一个widget控件,大小为1000*900,

设置了鼠标跟踪。

判断目前的鼠标移动的点是否在拖入的widget里面,代码如下

  1. setMouseTracking(true);
  2. setWindowFlags(Qt::FramelessWindowHint);
  3. 判断鼠标位置
  4. int  CaputerRect(QRect Rect,int x,int y)
  5. {
  6.     if(qAbs(x-Rect.topLeft().x())<2)           //左
  7.     {
  8.         if(qAbs(y-Rect.topLeft().y())<2)       //上
  9.         {
  10.             m_cursorPos=1;
  11.         }
  12.         else if(qAbs(y-Rect.bottomRight().y())<2)   //下
  13.         {
  14.             m_cursorPos=2;
  15.         }
  16.         else if( (y>=Rect.topLeft().y()+2) && (y<=Rect.bottomRight().y()-2) )  //上下之间
  17.         {
  18.             m_cursorPos=3;
  19.         }
  20.         else
  21.         {
  22.             m_cursorPos=0;
  23.         }
  24.     }
  25.     else if(qAbs(x-Rect.bottomRight().x())<2)           //右
  26.     {
  27.         if(qAbs(y-Rect.topLeft().y())<2)                //上
  28.         {
  29.             m_cursorPos=4;
  30.         }
  31.         else if(qAbs(y-Rect.bottomRight().y())<2)       //下
  32.         {
  33.             m_cursorPos=5;
  34.         }
  35.         else if( (y>=Rect.topLeft().y()+2) && (y<=Rect.bottomRight().y()-2) )  //上下之间
  36.         {
  37.             m_cursorPos=6;
  38.         }
  39.         else
  40.         {
  41.             m_cursorPos=0;
  42.         }
  43.     }
  44.     else if(qAbs(y-Rect.topLeft().y())<= 2)
  45.     {
  46.         if( (Rect.topLeft().x()+2) < x && x <(Rect.bottomRight().x()-2) )//中间
  47.          {
  48.             m_cursorPos=7;
  49.          }
  50.          else
  51.          {
  52.             m_cursorPos=0;
  53.          }
  54.     }
  55.     else if( qAbs(y-Rect.bottomRight().y())<= 2 )//下
  56.     {
  57.        qDebug()<<"44444444444";
  58.        if( Rect.topLeft().x()+2 < x && x <Rect.bottomRight().x()-2 )//中间
  59.         {
  60.           // qDebug()<<"youshang";
  61.            m_cursorPos=8;
  62.         }
  63.         else
  64.         {
  65.           // qDebug()<<"wuxiao"<<y<<Rect.y();
  66.            m_cursorPos=0;
  67.         }
  68.     }
  69.     else if((x>Rect.topLeft().x()+2) && (x<Rect.bottomRight().x()-2))          //左右之间
  70.     {
  71.         qDebug()<<"2222222222222222222"<<Rect.topLeft().y()<<Rect.bottomRight().y()<<y;
  72.         if( (y>Rect.topLeft().y()+2) && (y<=Rect.bottomRight().y()-2) )  //上下之间
  73.         {
  74.             qDebug()<<"5555555555555555"<<x<<y<<Rect.topLeft().y()<<Rect.bottomRight().y();
  75.             m_cursorPos=9;
  76.         }
  77.         else
  78.         {
  79.             qDebug()<<"666666666666666"<<x<<y<<Rect.topLeft().y()<<Rect.bottomRight().y();
  80.             m_cursorPos=0;
  81.         }
  82.     }
  83.     else
  84.     {
  85.         m_cursorPos=0;
  86.     }
  87.     return m_cursorPos;
  88. }
目前只有判断鼠标位置在这个ui->capture里面的位置的时候,总是判断出错,不知道什么原因,其他位置都显示正确
出错的代码,在上面的代码行中的71-83行,每次鼠标移动的event->pos()的y总是大于ui->capture这个窗口的bottomRight().y()。


快速回复
限100 字节
 
上一个 下一个