QGraphicsItem -> get the correct bounding rectangle - qt

I am trying the hello triangle example of OpenGL ES 2.0. I am using Qt, so I created a QGraphicsScene and added that code as a QGraphicsItem. It draws correctly, but I cannot get the bounding rectangle correctly. The triangle vertices are
GLfloat afVertices[] =
{-0.4f,-0.4f,0.0f,
0.4f ,-0.4f,0.0f,
0.0f ,0.4f ,0.0f};
and my viewport is glViewport(0, 0, 800, 480);
What would be the correct bounding rect coordinates?
I set the viewport to a QGLWidget. The thing with the QGraphicsItem is that I have to re-implement the bounding rectangle of the item and if I just use
QRectF myGraphicsItem::boundingRect() const
{
return QGraphicsItem::boundingRect();
}
it says undefined reference to `QGraphicsItem::boundingRect() const'
I had originally used
QRectF myGraphicsItem::boundingRect() const
{
return QRectF(-0.4, -0.4, 0.8, 0.8);
}
but the result is a very small bounding box. The seemingly correct one was created when I was used values like QRectf(300, 200, 200, 200) by trial and error -which is too 'manual'-, so I was wondering maybe there is some kind of coordinate correspondence or transformation that I'm unaware of.

QGraphicsItem::boundingRect() is a pure virtual function. Thus, there is no implementation. You must provide your own implementation. Based upon your vertices, probably
QRectF myGraphicsItem::boundingRect() const
{
return QRectF(-0.4, -0.4, 0.8, 0.8);
}

I'm not sure I follow, if you're using a QGraphicsItem (with or without an OpenGL viewport), you would typically use QGraphicsItem::boundingRect() to get the bounding rectangle?

I would do (in Python):
# inside class
def parentBoundingRect(self):
return self.mapToParent(self.boundingRect()).boundingRect()
# or if that doesn't work
def parentBoundingRect(self):
pos = self.pos()
rect = self.transform().mapToPolygon(self.boundingRect()).boundingRect()
return QRectF(pos.x(), pos.y(), rect.width(), rect.height())
# or if that doesn't work, keep playing with it til it does! :)

Related

Which coordinate do sceneRect and boundingRect work on?

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().

Customizing shape of bounding rect

I am drawing a line using mouse clicks. The line is drawn using paint function as:
painter->drawLine(start_p, end_p);
The bounding rect of line is defined as:
QRectF Line::boundingRect() const
{
// bounding rectangle for line
return QRectF(start_p, end_p).normalized();
}
This shows the line painted. I get the bounding rect for this as shown:
I want to have the bounding rect according to the shape of the item, something like:
How to achieve this?
Edit
While selecting any of the overlapping lines, the one with bounding rect on top is selected(see figure below). Even making use of setZValue won't work here.
I want to implement this by minimizing the bounding rect to the shape of line.
If you have an item that is not shaped like a rectangle, or is a rotated rectangle use QGraphicsItem::shape.
This function should return a QPainterPath. You should be able to create your path by using QPainterPath::addPolygon.
Here is a small example:
QPainterPath Item::shape() const
{
QPainterPath path;
QPolygon polygon;
polygon << QPoint(0, 0);
polygon << QPoint(5, 5);
polygon << QPoint(width, height);
polygon << QPoint(width - 5, height - 5);
path.addPolygon(polygon);
return path;
}
You of course should calculate your points inside the path in a different way, but you get the point. Now when you click on an item, it will only select it if the click happened inside the shape defined by the QPainterPath.
If you ever need to make curvy lines, you can use QPainterPathStroker::createStroke as suggested by cmannett85.
There are two relevant functions in a QGraphicsItem that you should be interested in. The first is boundingRect. This, as you probably realise is a rectangle which encompasses the whole item. Qt uses this for such things as quickly calculating how much of an item is visible and simple item collision.
That's great if you have rectangular items; you can just override boundingRect() in any items you inherit from QGraphicsItem or QGraphicsObject.
If you have a shape that isn't regular and you want to do things such as collision with an item's shape, then theshape() function needs overriding too in your class.
This returns a QPainterPath, so you can do something like this: -
QPainterPath Line::shape()
{
QRectF rect(start_p, end_p).normalized();
// increase the rect beyond the width of the line
rect.adjust(-2, -2, 2, 2);
QPainterPath path;
path.addRect(rect);
return path; // return the item's defined shape
}
Now, you can use a painter to draw the shape() item, instead of the boundingRect() and collision will work as expected.
boundingRect is always used for optimize painting process of of scene. So you have have no room for manipulation here.
BUT if you want change area for mouse interaction there is shape method. By default this method returns QPainterPath rectangle received from boundingRect method.
So just override this method and provide desired shape.
QPainterPath YourGraphicsItem::shape() const {
static const qreal kClickTolerance = 10;
QPointF vec = end_p-start_p;
vec = vec*(kClickTolerance/qSqrt(QPointF::dotProduct(vec, vec)));
QPointF orthogonal(vec.y(), -vec.x());
QPainterPath result(start_p-vec+orthogonal);
result.lineTo(start_p-vec-orthogonal);
result.lineTo(end_p+vec-orthogonal);
result.lineTo(end_p+vec+orthogonal);
result.closeSubpath();
return result;
}
You must draw yourself bounding if you want some thing like this. let Qt have it's QRect for bounding and define your new QRect dependent to the corner of previous QRect, top-left and bottom-right. for example if the top-left corner is (2,2) your new QRect top-left is (1,2) and top-right is (2,1) and ....

Determine bounding rect of line in Qt

I am drawing a line using QPainterPath between two points as follows:
QPainterPath line;
line.moveTo(start_p);
line.lineTo(end_p);
QPen paintpen(Qt::black);
paintpen.setWidth(1);
painter->setRenderHint(QPainter::Antialiasing);
painter->setBrush(Qt::SolidPattern);
painter->setPen(paintpen);
painter->drawPath(line);
I have defined bounding rect as:
QRectF Line::boundingRect() const
{
return QRectF(start_p.x(), start_p.y(), end_p.x(), end_p.y());
}
I get line painted correctly when:
start_p.x() < end_p.x()
and
start_p.y() < end_p.y()
How should the bounding rect be defined so that line is drawn correctly irrespective of the relationship between the coordinates of two points(start_p and end_p)?
You might try to normalize your rectangle:
QRectF Line::boundingRect() const
{
return QRectF(start_p.x(), start_p.y(), end_p.x(), end_p.y()).normalized();
}
You could either:-
Check for the conditions when the ends are greater than the start points and set the rect appropriately
Return the QPainterPath's bounding rect
Use a QGraphicsLineItem instead of reinventing the wheel.
If you just want a line, QGraphicsLineItem is likely the best way to go here.
You can use QPainterPath::boundingRect which returns the bounding rectangle of the QPainterPath. You can keep the painter path as a class member and access it in the boundingRect function :
QRectF Line::boundingRect() const
{
return line.boundingRect();
}

Using QGraphicsScene ItemAt() to detect QGraphicsLineItem

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.

how to center a Qt mainform on the screen?

I've tried these in my mainform's constructor:
QRect desktopRect = QApplication::desktop()->availableGeometry(this);
move(desktopRect.center() - frameGeometry().center());
QRect desktopRect = QApplication::desktop()->availableGeometry(this);
move(desktopRect.center() - rect().center());
but both put the bottom right corner of the form at about the center of the screen, instead of centering the form. Any ideas?
I've tried these in my mainform's constructor
That's likely the problem. You probably don't have valid geometry information at this point because the object isn't visible.
When the object is first constructed, it's essentially positioned at (0,0) with it's expected (width,height), as such:
frame geometry at construction: QRect(0,0 639x479)
But, after being shown:
frame geometry rect: QRect(476,337 968x507)
Thus, you can't yet rely on your frameGeometry() information.
EDIT: With that said, I presume you can easily move it as desired, but for completeness I'm dropping in Patrice's code which doesn't depend on the frame geometry information:
QRect desktopRect = QApplication::desktop()->availableGeometry(this);
QPoint center = desktopRect.center();
move(center.x() - width() * 0.5, center.y() - height() * 0.5);
The move function (see QWidget doc) takes one QPoint or two int as parameter. This corresponds to the coordinates of the top-left corner of your Widget (relative to its parent; Here OS Desktop).
Try:
QRect desktopRect = QApplication::desktop()->availableGeometry(this);
QPoint center = desktopRect.center();
move(center.x()-width*0.5, center.y()-height*0.5);
availableGeometry() is deprecated.
move(pos() + (QGuiApplication::primaryScreen()->geometry().center() - geometry().center()));
#include <QStyle>
#include <QDesktopWidget>
window->setGeometry(
QStyle::alignedRect(
Qt::LeftToRight,
Qt::AlignCenter,
window->size(),
qApp->desktop()->availableGeometry()
)
);
https://wiki.qt.io/How_to_Center_a_Window_on_the_Screen
move(QGuiApplication::primaryScreen()->geometry().center() - rect().center());
PyQT Python Version
# Center Window
desktopRect = QApplication.desktop().availableGeometry(self.window)
center = desktopRect.center();
self.window.move(center.x()-self.window.width() * 0.5,
center.y()-self.window.height() * 0.5);
Another solution, assuming the window in question is 800×800:
QRect rec = QApplication::desktop()->availableGeometry();
move(QPoint((rec.width()-800)/2, (rec.height()-800)/2));

Resources