1
votes

first, sorry for my poor english !

I want to make the cursor on the screen become invisible after it has stopped moving for 10s. I think this problem can be solved easily if there is a signal like positionChanged(QPoint lastPos , QPoint currentPos) existed,lastPos means the last position of the cursor (hot spot) of the primary screen in global screen coordinates, currentPos means the current position, this signal should be emitted once the cursor stopped moving .Note the Mouse move events here should occur even when a mouse button is not pressed down, i.e. there is mouse tracking with cursor.In Qt, mouse tracking could be enabled with QWidget::setMouseTracking(), however my problem is not restricted to Qt, it is system wide, I want to do this on Windows now ,anyone knows how to enable mouse tracking here ? any other solution is also welcome !

1

1 Answers

2
votes

You can use an event filter to see if a mouse moved:

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
  if (event->type() == QEvent::MouseMove)
  {
    QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    qDebug() << (QString("Moved! (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
  }
  return false;
}

and install it in your MainWindow or so:

qApp->installEventFilter(this);

Then, make a 10 second timer which gets reset when the mouse moves (and makes the cursor visible again). To make the mouse cursor vanish, you can call this when your timer runs out:

QApplication::setOverrideCursor(Qt::BlankCursor);

To make the cursor visible again, call:

QApplication::restoreOverrideCursor()