繁体   English   中英

Qt:用鼠标将QGraphicsItem(boundingRect())调整为QGraphicsScene

[英]Qt: resize a QGraphicsItem (boundingRect()) into a QGraphicsScene with the mouse

我想使用鼠标调整QGraphicsItemboundingRect()的大小。

为此,我找到了这个主题 因此,按照该主题的思想,我设法使其在我的boundingRect()的右侧,底部和底部。

但是,由于我的商品的位置是由boundingRect()左上角定义的,因此修改我商品的尺寸以移动链接到该位置的边缘会更加复杂。

我尝试使用左上方,但右下方也在移动。 我想固定右下角仅更改大小,而不更改整个位置。

这是我的代码的一部分:

void Myclass::mouseMoveEvent(QGraphicsSceneMouseEvent *event) 
{
    if(_resizemode) //if bottom selected
    {
        prepareGeometryChange();
        _h = event->pos().y();
    }

    if(_resizemode2) //if bottom right selected
    {
        prepareGeometryChange();
        _h = event->pos().y();
        _w = event->pos().x();

    }

    if(_resizemode3) //if right selected
    {
        prepareGeometryChange();
        _w = event->pos().x();
    }

    if(_resizemode4) //if top left selected HERE IS MY ISSUE
    {
        prepareGeometryChange();
        setPos(pos().x()+ event->pos().x(), pos().y() + event->pos().y());
        _h = _h - event->pos().y();
        _w = _w - event->pos().x();

    }

    if(!_resizemode&&!_resizemode2&&!_resizemode3&&!_resizemode4)
    {
    update();
    QGraphicsItem::mouseMoveEvent(event);
    }
}

_h_w或此处实现的高度和宽度:

QRectF Aabb::boundingRect() const
{
    return QRectF( 0, 0, _w, _h);
}

有人知道如何调整QGraphicsItem大小,请用鼠标选择boundingRect()每个角?

我的商品的位置是在boundingRect()的左上方定义的

在我看来,最简单的方法是更改​​此位置,以便通过项目的中心定义位置。 为此,请将原始边界rect定义为左上(-w / 2,-h / 2)和右下(w / 2,h / 2),或者如果您要返回boundingRect函数QRectF(- w / 2,-h / 2,w,h)表示(x,y,w,h)。

然后,在拐角处创建子“工具”项,该子项定义了可以单击并拖动原始项拐角的区域。 如果选择了该项目,则可能只希望它们出现。

在子项的moveEvent中,更新主项的边界矩形,以使其随子项一起移动。 释放鼠标后,重新计算主要项目的boundingRect,使其中心仍在(0,0)。

如果您将boundingRect的左上角和右下角映射并转换为场景坐标,然后使用宽度和高度,将项的局部坐标重新设置为使其保持(-w / 2,则可以很容易地做到这一点。 ,-h / 2,w,h);

我解决了我的问题。 到目前为止,这是我对mousePressEvent所做的mousePressEvent (定义了诸如QRectF(-w/2, -h/2, w, h)类的原始boundingRect函数):

void Myclass::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if(isSelected())
    {

    qDebug()<<"isseldeted";
    qreal adjust = 8;
    if((event->pos().x() > boundingRect().left() + adjust) && (event->pos().x() < boundingRect().right() - adjust) && (event->pos().y() < boundingRect().bottom() + adjust) && (event->pos().y() > boundingRect().bottom() - adjust) )
    {
        _adj = event->pos().y();
        _resizemode = true;
        qDebug()<<"bottom";
    }
    if((event->pos().x() > boundingRect().right() - adjust) && (event->pos().x() < boundingRect().right() + adjust) && (event->pos().y() < boundingRect().bottom() + adjust) && (event->pos().y() > boundingRect().bottom() - adjust))
    {
        _adj = event->pos().y();
        _adj2 = event->pos().x();
        _resizemode2 = true;
        qDebug()<<"bottom right";

    }

    if((event->pos().x() > boundingRect().right() - adjust) && (event->pos().x() < boundingRect().right() + adjust) && (event->pos().y() < boundingRect().bottom() - adjust) && (event->pos().y() > boundingRect().top() + adjust))
    {
        _adj2 = event->pos().x();
        _resizemode3 = true;
        qDebug()<<"right";

    }

    if((event->pos().x() > boundingRect().left() - adjust) && (event->pos().x() < boundingRect().left() + adjust) && (event->pos().y() > boundingRect().top() - adjust) && (event->pos().y() < boundingRect().top() + adjust))
    {
        _adj = event->pos().y();
        _adj2 = event->pos().x();
        _resizemode4 = true;
        qDebug()<<"top left";

    }

    if((event->pos().x() > boundingRect().left() - adjust) && (event->pos().x() < boundingRect().left() + adjust) && (event->pos().y() < boundingRect().bottom() - adjust) && (event->pos().y() > boundingRect().top() + adjust))
    {
        _adj2 = event->pos().x();
        _resizemode5 = true;
        qDebug()<<"left";

    }

    if((event->pos().x() > boundingRect().left() - adjust) && (event->pos().x() < boundingRect().left() + adjust) && (event->pos().y() < boundingRect().bottom() + adjust) && (event->pos().y() > boundingRect().bottom() - adjust))
    {
        _adj = event->pos().y();
        _adj2 = event->pos().x();
        _resizemode6 = true;
        qDebug()<<"bottom left";

    }

    if((event->pos().x() > boundingRect().left() + adjust) && (event->pos().x() < boundingRect().right() - adjust) && (event->pos().y() < boundingRect().top() + adjust) && (event->pos().y() > boundingRect().top() - adjust))
    {
        _adj = event->pos().y();
        _resizemode7 = true;
        qDebug()<<"top";

    }

    if((event->pos().x() < boundingRect().right() + adjust) && (event->pos().x() > boundingRect().right() - adjust) && (event->pos().y() < boundingRect().top() + adjust) && (event->pos().y() > boundingRect().top() - adjust))
    {
        _adj = event->pos().y();
        _adj2 = event->pos().x();
        _resizemode8 = true;
        qDebug()<<"top right";

    }
    }
update();
QGraphicsItem::mousePressEvent(event);
}

对于mouseReleaseEvent

void Myclass::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(_resizemode)
{
    prepareGeometryChange();
    _h += event->pos().y() - _adj; //good
    setPos(pos().x(), pos().y() + (event->pos().y() - _adj)/2 );//good

    _resizemode = false;
}

if(_resizemode2)
{
    _h += event->pos().y() - _adj; //good
    _w += event->pos().x() - _adj2;
    setPos(pos().x() + (event->pos().x() - _adj2)/2, pos().y() + (event->pos().y() - _adj)/2 );//good

    _resizemode2 = false;
}

if(_resizemode3)
{
    _w += event->pos().x() - _adj2;
    setPos(pos().x() + (event->pos().x() - _adj2)/2, pos().y());//good

    _resizemode3 = false;
}

if(_resizemode4)
{
    _h -= event->pos().y() - _adj; //good
    _w -= event->pos().x() - _adj2;
    setPos(pos().x() + (event->pos().x() - _adj2)/2, pos().y() + (event->pos().y() - _adj)/2 );//good

    _resizemode4 = false;
}

if(_resizemode5)
{
    _w -= event->pos().x() - _adj2;
    setPos(pos().x() + (event->pos().x() - _adj2)/2, pos().y());//good

    _resizemode5 = false;
}

if(_resizemode6)
{
    _h += event->pos().y() - _adj;
    _w -= event->pos().x() - _adj2;
    setPos(pos().x() + (event->pos().x() - _adj2)/2, pos().y() + (event->pos().y() - _adj)/2);//good

    _resizemode6= false;
}

if(_resizemode7)
{
    _h -= event->pos().y() - _adj;
    setPos(pos().x(), pos().y() + (event->pos().y() - _adj)/2);//good

    _resizemode7= false;
}

if(_resizemode8)
{
    _h -= event->pos().y() - _adj;
    _w += event->pos().x() - _adj2;
    setPos(pos().x() + (event->pos().x() - _adj2)/2, pos().y() + (event->pos().y() - _adj)/2);//good

    _resizemode8= false;
}

update();
QGraphicsItem::mouseReleaseEvent(event);
}

mouseMoveEvent也需要实现:

void Aabb::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{   if(!_resizemode&&!_resizemode2&&!_resizemode3&&!_resizemode4&&!_resizemode5&&!_resizemode6&&!_resizemode7&&!_resizemode8)
    {
    update();
    QGraphicsItem::mouseMoveEvent(event);
    }
}

问题是我们看不到移动的大小:释放鼠标时,我们会看到调整大小的项目。

有时,该物品的位置没有完全调整,我不知道为什么。

所以这是可行的,但我相信可以改善。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM