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?
-
Hummm! someone could give a hint on where in the Qt designer documentation is this specific question addressed ? Myself I could not find it. And it is not the signal/slot stuff – joaquin Nov 01 '11 at 10:35
-
1@joaquin: It *is* signal and slot stuff, for all he has to do is to load the user interface, and connect slots to the buttons in question. These are really Qt basics. – Nov 01 '11 at 10:39
-
1@lunaryorn thanks. Sorry but how do you create the slot "my_custom_function" in order to bind it to the clicked() signal of the pushButton on the Signal/Slot editor. Maybe I miss something but I can not find the way in designer documentation, where I just found how to bind sender and receiver predefined signals and slots . Could you give a link and answer in this way the OP question? – joaquin Nov 01 '11 at 10:47
2 Answers
Click on the
Edit Signal/Slots
tool.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 (grounding) connection).
When you release the mouse button, the
Configure Connection
dialog appears.In this dialog select a signal in the left text control (the sender), for example,
pressed()
.Then press
edit
in the right text control (the receiver). A dialog for theSignals/Slots of MainWindow
appears.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 functiondoit_when_yes_ispressed()
. Accept.Now in the
Configure Connection
dialog you will see your function in the right text control. Select and Accept.In the designer now you can see the signal and your function in the widget.

- 72,056
- 11
- 123
- 141

- 82,968
- 29
- 138
- 152
-
1thanks that exactly what i was looking for :) can you edit the function from the designer or you need to generate the code first? – pol Nov 01 '11 at 11:47
-
1No you cannot edit the slot from the designer. In the designer you just define the name of a slot as signal will connect to. When uic is executed the autogenerated ui_filename.h includes a connection between the signal and the slot. Notice that there will be no compilation error even if the slot does not exist. So you have to add a slot (preferably private) in your .h file and implement it in the .cpp file Similarly for Python. – pnezis Nov 01 '11 at 12:27
-
-
7Hi, I am using QT designer 4.8.6 and in the `Configure Connection` window, the `Edit` option is greyed out (is disabled and can't be clicked). Anyone knows why this odd behaviour? – zhirzh Apr 28 '15 at 19:15
-
Is there an automatic way to add the slot (defined in designer) to the code (.h and .c) by mouse click? – Silicomancer Feb 01 '18 at 23:05
- Right-click on your widget
- Select "Go to slot..."
- Select a signal and click OK
Your custom slot declaration and definition for that signal will be added to *.cpp and *.h files. Its name will be generated automatically.
upd: Sorry, I didn't notice that the question is about Python & QtDesigner itself, I was thinking of the designer mode in QtCreator IDE. However, this still may be useful for someone who is looking for Qt/C++ info, so I leave the answer.

- 604
- 8
- 9