|
1Qter豆
- #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形状改变么?
|
最佳答案
查看完整内容
看了你的代码应该是对的。QRect rect(30,30,200,200)这个区域的坐标是x1=30 x2=229 y1=30 y2=229。
|