Qt World Summit: Submit your Presentation

Can I use QPainterPath shape() const instead QRectF boundingRect() const for Item movility?

  • I have a custom QGraphicsItem class. I can click on and drag on the scene:


    Now, I would like to know if I can replace the boundingRect() method, used to check if I'm clicking on the item, for shape(), where I ve a more specific region to check this click. How can I do that?

    Thank you.

    ------------------- EDITED-----------------------

    Thank you for your fast reply.

    I added in custom QGraphicItem class the following line in costructor:

    this->setFlag(QGraphicsItem::ItemClipsToShape, true);

    And I've checked if mouse pressent event is called after clicking on my shape:
    mousePressEvent(QGraphicsSceneMouseEvent *event)

    My shape is build with the addition of some QRectF:

    QPainterPath result;
    foreach (blah blah){
    //Building QRectF here

    But mouse event is only called when clicking on the first added rectangle. I know there are more than 1 rectangle, because I have drawn the QPainterPath, and I can see all the rectangles that I added.

    Maybe some help with that?

    Thank you!

  • Check QGraphicsItem::ItemClipsToShape setting... It seems to be what you need:

    bq. The item clips to its own shape. The item cannot draw or receive mouse, tablet, drag and drop or hover events outside its shape. It is disabled by default. This behavior is enforced by QGraphicsView::drawItems() or QGraphicsScene::drawItems(). This flag was introduced in Qt 4.3.

Log in to reply