21

Whenever I use the signal/slot editor dialog box, I have to choose from the existing list of slots. So the question is how do I create a custom named slot?

Honest Abe
  • 8,430
  • 4
  • 49
  • 64
user24560
  • 898
  • 2
  • 12
  • 16

10 Answers10

29

This does seem to be possible in the version of Qt Designer 4.5.2, but it can't be done from the Signal/Slot Editor dock-widget in the main window.

This is what worked for me

  1. Switch to Edit Signals/Slots mode (F4)
  2. Drag and drop from the widget which is to emit the signal, to the widget which is to receive the signal.
  3. A Configure Connection dialog appears, showing the signals for the emitting widget, and the slots for the receiving widget. Click Edit... below the slots column on the right.
  4. A Signals/Slots of ReceivingWidget dialog appears. In here its is possible to click the plus icon beneath slots to add a new slot of any name.
  5. You can then go back and connect to your new slot in the Configure Connection dialog, or indeed in the Signal/Slot Editor dockwidget back in the main window.

Caveat: I'm using PyQt, and I've only tried to use slots added in this way from Python, not from C++, so your mileage may vary...

Nathan Davis
  • 5,636
  • 27
  • 39
Rob Smallshire
  • 1,450
  • 1
  • 15
  • 22
  • 3
    Both of the "Edit..." buttons in the Configure Connection dialog box are always grayed out for me. – James Nov 07 '09 at 21:27
  • Edit button in Configure Connection is disabled .How did you click it? –  Oct 19 '11 at 18:52
  • I had the greyed buttons too but finally noticed that if I connect the Signal to the main window I can add slots using the method given here. Seems to work as expected but does mean that you get a lot of methods in the main window instead of in individual widgets. – tom stratton Aug 03 '12 at 01:15
  • If the edit button is disabled, then you probably didn't properly accomplish step 2. When the Configure Connection window appears, look at the names of the two columns. The name of the right column is supposed to be the owner of the custom slot. – Honest Abe Aug 22 '15 at 23:59
  • 1
    For anyone who wanders in off Google, under Qt 5, you have to use the the "[promote widget](https://doc.qt.io/qt-5/designer-using-custom-widgets.html)" feature to make the widget in the editor an alias for a custom class before the "Edit..." button will become clickable on built-in widgets other than the top-level window. – ssokolow Sep 12 '16 at 07:58
29

right click on the main window and select "change signals and slots" and add a new slot. It will appear in your signal slot editor.

raigon
  • 291
  • 3
  • 2
4

I am able to do it by:

In MainWindow.h, add the line:

public slots:
     void example();

in the MainWindow class.

In MainWindow.cpp

void MainWindow::example() {
     <code>
}
rikitikitik
  • 2,414
  • 2
  • 26
  • 37
4

Unfortunately this is not possible in Qt4.

In Qt3 you could create custom slots which where then implemented in the ui.h file. However, Qt4 does not use this file so custom slots are not supported.

There is some discussion of this issue over on QtForum

David Dibben
  • 18,460
  • 6
  • 41
  • 41
  • The qt4 designer supports reading custom slots created in forms ported from qt3. It gave me this impression that it was also possible to create one. – user24560 Oct 03 '08 at 11:18
  • See comment by raigon below, this is currently possible. – OliJG May 31 '13 at 08:44
3

This doesn't seem to be possible in a simple way.

The designer only allows you to promote existing widgets to your own custom widgets. yet it doesn't allow you to connect the signals and slots of the class of promoted widgets.

The way this is possible is creating a plugin for the designer as is described here and in the pages that follow it.

The normal course of action is to promote a widget to your own class and then to connect it manually in your own code. this process is described here

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
shoosh
  • 76,898
  • 55
  • 205
  • 325
2

It is not possible to do it, because it means you would add a slot to an existing Qt class like QPushButton which is not really the way to go.

You should create your own QWidget eventually by subclassing an existing one. Then integrating it into Qt Designer as a plugin as suggested. Having your own class allows you to add/modifiy the signals/slots available as you want.

Pierre
  • 2,858
  • 22
  • 21
2

Don't forget about the slot auto-connection features. There are a few drawbacks, like having to rename your function if you rename your widget, but we use those a lot at my company.

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
2

You can use the magic slot format of

void on_objectName_signal() {
// slot code here, where objectname is the Qt Designer object name
// and the signal is the emission
}

The connection to this method is established by the method connectSlotsByName and whenever the signal is emitted, this slot is invoked.

ololuki
  • 377
  • 1
  • 7
  • 14
Henrik Hartz
  • 3,677
  • 1
  • 27
  • 28
1

Maybe it'll help.

By default you have to choose from the existing list of slots. But you can add slot by right-clicking at you object in the list at right side of designer and choose "slot/signals" and add your custom slot/signal. After that, you can choose it in signal/slot editor.

l0kix2
  • 11
  • 1
0

click the widget by right button

promote the widget into a class you defined

click the widget by right button again

you will see that signal and slot is editable