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. Get mouse event for widget with trasparent background
Forum Updated to NodeBB v4.3 + New Features

Get mouse event for widget with trasparent background

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 4.1k 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.
  • U Offline
    U Offline
    Unick
    wrote on 20 Jan 2017, 14:33 last edited by
    #1

    Hello.

    Is there a way to get mouse event in widget with transparent background, if mouse cursor is under transparent part of widgets.

    I have main Qt window, this window has one child, which is third-party non-Qt window. Above i have panel (QWindow), which is owned by main window. This panel has Qt::Window flag and parent is main widnow.

    My panel have attributes:

    setAttribute(Qt::WA_NoSystemBackground, true);
    setAttribute(Qt::WA_TranslucentBackground, true);
    

    On panel i have non-trasparent child widgets. I able to get mouse events only if mouse cursor is under child widget. I know if my transparent panel did not get event, a widget under it get event. But under my widget third-party window is located. I cannot modify his code. My goal to intercept mouse event and implement drag&drop and third-party window should not get mouse event in this case.

    I tested under Windows Qt 5.3.

    Under mac all mouse events under transparent part of panel i can get in main window mouse event function. It does not work the same under Windows.

    1 Reply Last reply
    0
    • U Offline
      U Offline
      Unick
      wrote on 24 Jan 2017, 16:44 last edited by
      #6

      I found one solution, which works for me. But it looks like a hack :)

      I added child QWidget to my panel. I added QGraphicsOpacityEffect for this QWidget and set opacity value to 0.01. As the results widget is invisible, but my panel is started to get mouse events. One remark: QWidget should fill full panel.

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 21 Jan 2017, 08:30 last edited by
        #2

        Hi
        You can have a go with event filter and see what flows around of events.
        http://doc.qt.io/qt-5/eventsandfilters.html

        bool MainView::eventFilter(QObject *obj, QEvent *event)
        {
        if (event->type() == QEvent::MouseButtonPress)
          {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            int mx=mouseEvent->pos().x(),my=mouseEvent->pos().y();
        
            qDebug() << QString::number(mx)<<" "<<QString::number(my);
        ...
          }
        }
        
        1 Reply Last reply
        0
        • U Offline
          U Offline
          Unick
          wrote on 23 Jan 2017, 12:51 last edited by
          #3

          You can have a go with event filter and see what flows around of events.

          Unfortunately it does not work. Also i tried to use nativeEvent method, but it does not work too.

          M 1 Reply Last reply 23 Jan 2017, 15:24
          0
          • U Unick
            23 Jan 2017, 12:51

            You can have a go with event filter and see what flows around of events.

            Unfortunately it does not work. Also i tried to use nativeEvent method, but it does not work too.

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 23 Jan 2017, 15:24 last edited by
            #4

            @Unick
            Ok but what did you learn?
            I never tested what WA_TranslucentBackground will do to the event flow but
            an event filer would cast some light over that.
            So such windows do not get any mouse events?

            1 Reply Last reply
            0
            • U Offline
              U Offline
              Unick
              wrote on 23 Jan 2017, 16:17 last edited by
              #5

              Looks like my third-party window gets mouse event first and process it. And event filter is not called for mouse events for transparent parts of my panel.

              I learned MSDN and looks like it is feature of Windows: [https://msdn.microsoft.com/en-us/library/ms997507.aspx#layerwin_topic2b](link url)

              Hit testing of a layered window is based on the shape and transparency of the window. This means that the areas of the window that are color-keyed or whose alpha value is zero will let the mouse messages through.
              If the layered window has the WS_EX_TRANSPARENT extended window style, the shape of the layered window will be ignored and the mouse events will be passed to the other windows underneath the layered window.

              But i will investigate more and post message if i find solution. Maybe i can use event hook.

              1 Reply Last reply
              1
              • U Offline
                U Offline
                Unick
                wrote on 24 Jan 2017, 16:44 last edited by
                #6

                I found one solution, which works for me. But it looks like a hack :)

                I added child QWidget to my panel. I added QGraphicsOpacityEffect for this QWidget and set opacity value to 0.01. As the results widget is invisible, but my panel is started to get mouse events. One remark: QWidget should fill full panel.

                1 Reply Last reply
                1

                1/6

                20 Jan 2017, 14:33

                • Login

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