title: How can I selectively make QWidget accept focus from a mouse click?
body: <p>I'm creating an application that provides a visual representation of nodes and lines connecting them together. Both nodes and lines are represented by custom QWidgets, WidgetNode and WidgetLine, say.</p>
  
  <p>I've implemented WidgetLine as a transparent widget that is large enough to contain the start and end point of the line, and a custom function to draw the line itself.</p>
  
  <p>I would like it so that if the user clicks on or right next to the line then the WidgetLine receives focus, but if they click further away from the line (but still over the rectangular area covered by WidgetLine's geometry) then the click is completely ignored by WidgetLine and passed on to the widget below.</p>
  
  <p>I first tried doing this with a custom <code>focusInEvent()</code> function on WidgetLine, but found the mouse clicks weren't propagating below. I then tried setting the focus policy to <code>Qt::NoFocus</code> and using a custom <code>mousePressEvent()</code> using <code>setFocus()</code> to manually set the focus when appropriate, but the mouse events are still not being propagated to widgets above even when I call <code>ignore()</code> on them.</p>
  
  <p>Finally, I've tried installing an event filter to reject mouse events, with this event filter function</p>
  
  <pre><code>bool WidgetLineFilter::eventFilter(QObject* object, QEvent* event)
  {
      assert(object == mCord);
      if (event-&gt;type() == QEvent::MouseButtonPress)
      {
          QMouseEvent* e = dynamic_cast&lt;QMouseEvent*&gt;(event);
          assert(e);
          if (e)
          {
              QPoint mouseRelativeToParent = mCord-&gt;mapToParent(e-&gt;pos());
              // calculate distance of mouse click to patch cord
              QLineF line(mCord-&gt;line());
              float distance = distanceFromPointToLine(QVector2D(line.p1()), QVector2D(line.p2()), QVector2D(mouseRelativeToParent));
              qDebug() &lt;&lt; distance;
              const float distanceThreshold = 2.f;
              if (distance &lt; distanceThreshold)
              {
                  qDebug() &lt;&lt; "consuming mouse click for focus";
                  mCord-&gt;setFocus(Qt::MouseFocusReason);
                  return true;
              }
              else{
                  qDebug() &lt;&lt; "mousepressevent too far for focus";
                  return QObject::eventFilter(object, event);
              }
          }
      }
      return false;
  }
  </code></pre>
  
  <p>But this still does not propagate the mouse events to the parent on the "mousepressevent too far for focus" case. I've also tried returning false and true from here, and calling <code>ignore</code> on <code>e</code> but the widgets below are not receiving the click.</p>
  
  <p>(NB the above approaches do work in the sense that WidgetLine only gets focus at the right time, it's just the widgets below that aren't receiving the press events when it doesn't get focus.)</p>
  
  <p>Any ideas on how to fix this?</p>