diverger
diverger

Reputation: 176

QToolButton released() signal fired twice

I make a color picker based on Qt 5.4.1. It works as this: when user clicked one QToolButton, it shows the QColorDialog. I use the QToolButton's 'released()' signal. Now the problem is, when I close the QColorDialog, the released() signal fired again. But when I change it to QPushButton, the problem gone.

My toolbutton has no menu with it. I wonder if QToolButton is designed as this, or it's just a bug? Because other reason, I can't use QPushButton. So, is there some workround to make toolbutton work?


Update:

I tried put only the code below in the released slot:

static int n = 0;
qDebug() << Q_FUNC_INFO << ++n;
return;

the counter is increased 2 every click. So it should has nothing with the QColorDialog.

Upvotes: 0

Views: 588

Answers (1)

diverger
diverger

Reputation: 176

I finally find the reason. I've never noticed Qt has the ability to connect signals and slots automatically. That is, QMetaObject::connectSlotsByName(). I accidently give my slot a name like

void on_<object name>_<signal name>(<signal parameters>);

style, this is just the style what "QMetaObject::connectSlotsByName()" needed. So, Qt automatically connect the released() signal to my slot. And I manually connect them too. In Qt, connect a signal twice, it will fire twice, even the sender and receiver is the same!!!!

Upvotes: 2

Related Questions