Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Bool QGraphicsItem::contains(const QPointF &point) const is never called
Forum Updated to NodeBB v4.3 + New Features

Bool QGraphicsItem::contains(const QPointF &point) const is never called

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 5.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Skyrpex
    wrote on last edited by
    #1

    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(&#41;;
    

    }@

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Skyrpex
        wrote on last edited by
        #3

        Yes, it looks like. I thought that mousePressEvent should call contains, which calls shape. Thanks for the quick answer!

        1 Reply Last reply
        0
        • E Offline
          E Offline
          Evelyn Tremble
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Skyrpex
            wrote on last edited by
            #5

            I would like to know if that's the intented behavior. I can't get contains called too.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dbzhang800
              wrote on last edited by
              #6

              [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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved