鼠标按下事件里面的Qpushbutton在QT [英] mouse press event inside Qpushbutton in QT

查看:320
本文介绍了鼠标按下事件里面的Qpushbutton在QT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用qt creator来创建计算器。有一个默认的mainwindow窗体有gui。在mainwindow.cpp中,我设置了setMouseTracking(true),点击时获取鼠标坐标。但是,只有当在主窗口窗体中的任何按钮外部进行点击时,我才能获得鼠标坐标。我想这有一个事实,当一个按钮被点击,已经生成某种类型的鼠标事件来处理该点击。
我需要获取点击按钮内的坐标。

I am using qt creator to make a calculator. There is a default mainwindow form that has the gui. And in mainwindow.cpp, i have set setMouseTracking(true) , to get mouse coordinates when clicked. But i get the mouse coordinates only when a click is made outside any button in the mainwindow form. I guess this has something to do with the fact that when a button is clicked, already a mouse event of some sort is being generated to handle that click. I need to get the coordinates when clicked inside a button. How do I do that?

推荐答案

第一件事:这是一个坏主意:)

First thing first: this is a bad idea : )

您要做的是捕获发送到子对象的事件。一种方法是重新实现子对象的事件处理程序。但是当你使用设计器来构造你的界面,这变得有点困难。您必须将所有窗口小部件提升为其重新实现的版本。

What you are trying to do is to capture an event that is sent to a child object. A way to do this is to re-implement event handler of the children objects. But when you use the designer to construct your interface, this becomes a little hard. You have to promote all your widgets to re-implemented versions of them.

另一种方法是使用Qt的均匀过滤器功能。你实现一个基于QObject的类,实现一个通用的事件处理函数。然后,将此对象作为事件过滤器安装在目标对象上。一个简单的偶数过滤器类看起来像这样:

Another method is to use "even filter" feature of the Qt. You implement an QObject based class which implements a generic event handler function. Then you install this object as an "event filter" on the target object. A simple even filter class looks like this;

class Filter : public QObject
{
protected:
    bool eventFilter(QObject *obj, QEvent *ev)
    {
        // obj : original receiver of the event
        // ev : any event that is sent to 'obj'
        QObject::event(obj, ev);
    }
};

及其用法:

Filter filterObject;
targetObject.installEventFilter(filterObject);

通常,您必须将此事件过滤器安装到用户界面中的所有对象但是,如果您将此过滤器安装到 QApplication 实例,它将捕获应用程序中的所有事件,而不是逐个进行。

Normally you have to install this event filter to all objects (widgets) in your user interface. But instead of going one by one, if you install this filter to your QApplication instance, it will capture all events in your application. After capturing, you can filter the event by it's type and originator.

这是一个更完整的示例,将捕获您的应用程序中的所有鼠标按下事件;

Here is a more complete example that will capture all mouse press events in your application;

class ClickFilter : public QObject
{
protected:
    bool eventFilter(QObject *obj, QEvent *ev)
    {
        if (ev->type() == QEvent::MouseButtonPress && obj->inherits("QWidgetWindow"))
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(ev);
            qDebug() << "clicked:" << mouseEvent->pos();
        }
        return QObject::eventFilter(obj, ev);
    }
};

您应该在 QApplication 实例,可能在 main 函数中;

You should install this event filter object on your QApplication instance, presumably in main function;

QApplication a(argc, argv);
ClickFilter cf;
a.installEventFilter(&cf);

这将捕获应用程序中的所有事件(包括所有窗口 - 如果您的应用程序有多个主窗口),然后通过类型和originator对象类型过滤它们。

This will capture all events in your application (that includes all windows - if your application has multiple main windows), then filter them by type and originator objects type.

但是这个代码有一个问题。我使用 QWidgetWindow 类型来检查事件发起者。这是因为如果事件不是由子窗口小部件处理的,则将其传播到其父窗口。因此,在某些情况下,对于同一个点击事件,会多次调用 ClickFilter :: eventFilter 。我选择 QWidgetWindow ,因为它看起来像一个简单的方法。问题是; QWidgetWindow 是一个内部Qt类型。在应用程序中使用它不是一个好主意。我确定还有另一种方法可以防止重复。

But there is a problem with this code. I've used the QWidgetWindow type to check for events originator. Thats because if an event is not processed by a child widget, it's propagated to its parent. So, in some cases ClickFilter::eventFilter is called multiple times for the same click event. I picked QWidgetWindow because it looked like an easy way of doing it. Problem is; QWidgetWindow is an internal Qt type. It's not a good idea to use it in your application. I'm sure there is another way of preventing duplicates.

更重要的是,在 QApplication 实例,它可能会导致性能问题。但另一方面,在我的应用程序中,我使用这种方法来轻松添加工具栏按钮的键盘快捷方式到它的工具提示在所有应用程序。您可以查看此处 a>。

More importantly it's not a good idea to install an event filter on your QApplication instance, it may cause performance problems. But on the other hand, in my application I've used this method to easily add keyboard shortcuts of a toolbar button to it's tooltip throughout all application. You can see it here.

这篇关于鼠标按下事件里面的Qpushbutton在QT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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