Home >Web Front-end >JS Tutorial >QGraphicsItem scaling

QGraphicsItem scaling

一个新手
一个新手Original
2017-09-22 10:19:242123browse


QGraphicsItem scaling

QgarphicsItem is an item in the Qt view system. QGraphicsItem itself does not support mouse dragging to zoom. This article describes how to modify the size of the item by changing mouse events. (The Qt version used in this article is Qt4.8)

The function implemented by the code below is: hold down shift and drag with the mouse to change the size of the Box.

Define the class Box

class Box:public QGraphicsItem
{
    Q_DECLARE_TR_FUNCTIONS(Box)public:    
    Box();
    ...protected:    
    void mousePressEvent(QGraphicsSceneMouseEvent *event);    
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);    
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
};
Box::Box()
{
    setFlags(QGraphicsItem::ItemIsSelectable|
             QGraphicsItem::ItemIsMovable|
             QGraphicsItem::ItemSendsGeometryChanges|
             QGraphicsItem::ItemIsFocusable);       //接受键盘事件
    mBoundingRect = QRectF(0,0,100,100);
    mBoundingRect.translate(-mBoundingRect.center());
}

The above two pieces of code are the definition of the Box class and the implementation of the constructor. The most important thing is the overloading of the three mouse functions, and making Box available in setFlag Accept keyboard events.

Overload mousePressEvent

void Box::mousePressEvent(QGraphicsSceneMouseEvent *event)
{    if(event->modifiers()&Qt::ShiftModifier)
    {
        resizing = true;             //resizing变量在鼠标点击时变为true                                                    //在放开时变为false
        setCursor(Qt::SizeAllCursor);//鼠标样式变为十字
    }    else
        QGraphicsItem::mousePressEvent(event);
}

Overload mouseMoveEvent

void Box::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{    if(resizing)
    {
        QRectF rect(mBoundingRect);        
        if(event->pos().x()<rect.x())
            rect.setBottomLeft(event->pos());        else
            rect.setBottomRight(event->pos());
        mBoundingRect=rect;
        mBoundingRect.translate(-mBoundingRect.center());
        scene()->update();
    }    else
        QGraphicsItem::mouseMoveEvent(event);
}

Here, simply update the lower left and upper right corners of the Box to match the mouse position. A better approach would be to handle the x and y coordinates separately.

Overload mouseReleaseEvent

void Box::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{    if(resizing)
    {
        resizing = false;
        setCursor(Qt::ArrowCursor);
    }    else
        QGraphicsItem::mouseReleaseEvent(event);
}

When the user releases the mouse during the size change process, the resizing is changed to true and the mouse style is changed back to the arrow.

The above is the detailed content of QGraphicsItem scaling. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn