0

I'm developing a small Qt application similar to the DiagramScene example. I have subclassed QGraphicsView instead of QGraphicsScene. My view is zooming in and out with the mouseWheel, I can drag it with the mouse and I can add Nodes and Links with clicking.

I click on one Node, (the first end of the line item is set), then move the mouse (the second end of the line is following the mouse cursor), then I click on the second Node and anchor the second end of the line item to this second node.

The problem is, when the view is zoomed in, or I have moved the view, when I click on a Node and move the mouse, the preview of the link is not visible. When I click on the second Node - the link is still not visible. The link between the two Nodes becomes visible only after I zoom out or drag the view to some point and it intersects with the sides of the view.

Any ideas how to fix this? Thank you so much in advance.

Expr
  • 25
  • 1
  • 5
  • It sounds like it could be a problem with not handling the different coordinate systems (local / scene / view), but without a code example, it's difficult to say for sure. – TheDarkKnight Oct 28 '13 at 11:24
  • @Merlin069 Unfortunately the code is too long or I don't know how to post it correctly(I'm very new to stackoverflow). I have reimplemented the mousePress, move and release events of QGraphicsView and whenever they are triggered I get their coordinates and map them to the scene with mapToScene(event->pos()). I that correct? – Expr Oct 28 '13 at 11:33
  • That depends on what you do with those mapped coordinates. Without a code example, I don't know. – TheDarkKnight Oct 28 '13 at 11:45
  • @Merlin069 Here`s the code for the events http://pastebin.com/RYWn18iv – Expr Oct 28 '13 at 11:52
  • I've tried the following: 1) Subclassing QGraphicsScene just like in the DiagramScene example 2) Reimplementing the events with different logic 3) Drawing a QGraphicsLineItem for the preview and when you connect it to the second node it is deleted and a Link is drawn from Node A to Node B All of these have the same effect, when I zoom in or drag the scene the Links are not visible until I zoom out a lot. I have to force the View to update itself and show the lines, but it's not working with any function. The only solution I can think of is to zoom out and then zoom in every time I add a link. – Expr Oct 28 '13 at 21:52
  • Looking at the code, you create a new Link, what is that class - is it derived from a QGraphicsLineItem, QGraphicsObject, or something else? Also, what does line->setX2 and line->setY2 do? Does it set local coords, scene coords or something else? Can you show that code? – TheDarkKnight Oct 29 '13 at 08:41
  • @Merlin069 Link is a subclass of QGraphicsLineItem. Link has X1,Y1,X2,Y2 data members and I'm setting them with setX2 and so on. I think they are local. The same thing is in the Node class. I think that's what's wrong. Cause when I move or zoom in the view the Links aren't visible until the two Nodes are back to their coordinates. That is why when I zoom out the links appear. How can I make custom GraphicsItems with the correct handling of coordinates? – Expr Oct 29 '13 at 09:13

1 Answers1

1

I finally fixed it. I was wrong to use data members for the coordinates (also the bounding rect and paint method) of my custom Graphics items. I changed the code using the setPos() function which gives the right coordinates to my items. @Merlin069 thank you, actually your last question got me thinking whether I set the coordinates correctly.

Expr
  • 25
  • 1
  • 5