简体   繁体   中英

Qt get mouse events outside of the application window

First I'm not certain this is even possible without some sort of hacking of X.11 input but the discussions i'd seen online made me think it was possible.

Allow me to explain what I hope to do. I want a Qt application which will most likely just be a small window that sides on the screen sort of like a widget. The application does nothing until the user drags another application window over the top of it. The way I was hoping to detect this was to track the mouse and see if the left click is down and the mouse is over the Qt window and Qt is not the active window then do some action. However currently I havent been able to get mouse events when my Qt application is not the active window. I think some of these posts I linked refer to 'window' as a QWindow inside the QApp.

What I mean by window however is a X.11 Window, any application opened in X. My screenshots I hope highlight my current plight. I've attached my code as well and am happy to take any suggestions. Any other hacks that are known to help me achieve this I would also appreciate being informed of.

状态1

The red shows where my cursor has clicked, and the mouse event is recorded outside of the Qt window. This was triggered by the 'FocusOut' event however and is the last event I have managed to detect.

州2

As we can see in the console, the mouse has moved but no events are caught. I really want to detect when the mouse crosses over onto the position the Qt App Window is at regardless of whether it is on top of another window or not.

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    {
      if (event->type() == QEvent::MouseMove)
      {
    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
    qDebug() << QString::number(mouseEvent->pos().x());
    qDebug() << QString::number(mouseEvent->pos().y());
  }
  if (event->type() == QEvent::FocusOut)
 {
     QFocusEvent *focusEvent = static_cast<QFocusEvent*>(event);
     focusEvent->accept();
     qDebug()<<"event Filter Mouse Move111"<<QCursor::pos();
 }
  return false;
}


void MainWindow::initWindow()
{
    //Makes the window frameless and always on top
    //setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);
    //Makes the window transparent
    //setAttribute(Qt::WA_TranslucentBackground);


    //Allows 'mouseMoved' events to be sent, not sure yet if this will be useful, I think we want mouseDragged
    setMouseTracking(true);
    grabMouse();

    //setup this as an event filter for mouse events
    qApp->installEventFilter(this);
}

Alright heres how I solved this problem. The event system in Qt, any application I assume, won't register events when the window is not active. However the process is obviously still running so data you can access while the window is active you can access whilst the window is no longer active.

Use a timed poll method to get the mouse position every n seconds

//Method used to hopefully track the mouse regardless of whether or not it is inside the active window
void MainWindow::pollMouse(unsigned long sec)
{
    //Loop forever
    while ( true )
    {
        QPoint mouseLoc = QCursor::pos();
        qDebug() << "Mouse position global: x,y"  << mouseLoc.x() << mouseLoc.y();

        QThread::sleep(sec);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM