Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Create a square in a scene with RubberBandDrag
Qt 6.11 is out! See what's new in the release blog

Create a square in a scene with RubberBandDrag

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 2.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AlvaroSA Offline
    AlvaroSA Offline
    AlvaroS
    wrote on last edited by
    #1

    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.

    D 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      no sample for rubberband. sadly

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

      1 Reply Last reply
      0
      • AlvaroSA AlvaroS

        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.

        D Offline
        D Offline
        Devopia53
        wrote on last edited by
        #3

        @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);
        });
        
        AlvaroSA 2 Replies Last reply
        1
        • D Devopia53

          @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);
          });
          
          AlvaroSA Offline
          AlvaroSA Offline
          AlvaroS
          wrote on last edited by
          #4

          @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!

          1 Reply Last reply
          0
          • D Devopia53

            @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);
            });
            
            AlvaroSA Offline
            AlvaroSA Offline
            AlvaroS
            wrote on last edited by
            #5

            @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!!!

            1 Reply Last reply
            0
            • E Offline
              E Offline
              euchkatzl
              wrote on last edited by
              #6

              A Stack Trace would help.

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved