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. Subclassing QGraphicsRectItem to have a reference rectangle on an image
QtWS25 Last Chance

Subclassing QGraphicsRectItem to have a reference rectangle on an image

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.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.
  • S Offline
    S Offline
    sergex
    wrote on last edited by
    #1

    In my application I am displaying an image on the MainWindow and also the same exact image in a smaller widget just below. For this I have a QGraphicsView, QGraphicsScene and a QGraphicsItem to display the image. And there are zooming capabilities as well.

    Now what I am trying to do is on the small image below, which never changes, I created a QGraphicsRectItem so that when I zoom in the big main image, the rectangle item that is displayed only on the small image, to be as a reference of how much I am zoomed and which portion of the original image I am looking at. Also I want to be able to move this rectangle and have it scroll the main image when I move it.

    The problems I am having is making the QGraphicsRectItem on the scene behave as I want it. For this I have a subclass of QGraphicsRectItem and that item is added to the scene so it is displaying the rectangle on top of the small image fine. I managed to get the events when I press, move and release on the small view, but those events are entered no matter where in the small image I press or move, etc. and I need it to only respond when the press and move are in the Rectangle (when my intention is to move the rectangle).

    For this I tried implementing the HoverEnter and HoverLeave events on the Rect Item but they are never reached. Also to resize the rectangle when I zoom in I tried with setRect and it doesn't do anything, the rectangle stays the same size on the scene. Can anyone please help me I would really appreciate any ideas, or tips on what might be wrong.

    Thanks!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rokemoon
      wrote on last edited by
      #2

      [quote author="sergex" date="1321629952"]
      For this I tried implementing the HoverEnter and HoverLeave events on the Rect Item but they are never reached. [/quote]
      To have hover events you need to set this "setAcceptHoverEvents":http://doc.qt.nokia.com/latest/qgraphicsitem.html#setAcceptHoverEvents

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sergex
        wrote on last edited by
        #3

        Yes, I did set that and did not make any difference.

        I also set the flags in my GraphicsRectItem class to be :

        @

        setFlags(QGraphicsRectItem::ItemIsMovable | QGraphicsRectItem::ItemIsSelectable |
        QGraphicsRectItem::ItemIsFocusable);

        setAcceptHoverEvents(true);
        @

        My main problem of at least changing the size of the rectangular item remains. For some reason setRect does not do anything.

        I have in the paint event of my rect item this:

        @

        void MyRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
        {
        QPen pen;
        pen.setStyle(Qt::SolidLine);
        pen.setColor(QColor(200, 200, 200));
        pen.setWidth(2);

        QBrush brush(Qt::Dense5Pattern);
        brush.setColor(QColor(200, 200, 200));
        painter->setPen(pen);
        painter->setBrush(brush);
        painter->drawRect(boundingRect());
        

        }
        @

        And the bounding rect method I did this:

        @
        QRectF MyRect::boundingRect() const
        {
        QRectF rect(leftPos, 0, rightPos - leftPos, myScene->sceneRect().height());
        return rect;
        }
        @

        In a different method is where I set the leftPos and rightPos values and do setRect(boundingRect()); I tried before calling this to do prepareGeometryChange() but still no effect. Just for testing I did in the same method setRect(x,y,w,h) instead and gave random values for x,y,w,h and the rectangle doesn't change.

        Any ideas? I would really appreciate it!
        Thanks.

        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