Qt 4.4:禁用的小部件接收鼠标事件 [英] Qt 4.4: disabled widgets receiving mouse events

查看:271
本文介绍了Qt 4.4:禁用的小部件接收鼠标事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所示,禁用的小部件是否可以接收鼠标事件?

As the title suggests, is there a way for a disabled widget to receive mouse events?

我正在使用QWidget::setEnabled()来更改小部件的外观,但我仍想接收其鼠标事件.在此先感谢:)

I'm using QWidget::setEnabled() for changing the appearance of widgets but I still want to receive their mouse events. Thanks in advance :)

推荐答案

您可以在相关小部件上使用事件过滤器来执行此操作.请参见 QObject :: eventFilter().您的实现可能看起来像这样:

You can do this with an event filter on the widget in question. See QObject::eventFilter(). Your implementation might look something like this:

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (ui->pushButton)
    {
        if (event->type() == QEvent::MouseButtonRelease)
        {
            qDebug() << "mouse button";
            return true;
        } else
        {
            return false;
        }
    } else
    {
        // pass the event on to the parent class
        return QMainWindow::eventFilter(obj, event);
    }
}

即使该按钮被禁用,这也将起作用.

This will work even if the button is disabled.

这篇关于Qt 4.4:禁用的小部件接收鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆