Qt Designer: how to add custom slot and code to a button

pol picture pol · Nov 1, 2011 · Viewed 32k times · Source

I use Qt4 Designer and I want that when I click on the "yes" button, some code will execute. And when I click on the "no", some other code will be execute. How can I do it?

Answer

joaquin picture joaquin · Nov 1, 2011
  1. Click on the Edit Signal/Slots tool.

  2. Create a connection for your button. For this, select your button in the designer by pressing on it with the left button of the mouse. Move the mouse to some place in the main window to create a connection with the main window (it is like a red line with a earth connection).

  3. When you release the mouse button, the Configure Connection dialog appears.

  4. In this dialog select a signal in the left text control (the sender), for example, pressed().

  5. Then press edit in the right text control (the receiver). A dialog for the Signals/Slots of MainWindow appears.

  6. In the slot panel add a new slot (green cross). The text slot1() appears. Double click on it to edit the line and write instead the name of your function doit_when_yes_ispressed(). Accept.

  7. Now in the Configure Connection dialog you will see your function in the right text control. Select and Accept.

  8. In the designer now you can see the signal and your function in the widget.