How to draw rectitems with mouse
-
hi..i want to draw rectitems on the scene with mouse ... i tried like this
@
void scene::mousePressEvent(QGraphicsSceneMouseEvent* event) {if (!rubberband)
rubberband = new QRubberBand(QRubberBand::Rectangle, this);
startPoint = event->pos();
rubberband->setGeometry(QRect(startPoint,QSize()));
rubberband->show();
rubberBandActive = true;
}
void scene::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
if(rubberBandActive){
rubberband->setGeometry(QRect(startPoint,event->pos()).normalized());
}
}
void scene::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
if(rubberBandActive){
endPoint = event->pos();
rubberBandActive = false;
rubberband->hide();
}
}@
i am not able to draw the rect item with this code can some one tell me how to draw rectitem with mouse
[edit: please add @ code tags to make your code more readable, Eddy]
-
Please wrap your code within 'at' to make appropriate formatting.
QRubberBand is (AFAIK) used for selecting of items, not for drawing rectangles. So it will disappear after you release left mouse button. But you are storing endPoint so you can use it along with startPoint in your paintEvent (or whatever QGraphicsScene is using) to paint desired rectangle.
-
Please read this part of the "forum help":http://developer.qt.nokia.com/wiki/ForumHelp#9bd9c32b79efb1b2d5b039e4d48300a9 about how to use syntax highlighting in your thread.
Please help us to make your questions easier to read. That will attract more people to help solve your problem.
-
have a look at the "graphics view doc":http://doc.qt.nokia.com/4.7/graphicsview.html
There is example code there.
"addRect":http://doc.qt.nokia.com/4.7/qgraphicsscene.html#addRect is what you need I suppose.
Make your scene and use one of it's addRect methodsedit : "complete examples":http://doc.qt.nokia.com/4.7/examples-graphicsview.html can be found here.