Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.



  • paint event i wrote like but iam not getting any rect item

    void scene::paintEvent(QPaintEvent *event)

    {
    QPainter painter(this);
    painter.drawRect(QRect(startpoint,endpoint));
    }



  • 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.



  • @void scene::paintEvent(QPaintEvent *event)
    {
    QPainter painter(this);
    painter.drawRect(QRect(startpoint,endpoint));

    }
    @
    i wrriten like this but it was not adding to the scene



  • 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 methods

    edit : "complete examples":http://doc.qt.nokia.com/4.7/examples-graphicsview.html can be found here.



  • no,i just want to draw rectitem with mouse on the scene..


Log in to reply