PDA

View Full Version : QPushButton clicked



ado130
14th February 2017, 18:00
Hello forum,
I'd like to ask you, if is possible to get clicked button by his accessibleName value.
I know only this ways:
1) RMB-->Slot--> generate clicked event
2) make a connect, sth like connect(ui->pushButton, SIGNAL( .... )


Thanks.

high_flyer
14th February 2017, 23:21
It will be easier if you tell what it is you want to achieve, the end result, not the way your want to do it, we probably can help you get there in a better way.

ado130
15th February 2017, 09:15
The first reason is that I just want to know it, if it is possible.
The second, let's suppose that I have a application where is a lot of buttons and I'm not sure if is good / clean / .. make a lot of connections. I think that could be better make a event that is automatically called after every clicked button and inside this function (event) will be sth to get AccessibleName.

Pseudocode:


void MainWindow::event(sth) {
if(sth->accessibleName == "btn1")
// do stuff #1
else if(sth->accessibleName == "btn2")
// do stuff #2
etc...
}


So the main purpose of it is how to serve a lot of buttons.

high_flyer
15th February 2017, 16:56
Have a look at QSignalMapper

Other than that, you can use use sender()->objectName() in the slot, but if you need that kind of a hack, something in your design probably is not quite right.

d_stranz
15th February 2017, 16:57
So the main purpose of it is how to serve a lot of buttons.

It sounds like you are asking for a slot to be called any time a button is clicked, whether you connect to its signal or not. How would that even be possible? How could the poor button know which of the slots in your app to call if you don't tell it?

If you don't connect -some- slot to -every- button's clicked() signal, your slots will only be called for the buttons where you did make a connection.

If you make a single slot and connect it to all of the buttons' clicked() signals, you can use the QObject::sender() method inside the slot to give you the pointer to the button instance that invoked the slot, and you can use that pointer to find out the button's properties (like name).

Edit: high_flyer's QSignalMapper might also work, but it is a bit hard to understand for a beginner.

anda_skoa
16th February 2017, 08:53
For the setup you can just retrieve all buttons or all buttons within a widget using QObject::findChildren().

Then loop over the list to connect to the slot or signal mapper.

Cheers,
_

ado130
16th February 2017, 10:18
Thanks!
d_stranz - you are right. I thought that exist a event that is called any time a button is clicked and then in that event/function I get clicked button property (e.g. accessibleName) and accordingly to it I do sth.
So, I'll try QObject::findChildre() to connect to the slot and QObject::sender() to get the clicked button.

anda_skoa
17th February 2017, 09:44
There is no "click" event because these are in fact multiple events in a certain order, i.e. a mouse press event followed by a mouse release event within a button's geometry.

If there were an event you could install a global event filter but that would also be way more hackish than dealing with the buttons explicitly.

Cheers,
_