Why is my mousePressEvent called twice?

[亡魂溺海] 提交于 2020-01-07 01:21:16

问题


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...


回答1:


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.



来源:https://stackoverflow.com/questions/29985844/why-is-my-mousepressevent-called-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!