Qt:调整鼠标移动事件上的png图像大小从矩形的左侧或顶部调整大小时不起作用

时间:2014-05-01 12:53:38

标签: qt qgraphicsitem qgraphicsscene

我有一个png图像,它作为QGraphicsPixmapItem显示在图形场景中。  图像是矩形的。每次在矩形的任何一侧(即从左,右,顶部或底部)拖动鼠标移动事件时,我都需要调整png图像的大小。

目前我只能在从右侧和底侧拖动时调整图像大小。从左侧和顶侧拖动时,图像大小调整失败。请让我知道下面代码中的错误。这里调整大小是基于最大尺寸的原始图像文件,但是场景本身上显示的初始图像是源图像的缩小版本。

部分代码发布在下面,我没有显示hoverEnterEvent()实现:

  PersonSizeGraphicsItem::PersonSizeGraphicsItem(const QPixmap &pixmap, QGraphicsScene *scene)
        :QGraphicsPixmapItem(pixmap, 0, scene)
    {
        this->setFlag(QGraphicsItem::ItemIsMovable, true);
        this->setFlag(QGraphicsItem::ItemIsSelectable, true);
        this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
        this->setFlag(QGraphicsItem::ItemIsFocusable, true);
        this->setFocus(Qt::MouseFocusReason);
        this->setAcceptHoverEvents(true);
        //this->setScale(0.5);

        rect_left_condition = false;
        rect_right_condition = false;
        rect_top_condition = false;
        rect_bottom_condition = false;
        rect_resize_occurred = false;
        image_rect = QRect();
        image_rect =  this->pixmap().toImage().rect();
    }

    PersonSizeGraphicsItem::~PersonSizeGraphicsItem()
    {

    }

    int PersonSizeGraphicsItem::type() const
    {
        return item_type;
    }

    void PersonSizeGraphicsItem::setSourceImage(const QImage& source_image)
    {
        this->source_image =  source_image;
    } 

    void PersonSizeGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        const QPointF event_pos = event->pos();
        const QPointF event_scene_pos = event->scenePos();

            QPoint current_top_left = image_rect.topLeft();
        QPoint current_bottom_right = image_rect.bottomRight();

        if((event->scenePos().x() > this->scene()->width()) || (event->scenePos().y() > this->scene()->height())
            || (event->scenePos().x() < 0) || (event->scenePos().y() < 0) )
        {
            return;
        }

        if( this->cursor().shape() == Qt::SizeHorCursor )
        {
            if(rect_right_condition)
            {

                image_rect = QRect( current_top_left, QPoint( event->pos().x(), current_bottom_right.y()) );


                rect_resize_occurred = true;
            }

            if(rect_left_condition)
            {
                image_rect = QRect( QPoint(event_pos.x(), 0), current_bottom_right );

                QPoint new_top_left = image_rect.topLeft();
                QPointF mapped_topLeft = mapToParent(QPointF(new_top_left.x(),new_top_left.y()));
                this->setPos(mapped_topLeft);
                rect_resize_occurred = true;

                //qDebug() << "new rectangle top left:" << this->pixmap().rect().topLeft();
            }

        }

        if( this->cursor().shape() == Qt::SizeVerCursor )
        {
            if(rect_bottom_condition)
            {
                image_rect = QRect(current_top_left, QPoint(current_bottom_right.x(), event->pos().y()));


                rect_resize_occurred = true;
            }

            if(rect_top_condition)
            {
                image_rect = QRect(QPoint(0, event_pos.y()), current_bottom_right);

                QPoint new_top_left = image_rect.topLeft();
                QPointF mapped_topLeft = mapToParent(QPointF(new_top_left.x(),new_top_left.y()));
                this->setPos(mapped_topLeft);

                qDebug() << "new rectangle top left###:" << this->pixmap().rect().topLeft();
                rect_resize_occurred = true;

            }
        }

        this->update();

    }


    void PersonSizeGraphicsItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        painter->drawImage(image_rect, source_image);
    }



     QRectF PersonSizeGraphicsItem:: boundingRect() const
     {
        qreal extra = 0.0;

          QRect rect = image_rect;
          return QRectF(rect.topLeft(), QSizeF(rect.width(), rect.height()))
            .normalized()
            .adjusted(-extra, -extra, extra, extra);
     }

1 个答案:

答案 0 :(得分:0)

我尝试了以下代码来调整矩形的大小并且它可以正常工作。在早期版本的mouseMoveEvent()中,我总是试图在每次调整大小后将topLeft坐标设置为(0,0)。    在新版本中,我让topLeft坐标保持原样,并且在每次调整大小后都不必费心将topLeft设置为(0,0)。完成调整大小后,topLeft永远不会在本地坐标中设置为原点。

void PersonSizeGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    const QPointF event_pos = event->pos();
    const QPointF event_scene_pos = event->scenePos();

    QPoint current_top_left = image_rect.topLeft();
    QPoint current_bottom_right = image_rect.bottomRight();

    if((event->scenePos().x() > this->scene()->width()) || (event->scenePos().y() > this->scene()->height())
        || (event->scenePos().x() < 0) || (event->scenePos().y() < 0) )
    {
        return;
    }

    if( this->cursor().shape() == Qt::SizeHorCursor )
    {
        if(rect_right_condition)
        {

            image_rect = QRect( current_top_left, QPoint( event->pos().x(), current_bottom_right.y()) );

            if( image_rect.width() <=8 || image_rect.height() <=24 )
            {
                return;
            }

            rect_resize_occurred = true;
        }

        if(rect_left_condition)
        {
            //image_rect = QRect( QPoint(event_pos.x(), 0), current_bottom_right );

            image_rect = QRect( QPoint(event_pos.x(), current_top_left.y()), current_bottom_right );

            if( image_rect.width() <=8 || image_rect.height() <=24 )
            {
                return;
            }
            rect_resize_occurred = true;

        /*  QPoint new_top_left = image_rect.topLeft();
            QPointF mapped_topLeft = mapToParent(QPointF(new_top_left.x(),new_top_left.y()));
            this->setPos(mapped_topLeft); */

            //qDebug() << "new rectangle top left:" << this->pixmap().rect().topLeft();
        }

    }

    if( this->cursor().shape() == Qt::SizeVerCursor )
    {
        if(rect_bottom_condition)
        {
            image_rect = QRect(current_top_left, QPoint(current_bottom_right.x(), event->pos().y()));

            if( image_rect.width() <=8 || image_rect.height() <=24 )
            {
                return;
            }

            rect_resize_occurred = true;
        }

        if(rect_top_condition)
        {
            //image_rect = QRect(QPoint(0, event_pos.y()), current_bottom_right);

            image_rect = QRect(QPoint(current_top_left.x(), event_pos.y()), current_bottom_right);

            if( image_rect.width() <=8 || image_rect.height() <=24 )
            {
                return;
            }

        /*  QPoint new_top_left = image_rect.topLeft();
            QPointF mapped_topLeft = mapToParent(QPointF(new_top_left.x(),new_top_left.y()));
            this->setPos(mapped_topLeft); */

            rect_resize_occurred = true;

        }
    }

    this->update();

}