简体   繁体   English

Qt事件过滤器未检测到objectName

[英]Qt eventfilter does not detect objectName

My program uses a ui-form-file that consists, next to other widgets, of a label called "grip" (its objectname). 我的程序使用一个ui-form-file,该文件除其他小部件外还包含一个名为“ grip”(其对象名)的标签。 As I am running the code, I see the code line grip was not detected and I am wondering why the mouse click on the label is not recognized. 在运行代码时,我看到grip was not detected代码行grip was not detected点,并且想知道为什么鼠标在标签上的单击无法被识别。 I also have defined a mousePressEvent(QMouseEvent *event) that works as intended if I click on that label. 我还定义了一个mousePressEvent(QMouseEvent *event) ,如果我单击该标签,它将按预期工作。

bool Note::eventFilter(QObject *target, QEvent *event)
{
    if (event->type()==QEvent::MouseButtonPress){
        qDebug() << "in Note::eventFilter" << endl;
        if (target->objectName()=="grip")
        {
            lastClicked="grip";
            qDebug() << "lastClicked == grip" << endl;
        }
        else
            qDebug() << "grip was not detected" << endl;
     }
    return false;
}

What may be a reason for target->objectName()=="grip" being false, if I click on that target and it is called "grip"? 如果我单击该目标并将其称为“抓地力”,则target->objectName()=="grip"为假的原因可能是什么?

EDIT: That is how my event functions are defined: 编辑:这就是我的事件函数的定义方式:

void Note::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
       qDebug() << "Left mouse button click detected";
        ...

the main event filter is initialised in the Note's constructor: 主事件过滤器在Note的构造函数中初始化:

Note::Note(std::vector<Note *> *nListIn){
    qDebug() << "in Note::Note()" << endl;
    ui.setupUi(this);
    installEventFilter(this);
    setWindowFlags(Qt::FramelessWindowHint);
    this->show(); //must be after the Qt::FramelessWindowHint
    nList = nListIn;
    nList->push_back(this);
    qDebug() << "Size of nList (aka noteList)" << nList->size() << endl;
}

EDIT 2: Found some description, might this be the reason? 编辑2:找到一些描述,这可能是原因吗?

If your widget only contains child widgets, you probably do not need to implement any event handlers. 如果您的窗口小部件仅包含子窗口小部件,则可能不需要实现任何事件处理程序。 If you want to detect a mouse click in a child widget call the child's underMouse() function inside the widget's mousePressEvent(). 如果要检测鼠标单击子窗口小部件,请在窗口小部件的mousePressEvent()中调用子窗口的underMouse()函数。

By definition, if you install an event filter only on itself (by calling installEventFilter(this) , the following holds: 根据定义,如果仅在自身上安装事件过滤器(通过调用installEventFilter(this) ,则以下内容成立):

bool Note::eventFilter(QObject *target, QEvent *) {
  Q_ASSERT(target == this);
  ...
}

Obviously, the target won't ever be called grip , unless you've named the instance of your Note class that way. 显然,除非您以这种方式命名了Note类的实例,否则目标永远不会被称为“ grip

If you want to filter the events on the grip label, then you must install the event filter on that label, not on the Note widget. 如果要过滤夹点标签上的事件,则必须将事件过滤器安装在该标签上,而不是“ Note小部件上。 The Note widget will only get events that the children have ignored, and by that time, it doesn't matter that you "filter" them - it's too late. Note窗口小部件将仅获取子级忽略的事件,到那时,“过滤”它们并不重要-为时已晚。

Your setup code could contain, for example: 您的设置代码可能包含,例如:

ui.grip->installEventFilter(this);

Or, assuming nothing about the structure of the ui class: 或者,不假设有关ui类的结构:

QWidget * grip = findChild<QWidget*>("grip");
if (grip) grip->installEventFilter(this);

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

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