1.void grabMouse();
void QWidget::grabMouse()
{
grabMouseForWidget(this);
}
#ifndef QT_NO_CURSOR
static void grabMouseForWidget(QWidget *widget, const QCursor *cursor = 0)
#else
static void grabMouseForWidget(QWidget *widget)
#endif
{
if (qt_mouseGrb)
qt_mouseGrb->releaseMouse();
mouseGrabWithCursor = false;
if (QWindow *window = grabberWindow(widget)) {
#ifndef QT_NO_CURSOR
if (cursor) {
mouseGrabWithCursor = true;
QGuiApplication::setOverrideCursor(*cursor);
}
#endif // !QT_NO_CURSOR
window->setMouseGrabEnabled(true);
}
qt_mouseGrb = widget;
qt_pressGrab = 0;
}
qt_mouseGrb设置为widget,此widget接收所有鼠标事件,直到调用releaseMouse()函数。
2.void releaseMouse();
void QWidget::releaseMouse()
{
releaseMouseGrabOfWidget(this);
}
static void releaseMouseGrabOfWidget(QWidget *widget)
{
if (qt_mouseGrb == widget) {
if (QWindow *window = grabberWindow(widget)) {
#ifndef QT_NO_CURSOR
if (mouseGrabWithCursor) {
QGuiApplication::restoreOverrideCursor();
mouseGrabWithCursor = false;
}
#endif // !QT_NO_CURSOR
window->setMouseGrabEnabled(false);
}
}
qt_mouseGrb = 0;
}
3.void grabKeyboard();
void QWidget::grabKeyboard()
{
if (keyboardGrb)
keyboardGrb->releaseKeyboard();
if (QWindow *window = grabberWindow(this))
window->setKeyboardGrabEnabled(true);
keyboardGrb = this;
}
bool QWindow::setKeyboardGrabEnabled(bool grab)
{
Q_D(QWindow);
if (d->platformWindow)
return d->platformWindow->setKeyboardGrabEnabled(grab);
return false;
}
4.void releaseKeyboard();
void QWidget::releaseKeyboard()
{
if (keyboardGrb == this) {
if (QWindow *window = grabberWindow(this))
window->setKeyboardGrabEnabled(false);
keyboardGrb = 0;
}
}
static inline QWindow *grabberWindow(const QWidget *w)
{
QWindow *window = w->windowHandle();
if (!window)
if (const QWidget *nativeParent = w->nativeParentWidget())
window = nativeParent->windowHandle();
return window;
}
先获取的QWidget的窗口句柄,然后设置keyboardGrb=0;
5.static QWidget *mouseGrabber();
/*!
\fn QWidget *QWidget::mouseGrabber()
Returns the widget that is currently grabbing the mouse input.
If no widget in this application is currently grabbing the mouse,
\nullptr is returned.
\sa grabMouse(), keyboardGrabber()
*/
QWidget *QWidget::mouseGrabber()
{
if (qt_mouseGrb)
return qt_mouseGrb;
return qt_pressGrab;
}
6.static QWidget *keyboardGrabber();
/*!
\fn QWidget *QWidget::keyboardGrabber()
Returns the widget that is currently grabbing the keyboard input.
If no widget in this application is currently grabbing the
keyboard, \nullptr is returned.
\sa grabMouse(), mouseGrabber()
*/
QWidget *QWidget::keyboardGrabber()
{
return keyboardGrb;
}