my qgraphicsscene is having qgraphicswidget which constantly adding qgraphicsLayoutItem. in graphicsView i need to get the qgraphicswidget geometry in scene coordinated.
i tried
QList items = scene()->items();
and check its with type
foreach (QGraphicsItem *item, items) {
if(item->type() == ItemType)
{
}
but how to convert item to qgraphicswidget and change its gemoetry to scene coordinates.
normal item.boundingRect returns constantly 0,0, 10x10
The bounding rect of the item is in item coordinates. To map it to scene coordinates, use QGraphicsItem::mapToScene():
const QRectF mapped = item->mapToScene(item->boundingRect());
To cast a QGraphicsItem, you can simply use dynamic_cast or static_cast, or the special qgraphicsitem_cast:
auto widget = qgraphicsitem_cast<QGraphicsWidget*>(item);
To map the coordinates, casting shouldn’t be necessary though.
Related
According to the Qt's documentation, the QPainter is working on the logical coordinate.
But how about the sceneRect of the QGraphicsScene and the boundingRect of the QGraphicsItem?
Are they working on the logical coordinates or the physical coordinates?
If it's on the logical coordinates, is there any functions like the QPainter::setWindow for them?
A GraphicsItem's boundingRect defines its area in local coordinates; local to the item. So, an item derived from QGraphicsItem, which overrides its paint function, can draw the item's area by drawing its boundingRect: -
painter->drawRect(boundingRect());
The sceneRect of a QGraphicsItem is the item's boundingRect translated into scene coordinates.
So, for example, from this skeleton class:
class MyItem : public QGraphicsItem
{
public:
QRectF boundingRect() const { return m_boundingRect; }
private:
QRectF m_boundingRect = QRectF(-10, -10, 20, 20);
}
The bounding rect is defined such that its centre lies at (0,0) in local coordinates.
If we add it to a scene, at position (0,0), calling the item's sceneBoundingRect function will return the same coordinates.
Now, if we move the item 5 units in the x: -
pItem->setPos(5, 0);
The boundingRect returns the same local coordinates, but its sceneBoundingRect will return its position in the scene; (-5, -10, 20, 20), with these being (x, y, width,height).
If an item is a child of another item, then this will be taken into account, as setting its position sets it relative to its parent or, in the case of no parent, will set it as the coordinates in the scene.
Therefore, calling an item's boundingRect() function, will always return the same coordinates, regardless of where the item resides in the scene, but it's sceneBoundingRect will return scene coordinates; where it resides in the scene.
If it's on the logical coordinates, is there any functions like the QPainter::setWindow for them?
Yes, the QPainter has its own transformation system, which allows you to perform actions such rotation or scaling before drawing. You can read more about its coordinate transformation in the Qt documentation for QPainter
sceneRect() and boundingRect() work in the scene coordinates (logical coordinates). However if you draw in a scene the QPainter also resides in these cordinates, it does not know the physical coordinates.
You probably want to use setWorldTransform() instead of setWindow(). While setWindow() might still work as intended, it does not support floating point coordinates, which is what you get from boundingRect() and friends.
To get back to physical coordinates from the QGraphicsScene, you can use QGraphicsView::mapToGlobal().
I have a QGraphicsScene which stores QGraphicsLinesItems and QGraphicsRectItems.
I am using the QGraphicsScene method to itemsAt() and I pass through x and y co ordiantes to return the QGraphicsItem and I use the following:
QPointF getItemPos= this->mapToScene(this->mapFromGlobal(QCursor::pos()));
QGraphicsItem *itm = scene->itemAt(getItemPos.x(),getItemPos.y());
QGraphicsLineItem *lineItm;
QGraphicsRectItem *rectItm;
if((lineItm = dynamic_cast<QGraphicsLineItem*>(itm))){
// do stuff with as_pnedge
qDebug("Line");
}else if((rectItm = dynamic_cast<QGraphicsRectItem*>(itm))){
// do stuff with as_pnitem
qDebug("Rect");
}
else
{
qDebug("Select Item");
}
The issue I am having is that QGraphicsRectItem is returned fine and can be detected. But no matter where I click around the QGraphicsLineItems it can never detect and return the item. Any assistance would be very much appreciated.
If your line uses a cosmetic pen (width of zero), it means that the shape() method will return a zero width QPainterPath (source code, search for "qt_graphicsItem_shapeFromPath").
You will have to derive from QGraphicsLineItem and reimplement shape() to clamp the minimum pen width for QPainterPathStroker to something reasonable.
I have a problem.
I have a class that inherits QGraphicsView, let's suppose it called "g". I reimplemented mousePressEvent method, the code of that method is:
void GraphWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::MiddleButton)
createNode(event->pos().x(), event->pos().y());
update();
QGraphicsView::mousePressEvent(event);
}
The code of createNode method is:
Node *GraphWidget::createNode(qreal x, qreal y, int id)
{
Node* node = new Node(this);
scene()->addItem(node);
node->setPos(x, y);
return node;
}
I use this class "g" as a central widget in my mainwindow class. So it's working like QGraphicsView.
The problem is when I press the middlebutton on the "draw area" - the point is created but not in the place when I clicked - the point is shifted. Why? So when I try to draw those points by pressing the middlebutton - all them are drawed on the wrong place (not under my cursor, they are drawn lefter left and above my cursor position).
How can I fix that?
QGraphicsView and QGraphicsScene have different coordinate spaces. When you call setPos, it should be in scene coordinates, but since your in the mouse event of the view, your x and y are going to be in view coordinates.
I suspect that mapping your x and y coordinates to the scene space should fix the issue:
node->setPos( mapToScene(QPoint(x, y) );
I have a scene where several items are added. The problem is that when the items are displayed, they are overlapping. Is there any way to indicate in the QGraphicsView or QGraphicsScene the position where each item should appear?
Yes, you have to use QGraphicsItem::setPos() method.
I suppose you added a QGraphicsPixmapItem, so it could look like :
QGraphicsScene *scene = ... ; // your scene
QImage image = ... ; // the QImage you want to add to the scene
QPixmap pixmap = QPixmap::fromImage(image) ;
// add image item to the scene
QGraphicsPixmapItem * imageItem = scene->addPixmap(pixmap) ;
// modify item's position in scene coordinates
QPointF imagePos = ... ; // whatever scene pos you want
imageItem->setPos(imagePos) ;
I can construct a QGraphicsScene by using constructor below:
QGraphicsScene::QGraphicsScene ( qreal x, qreal y, qreal width, qreal height, QObject * parent = 0 )
For example:
QGraphicsScene scene(-350, -350, 700, 700);
I know the first two parameters represent a point, but in which coordinate system?
When I create a QGraphics object and show this view like this:
QGraphicsView view(&scene);
view.show();
Where will this view appear on my device?
Is the location controlled by first two parameters mentioned above?
The first two parameter mean, when a view focus on this scene, which point in the scene is the top-left corner of the view.
In your code, the -350,-350 will be the top-left corner of the view window.
Here's another example:
QGraphicsScene *scene = new QGraphicsScene;
scene->setSceneRect(-360,-240,720,480);
QGraphicsView *view = new QGraphicsView(this);
view->setScene(scene);
if you try to add item without telling where to show it, it will show at (0,0). And in the code above, is the center of the view.