Make QGraphicsProxyWidget movable & selectable

后端 未结 2 1907
刺人心
刺人心 2021-02-02 04:38

I want to put a QWidget into a QGraphicsView and make the widget selectable and movable by using QGraphicsProxyWidget. (This works perfect

2条回答
  •  无人及你
    2021-02-02 05:03

    This behavior is not intended by QGraphicsProxyWidget, e.g. consider you have a QTextEdit widget and you want to click on the text of this widget to select something. At this point you have the problem whether this mouse event should be handled by the QTextEdit for the selection process or by the GraphicsWidget to move the QTextEdit.

    Option 1: Derive from QGraphicsProxyWidget and in the mouse functions (mouseMove, ...) you can call QGraphicsItem::mouseMove() again, this should bring back again the functionality of moving the Widget.

    Option 2: This is more like a workaround, but in most cases the better solution, because it prevents you from managing the events as it would be necessary by option 1. Just create a GraphicsRectItem R, which is larger than your Widget W. Set R movable etc. Add W to your scene, you will receive a QGraphicsProxyWidget P. Call P->setParentItem(R) and finally scene->AddItem(R);

    Now your Widget can be moved by moving the parent rectangle. You can also add additional GraphicsRectIrems r which have R as parent, e.g. to implement a resizing function. You would just have to install an EventFilter for r and react on the mouseMove event in the filter function.

提交回复
热议问题