Qt源码分析--QWidget(8)--Mouse and keyboard grabbing

59 篇文章 1 订阅
37 篇文章 1 订阅

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;
}

在OpenCV-Python中集成到QtQWidget窗口进行视频监控通常涉及到两个部分:Qt GUI管理和OpenCV视频处理。以下是一个简化的步骤: 1. **安装依赖**:首先确保你已经安装了`opencv-python`, `PyQt5`或`PySide2`(两者都支持Qt应用开发)。 2. **创建QWidget**:在Python文件中,初始化并创建一个QtQWidget作为主窗口,设置布局和其他UI元素。 ```python import sys from PyQt5.QtWidgets import QApplication, QWidget class VideoWindow(QWidget): def __init__(self): super().__init__() self.init_ui() def init_ui(self): # 设置窗口大小和标题 self.setGeometry(100, 100, 800, 600) self.setWindowTitle('OpenCV监控') # ... (其他UI设置) ``` 3. **添加OpenCV相机捕获**:在QWidget的槽函数中,创建一个OpenCV的VideoCapture对象,并在窗口上显示摄像头画面。 ```python from cv2 import.VideoCapture def show_video(self): cap = cv2.VideoCapture(0) # 使用默认摄像头,若有多台设备需要指定序号 while True: ret, frame = cap.read() if not ret: break # 转换BGR to QImage img_qt = QImage(frame.data, frame.shape[1], frame.shape[0], QImage.Format_RGB888) pixmap = QPixmap.fromImage(img_qt) # 显示QPixmap在窗口上 self.label.setPixmap(pixmap) self.update() # 将show_video绑定到一个按钮或其他触发事件上 button = QPushButton("Start Camera", self) button.clicked.connect(self.show_video) self.layout.addWidget(button) ``` 4. **运行应用程序**:在main函数中创建一个QApplication实例,然后启动窗口。 ```python if __name__ == "__main__": app = QApplication(sys.argv) video_window = VideoWindow() video_window.show() sys.exit(app.exec_()) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天进步2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值