PDA

View Full Version : QT Creator won't recognize a slot



lucasvickers
22nd February 2010, 20:59
Hello,

I wish to connect a clicked() signal (from a button) to a slot that I have in my MainWindow class.

In mainwindow.h I have:
protected slots:
void viewDataset();

but when I try to connect the slot in the ui I do:

sender / signal / reciever / slot
button / clicked() / MainWindow /

and for whatever reason viewDataset() is not an option in the slot selection box.

Is there anything I need to do in order to get the creator/designer to recognize my new slot ?

thanks,
lucas

squidge
22nd February 2010, 21:35
Yes, you need to add your slot to designer, it doesn't pick up anything from your source code, so click the "Add" button.

TheJim01
23rd February 2010, 15:38
Ignore: I think what he means is, he has a custom slot in his class, but that custom slot is not showing up in the "<slots>" drop-down when he clicks "Add". I agree, and can confirm that my custom slots (public or protected) do not show up in that drop-down. The only slots listed are those inherited from QMainWindow and QObject. I've tested this in both Windows and Linux. Maybe I'm using it wrong? It's just really frustrating because when you perform a clean, it also wipes out any connect() statements you added manually in ui_whatever.h. :(

EDIT:

To add a custom slot for a button:
Switch to signal/slot edit mode (F4).
Click and drag on the button to initiate the signal/slot editor.
In the left pane, select your "clicked()"/"triggered()"/whatever signal.
Below the right pane, click the "edit" button.
Below the top pane, click the "Add" button, and enter the call for your custom slot.
Click OK, and your custom slot should now be in the right pane.
Select your custom slot, click OK, and build your project.

After you do this, the slot will show up in the <slots> drop-down, but since a signal/slot connection is created for you at this point, you'll only need to utilize it from the drop-down if you use it with another signal.

Ignore: Unfortunately, this doesn't work for menus, since the signals/slots edit mode wants to attach to the menu bar, rather than the menu items. If anyone has a workaround for this, I'd greatly appreciate it...

EDIT:

There is an easy trick to getting Creator to automatically connect signals to your custom slots, regardless of what they are: You have to name the slot function appropriately.

You have to name them using the format "on_<sender object name>_<signal>". For example, if you have a button named "calculateButton", and you want to launch your calculate function via the "clicked()" signal, you must name your function: "on_calculateButton_clicked".

void on_calculateButton_clicked();
Note that if you're hand-coding everything, this doesn't matter, you can name your slots and functions whatever you want. But to make it easier when working in Creator, using this naming convention for slots makes it less of a headache.

cabuk
28th September 2010, 03:09
The latest solution!

I’ve started working with Qt 2 days ago and I’m already sniffing the Qt Creator IDE. Actually, there IS a way to create and use custom slots on the “Signals & Slots Editor”.

The tricky is:

Right click your QMainWindow widget on the design view OR right click the QMainWindow on the Object Inspector;
Choose “Change Signals/Slots…” on the menu.

Shall I explain the given screen or you guys can figure it out? ;)

hill_rg
1st April 2013, 00:33
The latest solution!

I’ve started working with Qt 2 days ago and I’m already sniffing the Qt Creator IDE. Actually, there IS a way to create and use custom slots on the “Signals & Slots Editor”.

The tricky is:

Right click your QMainWindow widget on the design view OR right click the QMainWindow on the Object Inspector;
Choose “Change Signals/Slots…” on the menu.

Shall I explain the given screen or you guys can figure it out? ;)

Hey, it's 3 years later, but I want to thank you for this!!!