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.
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.
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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:
Qt Code:
void MainWindow::event(sth) { if(sth->accessibleName == "btn1") // do stuff #1 else if(sth->accessibleName == "btn2") // do stuff #2 etc... }To copy to clipboard, switch view to plain text mode
So the main purpose of it is how to serve a lot of buttons.
Last edited by ado130; 15th February 2017 at 09:57.
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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?So the main purpose of it is how to serve a lot of buttons.
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.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
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,
_
Last edited by anda_skoa; 17th February 2017 at 09:42.
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.
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,
_
Bookmarks