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. Determine mouse press on dialog or child widget?
Forum Updated to NodeBB v4.3 + New Features

Determine mouse press on dialog or child widget?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 369 Views 2 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.
  • T Offline
    T Offline
    Taytoo
    wrote on last edited by
    #1

    I have a draggable dialog with bunch of labels on it. I only want to detect mouse events (down, move, release) on dialog (not on child widgets/labels). QLabel doesn't capture any mouse events resulting in parent/main window receiving those events.

    Is there a way to manually Hit-test dialog or have Qlabel widgets accept mouse events so that dialog doesn't receive those?

    1 Reply Last reply
    0
    • JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Taytoo
      There are several ways to accomplish this. Most involve either subclassing your child widgets or installing an event filter on each one. I'm thinking these would cause you hassle/a lot of work.

      One way I would propose which does not involve these is:

      /*virtual*/ void Dialog::mousePressEvent(QMouseEvent *mouseEvent) /*override*/
      {
          QWidget *w = QApplication::widgetAt(mouseEvent->globalPos());
          if (w != nullptr && w != this)
              qDebug() << "Dialog::mousePressEvent on some other widget" << w << w->objectName();
          else
              qDebug() << "Dialog::mousePressEvent on Dialog";
      }
      

      [Note thiis is Qt5. For Qt6 they may have changed to mouseEvent->globalPosition()?]

      You can also define an event filter just on the dialog to stop propagation of events on sub-widgets to the dialog, along the lines of:

      // this->installEventFilter(this);
      bool Dialog::eventFilter(QObject *object, QEvent *event)
      {
          QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(event);
          if (mouseEvent)
          {
              QWidget *w = QApplication::widgetAt(mouseEvent->globalPos());
              if (w != nullptr && w != this)
              {
                  qDebug() << "Dialog::eventFilter on some other widget" << w << w->objectName();
                  return true;
              }
              else
                  qDebug() << "Dialog::eventFilter on Dialog";
          }
          return false;
      }
      

      You can use this to see whether a mouse event arriving at the QDialog level was actually over some widget inside it. It uses QApplication::widgetAt() which is "slow", but may be fine unless you have very many widgets.

      I don't know whether someone else can propose a neater way?

      M 1 Reply Last reply
      0
      • JonBJ JonB

        @Taytoo
        There are several ways to accomplish this. Most involve either subclassing your child widgets or installing an event filter on each one. I'm thinking these would cause you hassle/a lot of work.

        One way I would propose which does not involve these is:

        /*virtual*/ void Dialog::mousePressEvent(QMouseEvent *mouseEvent) /*override*/
        {
            QWidget *w = QApplication::widgetAt(mouseEvent->globalPos());
            if (w != nullptr && w != this)
                qDebug() << "Dialog::mousePressEvent on some other widget" << w << w->objectName();
            else
                qDebug() << "Dialog::mousePressEvent on Dialog";
        }
        

        [Note thiis is Qt5. For Qt6 they may have changed to mouseEvent->globalPosition()?]

        You can also define an event filter just on the dialog to stop propagation of events on sub-widgets to the dialog, along the lines of:

        // this->installEventFilter(this);
        bool Dialog::eventFilter(QObject *object, QEvent *event)
        {
            QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent *>(event);
            if (mouseEvent)
            {
                QWidget *w = QApplication::widgetAt(mouseEvent->globalPos());
                if (w != nullptr && w != this)
                {
                    qDebug() << "Dialog::eventFilter on some other widget" << w << w->objectName();
                    return true;
                }
                else
                    qDebug() << "Dialog::eventFilter on Dialog";
            }
            return false;
        }
        

        You can use this to see whether a mouse event arriving at the QDialog level was actually over some widget inside it. It uses QApplication::widgetAt() which is "slow", but may be fine unless you have very many widgets.

        I don't know whether someone else can propose a neater way?

        M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        @JonB
        And what about:
        QWidget *QWidget::childAt(int x, int y)
        ?

        JonBJ 1 Reply Last reply
        0
        • M mpergand

          @JonB
          And what about:
          QWidget *QWidget::childAt(int x, int y)
          ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @mpergand
          Doesn't do descendent grandchildren etc. ? So by the time you do it recursively what advantages does it offer? And this is only a replacement for the QApplication::widgetAt(), hoping to make it faster, but you would follow one of the approaches above?

          T 1 Reply Last reply
          0
          • T Taytoo has marked this topic as solved on
          • JonBJ JonB

            @mpergand
            Doesn't do descendent grandchildren etc. ? So by the time you do it recursively what advantages does it offer? And this is only a replacement for the QApplication::widgetAt(), hoping to make it faster, but you would follow one of the approaches above?

            T Offline
            T Offline
            Taytoo
            wrote on last edited by
            #5

            @JonB I tried the eventfilter approach. The problem with it is that it layouts and spacers receive input events as well, and its quite a lot of controls that I have to filter through. Is it possible to disable mouse input being sent to all layouts and spacers?

            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