简体   繁体   English

使光标停止后在屏幕上不可见

[英]make the cursor on the screen invisible after it has stopped

first, sorry for my poor english ! 首先,对不起我的英语不好!

I want to make the cursor on the screen become invisible after it has stopped moving for 10s. 我想让屏幕上的光标停止移动10秒后变得不可见。 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, ie 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 ? 我认为如果存在诸如positionChanged(QPoint lastPos,QPoint currentPos)之类的信号,可以轻松解决此问题,lastPos表示主屏幕的光标(热点)在全局屏幕坐标中的最后位置,currentPos表示当前位置,此信号应在光标停止移动后发出。注意,即使没有按下鼠标按钮,此处也会发生鼠标移动事件,即使用鼠标进行跟踪。在Qt中,可以使用QWidget启用鼠标跟踪: setMouseTracking(),但是我的问题不仅限于Qt,它是系统范围的,我现在想在Windows上执行此操作,有人知道如何在此处启用鼠标跟踪吗? any other solution is also welcome ! 任何其他解决方案也欢迎!

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: 并将其安装在您的MainWindow中:

qApp->installEventFilter(this);

Then, make a 10 second timer which gets reset when the mouse moves (and makes the cursor visible again). 然后,设置一个10秒钟的计时器,当鼠标移动时该计时器将重置(并使光标再次可见)。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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