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. Drawing QRubberBands with variabe size, but fixed aspect ratio: how to do it?

Drawing QRubberBands with variabe size, but fixed aspect ratio: how to do it?

Scheduled Pinned Locked Moved Solved General and Desktop
qrubberbandqrectconstraints
3 Posts 2 Posters 321 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.
  • B Offline
    B Offline
    BwvB
    wrote on 19 Feb 2024, 21:49 last edited by
    #1

    Hi, I am using the QRubberBand class to draw a rectangular shape on the screen.

    By trapping the mouse position when the mouse is pressed, and connecting mouse move events to the setGeometry function I can move the mouse around, with the rubberband following the mouse cursor in a rectangular shape. This all works ok.

    I would like to constrain the mouse movements such that the rectangular rubber band still is of variable size, but always has a fixed aspect ratio. This could be handy, for instance, when cropping an image.
    Are there any suggestions how I could achieve such a constraint?

    Kind Regards,
    Bertwim

    J 1 Reply Last reply 20 Feb 2024, 06:31
    0
    • J jsulm
      20 Feb 2024, 06:31

      @BwvB You could calculate the size so that it has the aspect ratio you want in mouseMoveEvent (see https://doc.qt.io/qt-6/qrubberband.html):

      void Widget::mouseMoveEvent(QMouseEvent *event)
      {
          // Here calculate the QRect the way you need it
          rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
      }
      
      B Offline
      B Offline
      BwvB
      wrote on 20 Feb 2024, 10:04 last edited by
      #3

      @jsulm said in Drawing QRubberBands with variabe size, but fixed aspect ratio: how to do it?:

      rubberBand->setGeometry(QRect(origin, event->pos()).normalized());

      Thanks. It is indeed as easy as you indicated!
      Regards
      Bertwim

      1 Reply Last reply
      0
      • B BwvB
        19 Feb 2024, 21:49

        Hi, I am using the QRubberBand class to draw a rectangular shape on the screen.

        By trapping the mouse position when the mouse is pressed, and connecting mouse move events to the setGeometry function I can move the mouse around, with the rubberband following the mouse cursor in a rectangular shape. This all works ok.

        I would like to constrain the mouse movements such that the rectangular rubber band still is of variable size, but always has a fixed aspect ratio. This could be handy, for instance, when cropping an image.
        Are there any suggestions how I could achieve such a constraint?

        Kind Regards,
        Bertwim

        J Online
        J Online
        jsulm
        Lifetime Qt Champion
        wrote on 20 Feb 2024, 06:31 last edited by
        #2

        @BwvB You could calculate the size so that it has the aspect ratio you want in mouseMoveEvent (see https://doc.qt.io/qt-6/qrubberband.html):

        void Widget::mouseMoveEvent(QMouseEvent *event)
        {
            // Here calculate the QRect the way you need it
            rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
        }
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        B 1 Reply Last reply 20 Feb 2024, 10:04
        2
        • J jsulm
          20 Feb 2024, 06:31

          @BwvB You could calculate the size so that it has the aspect ratio you want in mouseMoveEvent (see https://doc.qt.io/qt-6/qrubberband.html):

          void Widget::mouseMoveEvent(QMouseEvent *event)
          {
              // Here calculate the QRect the way you need it
              rubberBand->setGeometry(QRect(origin, event->pos()).normalized());
          }
          
          B Offline
          B Offline
          BwvB
          wrote on 20 Feb 2024, 10:04 last edited by
          #3

          @jsulm said in Drawing QRubberBands with variabe size, but fixed aspect ratio: how to do it?:

          rubberBand->setGeometry(QRect(origin, event->pos()).normalized());

          Thanks. It is indeed as easy as you indicated!
          Regards
          Bertwim

          1 Reply Last reply
          0
          • B BwvB has marked this topic as solved on 20 Feb 2024, 10:04

          1/3

          19 Feb 2024, 21:49

          • Login

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