简体   繁体   English

如何在Qt环境中将鼠标光标限制为矩形(在Ubuntu 12.04中,不是Windows)

[英]How to limit mouse cursor into a rectangle in Qt environment (in Ubuntu 12.04, not Windows)

there is a question confuse me a few days, that is how can I limit mouse cursor into a rectangle in Qt environment? 有一个问题使我困惑了几天,那就是如何在Qt环境中将鼠标光标限制在一个矩形内? And my OS is ubuntu 12.04, so Windows API ClipCursor() doesn't work. 我的操作系统是ubuntu 12.04,因此Windows API ClipCursor()无法正常工作。 thank you very much. 非常感谢你。

this involving QGraphicsItem::itemChange() . 这涉及QGraphicsItem::itemChange() If you have an item which you want to restrict to a certain area then reimplement itemChange() for that item and monitor QGraphicsItem::ItemPositionHasChanged changes to see whether the items wants to be placed outside your area of interest and prevent that by returning a position from inside that area. 如果您有要限制在某个区域内的项目,请对该项目重新实现itemChange()并监视QGraphicsItem::ItemPositionHasChanged更改,以查看该项目是否要放置在您感兴趣的区域之外,并通过返回位置来防止从该区域内部。 for example: 例如:

    QVariant QGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    switch (change) {
    case ItemPositionHasChanged:
        if(x() < -200 || y() < -200 || x() > 200 || y() > 200)
            setPos(0, 0);
        graph->itemMoved();
        break;
    default:
        break;
    };

    return QGraphicsItem::itemChange(change, value);
}

If you don't have an item but rather want to force a user to click within the area then hide the mouse pointer (by setting it to a blank shape), create a dedicated item that will serve as a cursor, optionally grab the mouse to that shape and then use itemChange() like I described earlier. 如果您没有项目,而是想强迫用户在该区域内单击,然后隐藏鼠标指针(将其设置为空白形状),则创建一个专用项目作为光标,或者选择抓住鼠标调整为该形状,然后使用如我之前所述的itemChange()

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

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