Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Unsolved Multiple Rectangles Appearing On Mouse Move

    General and Desktop
    3
    8
    1252
    Loading More Posts
    • 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.
    • A
      asif_cse last edited by asif_cse

      Hello every one, I am new to qt c++. So as usual, I am facing problems :) I am developing a very simple paint application for desktop using qt gui application. I am ok with drawing free hand, but when I tried to draw rectangle on mouse move, multiple rectangles appearing! I need help to solve this problem. I want to know why this is happening!

      My code looks like below :

      void MainWindow::mouseMoveEvent(QMouseEvent *e)
      {
      QPen pen(mColor);
      pen.setCapStyle(Qt::RoundCap);
      pen.setWidth(mSize);
      mEnd = e->pos();
      QRect rect(mBegin.x(), mBegin.y(), mEnd.x(), mEnd.y());
      mPainter->setPen(pen);
      mPainter->drawRect(rect);
      mBegin = mEnd;
      update();
      e->accept();
      }

      Someone please help me to solve this problem. Thanks in advance :)

      1 Reply Last reply Reply Quote 0
      • G
        Gerd last edited by

        Hi,
        this is happening bacause you draw a new rectangle on every MouseMove. What you need is a Rubberband during mouse moves and drawing the rectangle in response to MouseRelease.
        The Documentation of QRubberband should give you a hint how to do that.

        A 2 Replies Last reply Reply Quote 0
        • A
          asif_cse last edited by

          Thanks Gerd, for providing me the clue :)

          1 Reply Last reply Reply Quote 0
          • A
            asif_cse @Gerd last edited by

            @Gerd
            Thanks Gerd, for providing me the clue :)

            1 Reply Last reply Reply Quote 0
            • A
              asif_cse @Gerd last edited by

              @Gerd
              Mouse Release Event is OK, but it does not mean it :( I want to show the drawings while mouse is moving, but not after releasing the mouse! And QRubberBand::Shape has line and rectangle not other shapes! I am not getting any idea how I will solve it !

              1 Reply Last reply Reply Quote 0
              • G
                Gerd last edited by

                Not sure what you mean.
                In your initial post you are talking about rectangles, so the way to do that is this:

                • on MousePressEvent start a rubberBand
                • on mouseMoveEvent update the rubberBand rectangle
                • on mouseReleaseEvent hide the rubberBand, take the geometry of the rubberBand and draw your rectangle with this geometry

                If you need to do this with other shapes you have to implement the background saving and restore by yourself, but thats another question...

                A 1 Reply Last reply Reply Quote 0
                • A
                  asif_cse @Gerd last edited by asif_cse

                  @Gerd
                  Yes it is Ok, what you have given as a solution is ok, but I have told I am developing paint tool, that is why besides rectangles I will need other shapes too :)

                  Thank you for your guidence. I am trying to solve this. And obviously your suggestion was very helpful, though I could not properly utilize!

                  1 Reply Last reply Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion last edited by

                    hi
                    maybe you can be inspired by this sample
                    https://github.com/bruceoutdoors/DrawingApp

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post