Create a square in a scene with RubberBandDrag



  • Hello to everyone.

    First of all thanks a lot for reading this post and being able to help.

    I have an Scene where I would like to add a square item (polygon) just with RubberBandDrag mode.

    So user clicks on scene and drag mouse and when users releases mouse then add the polygon.

    How can I do that? thanks.


  • Qt Champions 2016

    Hi
    no sample for rubberband. sadly

    Maybe this is interesting?
    http://www.walletfox.com/course/qgraphicsitemruntimedrawing.php



  • @AlvaroS

    Hi.

    I have an Scene where I would like to add a square item (polygon) just with RubberBandDrag mode.
    So user clicks on scene and drag mouse and when users releases mouse then add the polygon

    something like this:

    drawRect = QRectF(0, 0, 0, 0);
    connect(ui->graphicsView, &QGraphicsView::rubberBandChanged,
            [&](QRect rubberBandRect, QPointF fromScenePoint, QPointF toScenePoint){
        if (rubberBandRect.isNull()) {
            ui->graphicsView->scene()->addRect(drawRect);
            drawRect = QRectF(0, 0, 0, 0);            
        }
        else
            drawRect = QRectF(fromScenePoint, toScenePoint);
    });
    


  • @Devopia53 Thanks a lot my friend. It works.

    Now, how can i disconnect it??
    Because I would like to connect it when I press a button and when I press the same button again to disconnect it.

    Thanks again!



  • @Devopia53 said:

    @AlvaroS

    Hi.

    I have an Scene where I would like to add a square item (polygon) just with RubberBandDrag mode.
    So user clicks on scene and drag mouse and when users releases mouse then add the polygon

    something like this:

    drawRect = QRectF(0, 0, 0, 0);
    connect(ui->graphicsView, &QGraphicsView::rubberBandChanged,
            [&](QRect rubberBandRect, QPointF fromScenePoint, QPointF toScenePoint){
        if (rubberBandRect.isNull()) {
            ui->graphicsView->scene()->addRect(drawRect);
            drawRect = QRectF(0, 0, 0, 0);            
        }
        else
            drawRect = QRectF(fromScenePoint, toScenePoint);
    });
    

    @mrjj said:

    Hi
    no sample for rubberband. sadly

    Maybe this is interesting?
    http://www.walletfox.com/course/qgraphicsitemruntimedrawing.php

    Hello again. i have just figured it out the solution and it is the next:

        if (check_generate_boxes_button == 0){
            ui->graphicsView->blockSignals(false);
            connect(ui->graphicsView, &QGraphicsView::rubberBandChanged,
                    [&](QRect rubberBandRect, QPointF fromScenePoint, QPointF toScenePoint){
                if (rubberBandRect.isNull()) {
                    rectangle = ui->graphicsView->scene()->addRect(drawRect);
                    std::cout << "estoy en if" << std::endl;
                    drawRect = QRectF(0, 0, 0, 0);
                    rectangle->setFlag(QGraphicsRectItem::ItemIsSelectable, true);
                    rectangle->setCursor(Qt::PointingHandCursor);
                }
                else{
                    drawRect = QRectF(fromScenePoint, toScenePoint);
                    std::cout << "estoy en ELSE DEL if" << std::endl;
                }
    
            });
            check_generate_boxes_button = 1;
        }
    
        else{
            ui->graphicsView->blockSignals(true);
            check_generate_boxes_button = 0;
        }
    

    I would like that the rectangle is selectable. It does not give me an error but when I select the rectangle that I define the program is unexpectedly finish.

    Does anyone know why?

    thanks!!!



  • A Stack Trace would help.

    What are you doing with your rectangle variable ?
    Is it deleted anywhere ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.