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. Multiple Rectangles Appearing On Mouse Move

Multiple Rectangles Appearing On Mouse Move

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.6k Views
  • 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 Offline
    A Offline
    asif_cse
    wrote on last edited by asif_cse
    #1

    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
    0
    • G Offline
      G Offline
      Gerd
      wrote on last edited by
      #2

      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
      0
      • A Offline
        A Offline
        asif_cse
        wrote on last edited by
        #3

        Thanks Gerd, for providing me the clue :)

        1 Reply Last reply
        0
        • G Gerd

          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 Offline
          A Offline
          asif_cse
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • G Gerd

            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 Offline
            A Offline
            asif_cse
            wrote on last edited by
            #5

            @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
            0
            • G Offline
              G Offline
              Gerd
              wrote on last edited by
              #6

              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
              0
              • G Gerd

                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 Offline
                A Offline
                asif_cse
                wrote on last edited by asif_cse
                #7

                @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
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

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

                  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