boundingRect调用问题

Oskar_Sun 2015-01-19 10:48:03
想在boundingRect内画一图形,然后鼠标拖动改变大小位置等。需要boungdingRect随着图形的大小改变而改变。
#include "rectitem.h"
#include<QPainter>
#include<QGraphicsSceneMouseEvent>
#include<QDebug>

RectItem::RectItem()
{
rect.setRect(-50,-50,100,100);
}

QRectF RectItem::boundingRect()const
{
qreal penWidth=1;
return QRectF(rect.adjusted(-penWidth,-penWidth,penWidth,penWidth));
}
void RectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRect(rect);
}
void RectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
rect.setBottom(event->pos().y());
qDebug()<<"1";
update();
}

其中boundingRect想通过adjusted函数随着我的rect的改变而改变,但实际中出现如下图问题:

问题描述:当鼠标在rect内点击的时候rect确实改变了大小,但原来的rect底边没有刷新掉。但是拖动widget改变窗口大小或者按键盘上的某些按键也会让没刷掉的边消失。这是什么原因?怎么解决?
...全文
585 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Oskar_Sun 2015-01-19
  • 打赏
  • 举报
回复
引用 2 楼 dbzhang800 的回复:
多看看Manual即可解决这个问题 QRectF QGraphicsItem::boundingRect() const [pure virtual] If you want to change the item's bounding rectangle, you must first call prepareGeometryChange(). This notifies the scene of the imminent change, so that it can update its item geometry index; otherwise, the scene will be unaware of the item's new geometry, and the results are undefined (typically, rendering artifacts are left within the view).
问题解决了,低级的错误,让您见笑了。 不过还有一个问题是,当我把矩形rect右边的边线拖动到左边的时候(就是类似于把矩形翻转,让右边变成左边,左边变成右边)又会出现这样无法更新的问题。这是因为什么呢?
dbzhang800 2015-01-19
  • 打赏
  • 举报
回复
多看看Manual即可解决这个问题 QRectF QGraphicsItem::boundingRect() const [pure virtual] If you want to change the item's bounding rectangle, you must first call prepareGeometryChange(). This notifies the scene of the imminent change, so that it can update its item geometry index; otherwise, the scene will be unaware of the item's new geometry, and the results are undefined (typically, rendering artifacts are left within the view).
Oskar_Sun 2015-01-19
  • 打赏
  • 举报
回复
个人分析应该是boundingRect在rect改变后没有及时调用返回新的QRectF,但是boundingRect不是QGraphicsView调用的么,那鼠标的点击或松开就应该调用到boundingRect啊。 还是说boundingRect已经返回新的边框,但Widget中没有刷新?

16,817

社区成员

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

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