QT鼠标事件处理问题

发布于 2024-09-19 01:33:12 字数 486 浏览 0 评论 0原文

大家好,

如图所示

http://i51.tinypic.com/2r56s1j.jpg

我有一个扩展的 QWidget QScrollBar 内的对象(绘制单元格图像和一些计数数据)。 用户可以使用鼠标滚轮放大/缩小图像(QWidget 大小根据 QImage 的缩放大小而改变)。

我通过在 QWidget 中实现侦听器方法来处理事件(mouseMoveEvent()、wheelEvent() 等)。 我的问题是,当鼠标指针位于 QWidget 上时,我只能执行缩放(和其他事件)。 如果鼠标点位于 QScrollBar 上(图像中的灰色区域),则这些事件将由 QScrollBar 消耗。

任何提示,

[编辑] 抱歉,我指的是 QScrollArea ,而不是 QScrollBar 。

谢谢, 乌曼加

Greetings all,

As seen in the picture

http://i51.tinypic.com/2r56s1j.jpg

I have an extended QWidget object (which draws the cell images and some countour data) inside a QScrollBar.
User can zoom in/out the Image (QWidget size is changed according to the zoomed size of the QImage ) using mouse wheel.

I process the events (mouseMoveEvent(),wheelEvent()..etc) by implementing the listener methods in QWidget.
My problem is ,I can only perform zooming (and other events) when the mouse pointer is over the QWidget.
If the mouse point is over the QScrollBar (the gray area in the image) ,those events are consumed by the QScroolBar.

Any tips,

[Edit] Sorry I was refering to QScrollArea , not QScrollBar.

thanks,
umanga

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

去了角落 2024-09-26 01:33:12

我不确定您是否希望滚轮用于缩放图像,或者您是否希望滚轮在图像小于滚动区域视口时控制缩放,然后使用当图像大于滚动区域视口时,滚轮进行滚动。在任何一种情况下,您都应该能够通过以下方式自定义轮子的处理方式:

由于我还没有实际尝试过这个,所以我不确定它是否会起作用。希望如果您安装事件过滤器并设置忽略该事件,该事件仍将传播回您的图像小部件。这将允许您在图像小部件中保持当前的鼠标处理不变。

bool YourImageWidget::eventFilter(QObject *obj, QEvent *event)
{
    if((obj == scrollAreaPointer) && (event->type() == QEvent::Wheel))
    {
        if(!scrollAreaShouldHandleWheel)
        {
            event->ignore();
        }
    }
    return false; // always pass the event back to the scroll area
}

scrollAreaShouldHandleWheel 标志是一个布尔值,您可以根据是否希望滚动区域处理滚轮事件从图像小部件中设置。

在代码中的某个位置,您将安装图像小部件作为滚动区域的事件过滤器。

scrollArea->installEventFilter(imageWidget);

如果这不起作用,您始终可以使用此过滤器来确保您的小部件获取事件并直接处理它,然后返回 true,以便滚动区域将无法接收该事件。

I'm uncertain if you want the scroll wheel to only ever be used for zooming the image or if you want the scroll wheel to control zooming when the image is smaller than the scroll area viewport and then use the scroll wheel to do scrolling when the image is larger than the scroll area viewport. In either case, you should be able to customize how the wheel is handled with the following:

Since I've not actually tried this one, I'm not sure if it will work. The hope is that if you install an event filter and set ignore on the event, the event will still be propagated back to your image widget. This will allow you to leave your current mouse handling in the image widget intact.

bool YourImageWidget::eventFilter(QObject *obj, QEvent *event)
{
    if((obj == scrollAreaPointer) && (event->type() == QEvent::Wheel))
    {
        if(!scrollAreaShouldHandleWheel)
        {
            event->ignore();
        }
    }
    return false; // always pass the event back to the scroll area
}

The scrollAreaShouldHandleWheel flag is a boolean you would set from your image widget based on whether or not you want the scroll area to handle the wheel events.

Somewhere in your code you would install your image widget as an event filter for the scrollarea.

scrollArea->installEventFilter(imageWidget);

If this doesn't work, you can always use this filter to make sure your widget gets the event and handle it directly and then return true so that the scroll area won't be able to receive the event.

殤城〤 2024-09-26 01:33:12

我建议您使用 QGraphicsScene 和 QGraphicsView。图形框架已经提供了许多有用的功能(包括视口转换)。 QGraphicsView是一个滚动区域。

I recommend you use QGraphicsScene and QGraphicsView. The graphics framework already provides a lot of useful features (including viewport transformation). And the QGraphicsView is a scroll area.

薄凉少年不暖心 2024-09-26 01:33:12

您是否为Qwidget(即您显示图像的那个)完成了grabMouse()

have you done grabMouse() for Qwidget i.e for the one which you display image?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文