btse
btse

Reputation: 8021

Proper way to attach a function to button in Qt creator?

Note: I am developing in Qt creator 5.0

I am looking at someone else's Qt code and I am a little confused. It appears to me that there are essentially two ways to achieve the same task of attaching a function to a QPushButton clicked() signal.

The way that the tutorial on the Qt website instructs to do it is to go into the Design mode, right click on the QPushButton, click on "Go to slot..." and then write the code for the button in the created function.

In the code that I am currently looking at, they have written their own public function in the MainWindow class (which is the root class). Then in the Design mode's Edit/signal slots mode they are attaching(dragging) the QPushButton signal to the MainWindow, at which point they select the manually created function as the slot.

Is there a difference between these two methods, and is one way preferred over the other?

The main difference I have noticed is that if I follow the first method, then the slot/signal that I just created does not appear in the "Signal & Slots Editor".

Upvotes: 3

Views: 6950

Answers (2)

Resurrection
Resurrection

Reputation: 4106

The main difference I have noticed is that if I follow the first method, then the slot/signal that I just created does not appear in the "Signal & Slots Editor".

That is because the slot in the first method is private slot of the MainWindow while in the second method the button sends signal to a public slot (or function in your example which is imho BAD habit to do though) of MainWindow. The difference is apparent I guess that the private slots are used within the class (MainWindow in this case) only and cannot be connected outside of it. I believe it's useful distinction for understanding the code which is why I disagree with this (stressed by me):

With the first one, you are totally sure of what you're doing, no misspelled words... But hard to be updated if your code comes with poor documentation.

Two reasons:

1) As long as you name your slots sensibly (i.e. PushButton1_Clicked) it is pretty starightforward what it is connected to (signal clicked() of PushButton1).

3) Even if the functionality is reused elsewhere (the only reason to have it as public function imho) you would better call that function from such private slot of the button rather than connecting the button in the code directly to it treating it as a slot (which again is at least pretty confusing thing to do). Better yet having private slot for button and public slot for whatever is needed outside of MainWindow scope and the function itself as private method called by both... Or if the functionality is NOT used in signals/slots outside of MainWindow have it as public function but still call it from button's private slot rather than connecting it directly.

The reason for me to separate it like this is to separate the functionality and UI which applies especially to cases where you need the code to be reused outside of the UI elements but also if more than one element uses it. Being able to see the functionality isolated from UI makes it easier to update without worrying about what UI elements are connected to it. But that is just me. :-)

PS. Realted remark. Unless you specifically need the button to do something on clicked() I would suggest using released() signal instead because user could reconsider after clicking the button but with clicked() he would not be able to.

Upvotes: 2

Thomas Ayoub
Thomas Ayoub

Reputation: 29451

With the first one, you are totally sure of what you're doing, no misspelled words... But hard to be updated if your code comes with poor documentation.

With the second one, edition will be easier and, even if you don't document your code enough, it will be easy to update for anyone else.

Upvotes: 0

Related Questions