• MouseMoveEvent为了不太耗资源在默认状态下是要鼠标按下才能捕捉到。要想鼠标不按下时的移动也能捕捉到,需要setMouseTracking(true)


    最近用Qt软件界面,需要用到mouseMoveEvent,研究了下,发现些问题,分享一下。
    在Qt中要捕捉鼠标移动事件需要重写MouseMoveEvent,但是MouseMoveEvent为了不太耗资源在默认状态下是要鼠标按下才能捕捉到。要想鼠标不按下时的移动也能捕捉到,需要setMouseTracking(true)。


    bool mouseTracking
    这个属性保存的是窗口部件跟踪鼠标是否生效。

    如果鼠标跟踪失效(默认),当鼠标被移动的时候只有在至少一个鼠标按键被按下时,这个窗口部件才会接收鼠标移动事件。
    如果鼠标跟踪生效,如果没有按键被按下,这个窗口部件也会接收鼠标移动事件。
    QWidget中使用是没有问题的,但是,对于QMainWindow即使使用了setMouseTracking(true)依然无法捕捉到鼠标没有按下的移动,只有在鼠标按下是才能捕捉。
    解决办法:要先把QMainWindow的CentrolWidget使用setMouseTracking(true)开启移动监视。然后在把QMainWindow的setMouseTracking(true)开启监视。之后就一切正常了。
    原因:CentrolWIdget是QMainWindow的子类,你如果在子类上响应鼠标事件,只会触发子类的mouseMoveEvent,根据C++继承和重载的原理,所以子类也要setMouseTracking(true); 所以如果你想响应鼠标事件的控件被某个父控件包含,则该控件及其父控件或容器也需要setMouseTracking(true);

    ui->centralWidget->setMouseTracking(true);

    setMouseTracking(true); //这是激活整个窗体的鼠标追踪
    ui->pBtnMenu->setMouseTracking(true); //进入某个按钮时,鼠标追踪属性失效,因此我们也需要激活该按钮的鼠标追踪功能
    ui->pBtnTest->setMouseTracking(true);

    //然后再实现mouseMoveEvent()事件

    void MainWindow::mouseMoveEvent(QMouseEvent *e)
    {
    qDebug()<<"mouse move ";
    e->accept();
    if(enterBtn(e->pos(),ui->pBtnMenu))
    //Qlab_context->setText("这是第一个按钮");
    qDebug()<<"menu";
    if(enterBtn(e->pos(),ui->pBtnTest))
    qDebug()<<"test";
    //Qlab_context->setText("这是第二个按钮");
    }
    //这里我使用另一个函数来完成判断鼠标是否在一个按钮区域内,如果在区域内只返回真,否则返回假
    bool MainWindow::enterBtn(QPoint pp, QPushButton *btn)
    {
    int height = btn->height();
    int width = btn->width();
    QPoint btnMinPos = btn->pos();
    QPoint btnMaxPos = btn->pos();
    btnMaxPos.setX(btn->pos().x()+width);
    btnMaxPos.setY(btn->pos().y()+height);
    if(pp.x() >= btnMinPos.x() && pp.y() >= btnMinPos.y()
    && pp.x() <= btnMaxPos.x() && pp.y() <= btnMaxPos.y())
    return true;
    else
    return false;
    }

  • 相关阅读:
    oc 阿拉伯数字转中文数字
    std::vector<char> 转 const char
    cocos2d-x类型转换(CCstring int string char UTF-8互转)
    js Array.prototype.join.call(arguments,",") 理解
    去除console.log
    This renderer process has Node.js integration enabled and attempted to load remote content from Electron的警告
    [vue/no-parsing-error] Parsing error: x-invalid-end-tag 提示报错
    node 调用cmd 乱码
    nodeIntegration: true Electron的警告
    take it away `electron-builder` Electron打包失败的问题
  • 原文地址:https://www.cnblogs.com/findumars/p/11011387.html
Copyright © 2020-2023  润新知