Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 899
pol Avatar asked Nov 01 '11 09:11

pol


People also ask

How do I add a custom widget to Qt?

Adding the Custom Widget to Qt Designer. Click Tools|Custom|Edit Custom Widgets to invoke the Edit Custom Widgets dialog. Click New Widget so that we are ready to add our new widget. Change the Class name from 'MyCustomWidget' to 'Vcr'.

How do I use the signal slot editor Qt Designer?

To begin connecting objects, enter the signals and slots editing mode by opening the Edit menu and selecting Edit Signals/Slots, or by pressing the F4 key. All widgets and layouts on the form can be connected together. However, spacers just provide spacing hints to layouts, so they cannot be connected to other objects.

How do I add a button to my toolbar in Qt?

It's one of the panes of Qt Designer, select View->Action Editor if you don't see it. You can create new actions here. Next, drag actions to the toolbar to populate it with pushbuttons that trigger the actions.


2 Answers

  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.

like image 186
joaquin Avatar answered Sep 28 '22 03:09

joaquin


  • 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.

like image 42
volvpavl Avatar answered Sep 28 '22 04:09

volvpavl