Bool QGraphicsItem::contains(const QPointF &point) const is never called
-
Hi all,
I'm using the Qt Graphics Framework for a while and now I suddenly discovered that the function metioned in the title won't get called. Here is the minimal code example (using Windows 7 and Qt 4.7.1 to Qt 4.7.4):
@#include <QtGui>
#include <QDebug>
#include <QTime>class Item : public QGraphicsItem
{
public:
inline Item()
{
this->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
}inline QRectF boundingRect() const { return QRectF(QPointF(0, 0), QPointF(100, 100)); } inline QPainterPath shape() const { qDebug() << __FUNCTION__ << QTime::currentTime(); QPainterPath path; path.addRect(this->boundingRect()); return path; } inline bool contains(const QPointF &point) const { qDebug() << __FUNCTION__ << QTime::currentTime(); return this->boundingRect().contains(point); } inline void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { painter->drawRect(this->boundingRect()); }
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);QGraphicsScene scene; scene.addItem(new Item); QGraphicsView view(&scene); view.show(); return a.exec();
}@
Maybe I should let it go and use contains in the mousePressEvent? It's strange because "the doc":http://doc.qt.nokia.com/stable/qgraphicsitem.html#contains says QGraphicsView calls contains, which calls shape by default... but the only called is shape. It's like my own contains doesn't override the QGraphicsItem contains.
-
I think you misinterpreted the API docs:
The docs gives an example when contains() is called: detecting whether the mouse curser is over a certain item.
The docs also state, that the default implementation calls shape().From your above snippet I would not expect contains() to be called.
-
Hi all,
Just to mention that the doc is rather ambiguous on this point.
It is explicitly stated that QGraphicsItem::contains is called from QGraphicsView to determine what item is under cursor.I mention this because I've noticed a very weird QGraphicsItem selection behavior in one of my apps when updating form Qt 4.6.3 to 4.8.0.
The only description I can give, is that some items couldn't be selected, even though they clearly were under the cursor, while others were selected when not under the cursor.I only inherited standard Qt classes (QGraphicsRectItem, in this case), overloadeding the paint methods only, so nothing that should have an impact on selection.
First, I was pretty surprised (until I found this thread after a few hours debugging) that QGraphicsItem::contains wasn't ever called.
And finally though, I simply solved it by overloading QGraphicsRectItem::collidesWithPath as follows :
@/**********************************************************/
bool MyRectItem::collidesWithPath ( const QPainterPath & path, Qt::ItemSelectionMode mode ) const
{
return contains(path.currentPosition());
}@Question is: shouldn't it be the standard behavior when it comes to finding if the item is under the cursor?
-
[quote author="Skyrpex" date="1333475801"]I would like to know if that's the intented behavior. I can't get contains called too.[/quote]
Hi Skyrpes,
I think you misinterpreted the API docs. You should call it by yourself if you need it to be called.
Debao