PDA

View Full Version : how to use setDragMode in QGraphicsView to draw a circle when user drags over figure



thierry Joel
12th August 2016, 12:12
I know how to use the setDragMode in QGraphicsView to allow the user draw a rectangle on figure. I want an easy way to be able to do the same thing only this time around, when the user clicks and drags on the figure, a circle should be drawn, and not a rectangle. I dont know if that is possible, but i would really appreciate your suggestions.

Here is a section of my code:


void Scene::setMode(Mode mode){
sceneMode = mode;
QGraphicsView::DragMode vMode =
QGraphicsView::NoDrag;
if(mode == DrawCircle){
makeItemsControllable(false);
vMode = QGraphicsView::RubberBandDrag;
}
else if(mode == DrawRect){
makeItemsControllable(true);
vMode = QGraphicsView::RubberBandDrag;
}
QGraphicsView* mView = views().at(0);
if(mView)
mView->setDragMode(vMode);
}

void Scene::makeItemsControllable(bool areControllable){
foreach(QGraphicsItem* item, items()){
item->setFlag(QGraphicsItem::ItemIsSelectable,
areControllable);
item->setFlag(QGraphicsItem::ItemIsMovable,
areControllable);
}
}


When mode == DrawCircle, i want the user to be able to drag and draw a circle on the figure instead of a rectangle. Thanks in advance.

anda_skoa
12th August 2016, 15:23
I don't think the rubber band drawing can easily be customized, but could likely implement your own rubber band circle by deriving from the view class, implementing the mouse even handler methods an drawing the circle in drawForeground().

Cheers,
_

thierry Joel
12th August 2016, 18:36
Thanks for the advice.

Added after 10 minutes:

Can you give me more information on what you described, or probably help me with a link to where a similar situation is handled? because am pretty new to qt and c++

anda_skoa
12th August 2016, 21:00
Maybe the ScribbleExample http://doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html as a starting point on how to do event handling in derived classes and how painting works.

Cheers,
_