I'm trying to draw a hexagonal lattice in Qt, in which each hexagon needs to be clickable. I've subclassed QGraphicsItem and am drawing the lattice fine using a for loop. The problem is that because the bounding rectangles for adjacent hexagons overlap, half the time the wrong hexagon gets clicked.
Is there any way around this? Which QGraphicsItem, QGraphicsScene, or QGraphicsView method would I need to re-implement?
QGraphicsItem::shape()
Related
I'm painting onto QPixmap with transparency with QPainter. How to erase a polygon with QPainter::drawPolygon()? I need to erase not only the edges of the polygon but the inside area too.
painter.setCompositionMode(QPainter::CompositionMode_Clear);
See Composition Modes sample provided with Qt.
I am trying to make a simple program in which I have added a qgraphics scene and in this I have added a QGraphicsRectItem. I have implemented mouse press event, paint event, bounding rect. Now I have drawn a point on one side of rectangle because there can be multiple rectangle I can drop on screen so just to differentiate between them of different color. Now I can move my rectangle inside graphics seen and can increase the size of rectangle by moving it's one side at a time. The problem that I am facing is when I trying to draw point on one side of rectangle at the time of moving it, it leaves traces on graphics scene. can I remove the ghost lines?
This happens either because your boundingRect method isn't correct, or because you forgot to call prepareGeometryChange before making changes that affect the boundingRect result. Your boundingRect needs to include space for line widths, for example; that's a common mistake.
I'm in trouble with such issue:
I need to select some area with rect on my label, I'm using QRubberBand to do this, but there is one problem: I need to know coordinates of current rect on my label, so I'm in trouble with it, because mouseEvent->pos() give coordinates which starts with top left corner on mainWindow border, moreover i've rotate standard coords on label (from top left to bottom left corner, as we paint them usually).
Anybody knows how can I do this translation?
QPoint mappedPos = myLabel->mapFromParent(myWindow, mouseEvent->pos());
Also, QTransform provides a number of map() functions which should be able to get you the point in rotated coordinates as well.
See:
QWidget::mapFromParent()
QTransform::map()
I have a QGraphicsItem with a custom graphic as shown below in the top half of the figure, where the red circle is the shape() of the item:
Is there a way to preserve the graphic but just shift it like shown in the bottom half of the figure?
I don't think, you can move QGraphicsItem without shape. Shape is bounded to QGraphicsItem.
It changes its co-ordinates according to QGraphicsItem.
If you want to achieve something like your second half of the fig. You need to have two QGraphicsItems (one is your red outer circle and second your inner graphics item).
I'm trying to use QGraphicsView in order to display a map image and draw some items on top of it. The map boundaries are in a Cartesian coordinate system, for example NE(-500,200) to SW(600,-350). I know how to map image pixels <--> my coordinate system.
I would have to achieve the following:
Add a map image to the scene and tell Qt how to map the actual image pixels to scene coordinates.
Add graphic items at their real position, e.g. (-100,200)
Doing (2) is straightforward - simply add the item to the scene. How do I achieve (1)? what should I do after I call scene->addPixmap()?
Edit - A few clarifications:
I'm mapping an indoor area of a few hundred meters
The map will change at real-time in two ways:
The map gets bigger every few seconds
The graphic items move, change colors, etc.
Put the pixmap into a QGraphicsPixmapItem and place it in the scene.
Call setScale() to map the QGraphicsPixmapItem so 1 meter maps to 1 unit in the scene coordinate. ie. setScale(0.1) if 10 pixels in the pixmap equal 1 meter.
Update the pixmap and scale of the item as needed.
Call fitInView() to zoom to the pixmap.
Place other graphic items in the scene. Treat the units of the scene coordinate as meters.
...
Profit! :)