Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Disable QBrush rotating inside QGraphicsItem.
QtWS25 Last Chance

[SOLVED] Disable QBrush rotating inside QGraphicsItem.

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vovasty
    wrote on last edited by
    #1

    Hi,
    I am attempting to draw QGraphicsRectItem object. I would like the brush not to rotate as the QGraphicsRectItem object is rotated.

    Does anybody know how can I achieve this?

    My implementation is below:
    graphicsrectitem.h
    @class GraphicsRectItem : public QGraphicsRectItem
    {
    public:
    GraphicsRectItem();

    protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

    private:
    QPointF _initialPos;
    qreal _rotation;
    QTransform _transform;
    bool _isTransformed;
    };@

    graphicsrectitem.cpp
    @GraphicsRectItem::GraphicsRectItem() :
    _rotation(0),
    _isTransformed(false)
    {
    setFlag(ItemIsMovable);
    setFlag(ItemSendsGeometryChanges);
    }

    void GraphicsRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    _initialPos = mapToScene(event->pos());
    QGraphicsItem::mousePressEvent(event);
    }

    void GraphicsRectItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
    QPointF pos = mapToScene(event->pos());

    if (pos.y() > _initialPos.y())
        ++_rotation;
    else
        --_rotation;
    
    setRotation(_rotation);
    _initialPos = pos;
    
    brush().setTransform(_transform);
    

    }

    void GraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem option, QWidget)
    {
    QLinearGradient gradient;
    gradient.setColorAt(0, Qt::white);
    gradient.setColorAt(1, Qt::gray);
    QBrush brush(gradient);
    painter->setBrush(brush);
    painter->drawRect(-40, -30, 80, 60);

    if (!_isTransformed)
    {
        _isTransformed = true;
        _transform = brush.transform();
    }
    

    }
    @

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Get the current object rotation and apply negative rotation to the brush before painting, i.e. change this:
      @
      QBrush brush(gradient);
      painter->setBrush(brush);
      @
      to
      @
      QBrush brush(gradient);
      QTransform t;
      t.rotate(-rotation());
      brush.setTransform(t);
      painter->setBrush(brush);
      @

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vovasty
        wrote on last edited by
        #3

        Thank you, it's working!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved