I have a button named ‘Start’, and when it’s clicked, a lengthy operation starts and I want it to change to a button named ‘Stop’. The behavior when you click it obviously changes as well. What is the best way to implement this – by changing the button’s text and reconnecting the ‘clicked()’ signal to a different slot, or by having two buttons and then hiding ‘Start’ and showing ‘Stop’.
Share
Both of your options work. A simple third alternative is to change the button text (changing the icon would be a good idea too), and saving the “state” (playing/not playing) somewhere. In your connected slot, just do the Right Thing depending on current state. (That way you don’t have to reconnect anything).
Another option is to use the push button with
setCheckable(true)set. This way the button acts more like a toggle-button (stays depressed when first clicked, raises back with second click), and combining that with your dynamic text/icon change.If you use this, you should use the
toggled(bool)signal rather than theclicked()one. The slot argument tells you whether the button is “active” or not. (This can also be queried withisChecked().)