问题
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?
回答1:
Click on the
Edit Signal/Slotstool.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).
When you release the mouse button, the
Configure Connectiondialog appears.In this dialog select a signal in the left text control (the sender), for example,
pressed().Then press
editin the right text control (the receiver). A dialog for theSignals/Slots of MainWindowappears.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 Connectiondialog 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.
回答2:
- 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.
来源:https://stackoverflow.com/questions/7964869/qt-designer-how-to-add-custom-slot-and-code-to-a-button