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. Accept/reject focus coming by mouse click, based on coordinates
Forum Updated to NodeBB v4.3 + New Features

Accept/reject focus coming by mouse click, based on coordinates

Scheduled Pinned Locked Moved Solved General and Desktop
widgetfocusmouse
11 Posts 3 Posters 3.2k Views 3 Watching
  • 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
    stryga42
    wrote on last edited by
    #2

    Just a guess, not a proven solution: Did you try to install an event filter (see QObject::eventFilter) on your widget? You could look for QEvent::FocusIn, calculate if the click was inside your visual boundary and accept or reject accordingly. But honestly, I'm not sure if this works better than ignoring the QWidget::focusInEvent...

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sommerluk
      wrote on last edited by
      #3

      @stryga42 Thanks for the suggestion.

      I’ve tried that now, but the result is the same as with reimplementing event() or focusInEvent(): It doesn’t prevent that the focus is assigned. It seems the focus is assigned anyway, and the event just informs the widget about that, without a possibility to cancel it?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        stryga42
        wrote on last edited by
        #4

        Maybe you are right that the focusIn is "too late".
        What about combining the eventFilter approach (the filter is notified definitely before the target object gets the event) with your current mouse click approach? Use FocusPolicy::ClickFocus and suppress the MouseButtonPress event in the event filter if the mouse position is not truly "inside".

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sommerluk
          wrote on last edited by
          #5

          I’ve just tied that. Same result: Even when filtering all mouse and focus events, the focus itself gets assigned nevertheless.

          1 Reply Last reply
          0
          • jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #6
            #include <QApplication>
            #include <QWidget>
            #include <QPainter>
            #include <QLayout>
            #include <QMouseEvent>
            #include <QPixmap>
            #include <QImage>
            
            class Widget : public QWidget
            {
                Q_OBJECT
            
            protected:
                void paintEvent([[maybe_unused]] QPaintEvent *event) override
                {
                    QPainter painter(this);
                    painter.drawRect(this->rect());
                    painter.setBrush(this->hasFocus() ? Qt::red : Qt::blue);
                    painter.drawEllipse(this->rect());
                }
            
                void mousePressEvent(QMouseEvent *event) override
                {
                    QPixmap pixmap = this->grab(QRect(event->x(), event->y(), 1, 1));
                    auto pixel = pixmap.toImage().pixel(0, 0);
                    if (pixel == 0xFF0000FF)
                        this->setFocus(Qt::MouseFocusReason);
                    QWidget::mousePressEvent(event);
                }
            };
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                QWidget topLevel;
                topLevel.setGeometry(0, 0, 200, 100);
                QHBoxLayout layout(&topLevel);
                Widget w1, w2;
                layout.addWidget(&w1);
                layout.addWidget(&w2);
                topLevel.show();
                return a.exec();
            }
            
            #include "main.moc"
            

            Asking a question about code? http://eel.is/iso-c++/testcase/

            S 1 Reply Last reply
            0
            • jeremy_kJ jeremy_k
              #include <QApplication>
              #include <QWidget>
              #include <QPainter>
              #include <QLayout>
              #include <QMouseEvent>
              #include <QPixmap>
              #include <QImage>
              
              class Widget : public QWidget
              {
                  Q_OBJECT
              
              protected:
                  void paintEvent([[maybe_unused]] QPaintEvent *event) override
                  {
                      QPainter painter(this);
                      painter.drawRect(this->rect());
                      painter.setBrush(this->hasFocus() ? Qt::red : Qt::blue);
                      painter.drawEllipse(this->rect());
                  }
              
                  void mousePressEvent(QMouseEvent *event) override
                  {
                      QPixmap pixmap = this->grab(QRect(event->x(), event->y(), 1, 1));
                      auto pixel = pixmap.toImage().pixel(0, 0);
                      if (pixel == 0xFF0000FF)
                          this->setFocus(Qt::MouseFocusReason);
                      QWidget::mousePressEvent(event);
                  }
              };
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  QWidget topLevel;
                  topLevel.setGeometry(0, 0, 200, 100);
                  QHBoxLayout layout(&topLevel);
                  Widget w1, w2;
                  layout.addWidget(&w1);
                  layout.addWidget(&w2);
                  topLevel.show();
                  return a.exec();
              }
              
              #include "main.moc"
              
              S Offline
              S Offline
              sommerluk
              wrote on last edited by
              #7

              @jeremy_k

              Thanks for the proposition.

              However, this code would just allow to capture the mouse focus within the circle. This is what my code is doing yet. The question is how to prevent that the widget gets focus when clicked outside the circle, but inside the rectangle, when QWidget::setFocusPolicy(Qt::FocusPolicy::ClickFocus) had been called.

              1 Reply Last reply
              0
              • jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by
                #8

                I'm not sure what is meant by focus, if not the return value of QWidget::hasFocus().

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sommerluk
                  wrote on last edited by
                  #9

                  Indeed.

                  Now, my problem is not how to reimplement mousePressEvent(). I've done that yet, just as you propose, and it works. The actual implementation is here: https://sommerluk.github.io/perceptualcolor/chromahuediagram_8cpp_source.html#l00137

                  It does quite exactly the same thing.

                  My problem is that this feels a little bit like a hack. This implementation will get focus by mouse click even when the widget’s focusPolicy is Qt::FocusPolicy::TabFocus. On the other hand, when focusPolicy is Qt::FocusPolicy::ClickFocus, it will also gather the focus by clicks outside the circle, but within the widget rectangle. That feels inconsistent…

                  jeremy_kJ 1 Reply Last reply
                  0
                  • S sommerluk

                    Indeed.

                    Now, my problem is not how to reimplement mousePressEvent(). I've done that yet, just as you propose, and it works. The actual implementation is here: https://sommerluk.github.io/perceptualcolor/chromahuediagram_8cpp_source.html#l00137

                    It does quite exactly the same thing.

                    My problem is that this feels a little bit like a hack. This implementation will get focus by mouse click even when the widget’s focusPolicy is Qt::FocusPolicy::TabFocus. On the other hand, when focusPolicy is Qt::FocusPolicy::ClickFocus, it will also gather the focus by clicks outside the circle, but within the widget rectangle. That feels inconsistent…

                    jeremy_kJ Offline
                    jeremy_kJ Offline
                    jeremy_k
                    wrote on last edited by
                    #10

                    @sommerluk said in Accept/reject focus coming by mouse click, based on coordinates:

                    My problem is that this feels a little bit like a hack. This implementation will get focus by mouse click even when the widget’s focusPolicy is Qt::FocusPolicy::TabFocus.

                    So check the widget's focus policy and call or don't call setFocus() according to your needs. It looks like adding a new focus policy reason that fits in the enum won't be rejected as of 5.15.0, although there's no explicit allowance for it in the API. Adding a separate attribute of some sort is less likely to conflict with Qt API changes.

                    On the other hand, when focusPolicy is Qt::FocusPolicy::ClickFocus, it will also gather the focus by clicks outside the circle, but within the widget rectangle. That feels inconsistent…

                    The documentation says Qt::ClickFocus 0x2 the widget accepts focus by clicking.
                    QWidgets are rectangular. The unpainted or differently painted space within the rectangle still belongs to that widget as far as the framework is concerned.

                    Have a look at the implementation. Determining if the widget gets focus due to ClickFocus happens before the click is delivered to the widget, based on its geometry, focus policy, and focus proxy.
                    https://code.woboq.org/qt5/qtbase/src/widgets/kernel/qapplication.cpp.html#3125

                    For what it's worth, other people have asked for the same functionality.
                    https://bugreports.qt.io/browse/QTBUG-598

                    Asking a question about code? http://eel.is/iso-c++/testcase/

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      sommerluk
                      wrote on last edited by
                      #11

                      Okay, got it: Relevant code resides in QApplication and cannot be changed by subclassing QWidget. There’s no way around Qt::FocusPolicy but there is the possibility to add other attributes.

                      Thanks a lot!

                      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