QRect::contains函数问题

Oskar_Sun 2014-12-10 10:21:59
两个问题
#include "mywidget.h"
#include<QtGui>


MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
setMouseTracking(true);
LineOfRect=false;
}
void MyWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen(Qt::red,4);
painter.setPen(pen);
QRect rect(30,30,200,200);
painter.drawRect(rect);
}

void MyWidget::setcursor()
{
if(LineOfRect)
{
setCursor(Qt::OpenHandCursor);
}
else{
setCursor(Qt::ArrowCursor);
}
}

void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
QRect rect(30,30,200,200);
if(rect.contains(event->pos(),false))
{
LineOfRect=true;
}
else{
LineOfRect=false;
}
setcursor();
QString pos=QString("%1,%2").arg(event->pos().x()).arg(event->pos().y());
QToolTip::showText(event->globalPos(),pos,this);
}



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形状改变么?
...全文
924 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
syaowen5835889 2014-12-11
  • 打赏
  • 举报
回复

    if(rect.contains(event->pos(),false)&!(rect.contains(event->pos(),true)))
就可以了。当参数为false时,鼠标在边框和边框内返回为真

16,818

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧