当前位置: 代码迷 >> QT开发 >> boundingRect调用有关问题
  详细解决方案

boundingRect调用有关问题

热度:260   发布时间:2016-04-25 03:02:45.0
boundingRect调用问题
想在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改变窗口大小或者按键盘上的某些按键也会让没刷掉的边消失。这是什么原因?怎么解决?
------解决思路----------------------
多看看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).