为什么我的mousePressEvent被调用两次? [英] Why is my mousePressEvent called twice?

查看:574
本文介绍了为什么我的mousePressEvent被调用两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使QLabel可点击,然后按照操作方法".我不确定如何将这段代码导入到我的GUI中(对qt来说我还是一个新手).我所做的是:

  1. 我创建了一个新类(只是从链接中复制/粘贴了ClickableLabel,但我将信号更改为clicked(QMouseEvent* event))
  2. 我在我的GUI中添加了QLabel并将其提升"为ClickableLable
  3. 我将信号连接到主窗口的插槽中,在该插槽中我std::cout有一些东西:

    connect(this->ui->label,SIGNAL(clicked(QMouseEvent*)),
            this,SLOT(on_label_clicked(QMouseEvent*)));
    

似乎可行.问题是,每次我单击标签时,mousePressedEvent都会被调用两次.我也尝试过mouseReleasedEvent,但相同.

有什么想法会出问题吗?

这是我修改的ClickableLable:

class MyClickableLabel : public QLabel {
    Q_OBJECT
    public:
        MyClickableLabel(QWidget* parent=0);
        ~GBoardLabel();
    signals:
        void clicked(QMouseEvent* event);
    protected:
        void mouseReleaseEvent(QMouseEvent* event);
};
MyClickableLabel::MyClickableLabel(QWidget* parent) : QLabel(parent) {this->setText("");}
MyClickableLabel::~MyClickableLabel() {}
void MyClickableLabel::mouseReleaseEvent(QMouseEvent *event){
    std::cout << "CLICKED R" << std::endl;
    std::cout << event->type() << std::endl;
    std::cout << event->pos().x() << std::endl;
    std::cout << event->pos().y() << std::endl;
    emit clicked(event);
}

上述方法中的cout s是我稍后才添加的,并且意识到mouseReleaseEvent实际上仅被调用一次.但是,当我将clicked连接到主窗口的某个插槽时,该插槽会两次接收到该事件.

然后我删除了connect语句,令我惊讶的是,该信号仍然发出和接收(仅一次).我对此感到有些困惑,因为我敢肯定我不会在代码中的任何地方误用connect.

我的标签正在工作,但是我想了解发生了什么.实际上,我不再100%确信我没有使用某些Qt创建器功能进行连接.但是,我不知道在哪里可以找到这样的联系.例如,我在同一主窗口上有一个QButton.在gui编辑器中,我右键单击它,然后显示广告位"->"clicked()"->确定",并自动创建一个名为on_pushButton_clicked()的方法,但是我不知道这在哪里/如何按钮的信号已连接到此方法.另一方面,我的标签插槽列表中没有列出MyClickableLabel::clicked(QMouseEvent*),因此我不认为我是用这种方式创建的连接...

解决方案

我可以修复它,但是我不太了解发生了什么事情...

不是mousePressEvent触发了两次,但是我的on_label_clicked插槽两次接收到该事件.

我删除了connect语句,现在每次单击一次仅调用一次on_label_clicked.似乎引擎盖下正在发生某些事情.也许当插槽被称为on_label_clicked时,它会自动(从"qtmatically")连接到由名为label的子代产生的鼠标事件?

编辑:仍未找到官方文档,但此博客对此进行了很好的解释.总之,只需要为广告位使用命名约定

void on_<widget name="">_<signal name="">(<signal parameters="">);

使用自动连接功能.

I want to make a QLabel clickable and followed this "how-to". I was not sure how to get this piece of code into my GUI (I am quite newbie to qt). What I did was:

  1. I created a new class (just copy/paste of ClickableLabel from the link, but I changed the signal to clicked(QMouseEvent* event))
  2. I added a QLabel to my GUI and "promoted" it to a ClickableLable
  3. I connected the signal to a slot of my main window where I std::cout some stuff:

    connect(this->ui->label,SIGNAL(clicked(QMouseEvent*)),
            this,SLOT(on_label_clicked(QMouseEvent*)));
    

It seems to work. The problem is that each time I click on the label the mousePressedEvent gets called twice. I also tried mouseReleasedEvent but its the same.

Any ideas what could go wrong?

EDIT: Here is my modified ClickableLable:

class MyClickableLabel : public QLabel {
    Q_OBJECT
    public:
        MyClickableLabel(QWidget* parent=0);
        ~GBoardLabel();
    signals:
        void clicked(QMouseEvent* event);
    protected:
        void mouseReleaseEvent(QMouseEvent* event);
};
MyClickableLabel::MyClickableLabel(QWidget* parent) : QLabel(parent) {this->setText("");}
MyClickableLabel::~MyClickableLabel() {}
void MyClickableLabel::mouseReleaseEvent(QMouseEvent *event){
    std::cout << "CLICKED R" << std::endl;
    std::cout << event->type() << std::endl;
    std::cout << event->pos().x() << std::endl;
    std::cout << event->pos().y() << std::endl;
    emit clicked(event);
}

The couts in the in the above method I added only later and realized that the mouseReleaseEvent is actually only called once. But when I connect the clicked to a slot of my mainwindow, this slot recieves the event twice.

Then I removed the connect statement and to my surprise the signal is still emited and recieved (only once). I am a bit puzzled how this works, because I am pretty sure that I do not mistakenly have a connect anywhere in the code.

My label is working, but I would like to understand what is going on. Actually I am not 100% sure anymore that I didn't use some Qt creator feature make the connection. However, I have no idea where to find such connections. For example, I have a QButton on the same main window. In the gui editor I right clicked it and then "show slots"->"clicked()"->"OK" and automatically a method called on_pushButton_clicked() is created, but I have no idea, where this is called / how the button's signal is connected to this method. On the other hand, I do not get the MyClickableLabel::clicked(QMouseEvent*) listed in the list of slots for my label, thus I don't think I created the connection this way...

解决方案

I could fix it, but I do not really understand what is going on...

It was not the mousePressEvent that was fired twice, but my on_label_clicked slot recieved the event twice.

I removed the connect statement and now the on_label_clicked is called only once per click. It seems like, there is something going on under the hood. Maybe when the slot is called on_label_clicked it gets automatically ("qtmatically") connected to the mouse events emmited from the child called label ?

EDIT: Still didnt find the official docs, but this blog explains it quite nicely. In summary, one just needs to use the naming convention for the slot

void on_<widget name="">_<signal name="">(<signal parameters="">);

to make use of the auto connect feature.

这篇关于为什么我的mousePressEvent被调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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