找回密码
 立即注册
收起左侧

QRect::contains 函数

1
回复
9255
查看
[复制链接]
累计签到:22 天
连续签到:1 天
来源: 2014-12-11 09:29:19 显示全部楼层 |阅读模式
1Qter豆
  1. #include "mywidget.h"
  2. #include<QtGui>


  3. MyWidget::MyWidget(QWidget *parent) :
  4.     QWidget(parent)
  5. {
  6.     setMouseTracking(true);
  7.     LineOfRect=false;
  8. }
  9. void MyWidget::paintEvent(QPaintEvent *event)
  10. {
  11.     QPainter painter(this);
  12.     QPen pen(Qt::red,4);
  13.     painter.setPen(pen);
  14.     QRect rect(30,30,200,200);
  15.     painter.drawRect(rect);
  16. }

  17. void MyWidget::setcursor()
  18. {
  19.     if(LineOfRect)
  20.     {
  21.         setCursor(Qt::OpenHandCursor);
  22.     }
  23.     else{
  24.         setCursor(Qt::ArrowCursor);
  25.     }
  26. }

  27. void MyWidget::mouseMoveEvent(QMouseEvent *event)
  28. {
  29.     QRect rect(30,30,200,200);
  30.     if(rect.contains(event->pos(),false))
  31.     {
  32.         LineOfRect=true;
  33.     }
  34.     else{
  35.         LineOfRect=false;
  36.     }
  37.   setcursor();
  38.     QString pos=QString("%1,%2").arg(event->pos().x()).arg(event->pos().y());
  39.     QToolTip::showText(event->globalPos(),pos,this);
  40. }
复制代码
QRect::contains 函数帮助文档中:
bool QRect::contains ( const QPoint & point, bool proper = false ) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false. If proper is true, this function only returns true if the given point is inside the rectangle (i.e., not on the edge).
1.当proper为false时,当point在rect的边缘上应该也返回ture,但是我在上面的代码中鼠标在rect边缘上的时候鼠标形状并没有改变,而是当鼠标在rect内部时cursor才改变形状,为什么?
2.如果if语句改成    if(rect.contains(event->pos(),false)&&(!(rect.contains(event->pos(),ture)))) 可以实现当鼠标在rect边框上cursor形状改变么?

最佳答案

查看完整内容

看了你的代码应该是对的。QRect rect(30,30,200,200)这个区域的坐标是x1=30 x2=229 y1=30 y2=229。
回复

使用道具 举报

累计签到:22 天
连续签到:1 天
2014-12-11 09:29:20 显示全部楼层
看了你的代码应该是对的。QRect rect(30,30,200,200)这个区域的坐标是x1=30 x2=229 y1=30 y2=229。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了