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. Check if cursor is near a widget's edges
Forum Updated to NodeBB v4.3 + New Features

Check if cursor is near a widget's edges

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 261 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.
  • S Offline
    S Offline
    swurl
    wrote on last edited by
    #1

    What would be the best way to go around checking if the cursor is near a widget's edges and determining which direction it is, without the use of massive if-blocks?

    C 1 Reply Last reply
    0
    • S swurl

      What would be the best way to go around checking if the cursor is near a widget's edges and determining which direction it is, without the use of massive if-blocks?

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      This is one way:

          void mouseMoveEvent(QMouseEvent *event) {
              QRect r = rect() - QMargins(20, 20, 20, 20);
              // ^^^ assumes the client area is big enough to shrink 40 pixels.  Example only.
              const bool nearEdge = !r.contains(event->pos());
              // ^^^ near any edge
              const bool nearEdgeL = nearEdge && (event->pos().x() < r.left());
              const bool nearEdgeR = nearEdge && (event->pos().x() > r.right());
              const bool nearEdgeT = nearEdge && (event->pos().y() < r.top());
              const bool nearEdgeB = nearEdge && (event->pos().y() > r.bottom());
              // ^^^ More than one of these can true at the same time in corners
              qDebug() << nearEdge << nearEdgeL << nearEdgeR << nearEdgeT << nearEdgeB;
              QWidget::mouseMoveEvent(event);
          }
      
      S 1 Reply Last reply
      3
      • C ChrisW67

        This is one way:

            void mouseMoveEvent(QMouseEvent *event) {
                QRect r = rect() - QMargins(20, 20, 20, 20);
                // ^^^ assumes the client area is big enough to shrink 40 pixels.  Example only.
                const bool nearEdge = !r.contains(event->pos());
                // ^^^ near any edge
                const bool nearEdgeL = nearEdge && (event->pos().x() < r.left());
                const bool nearEdgeR = nearEdge && (event->pos().x() > r.right());
                const bool nearEdgeT = nearEdge && (event->pos().y() < r.top());
                const bool nearEdgeB = nearEdge && (event->pos().y() > r.bottom());
                // ^^^ More than one of these can true at the same time in corners
                qDebug() << nearEdge << nearEdgeL << nearEdgeR << nearEdgeT << nearEdgeB;
                QWidget::mouseMoveEvent(event);
            }
        
        S Offline
        S Offline
        swurl
        wrote on last edited by
        #3

        @ChrisW67 I figured this out on my own, but that's essentially what I came up with, additionally creating flags for the resize direction. Thanks!

        1 Reply Last reply
        0
        • S swurl has marked this topic as solved on

        • Login

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