RSS

Add custom slot to component in QT creator

05 Jan

Kemarin bingung, nyari gimana caranya nyambungin slot yang uda dibikin ke komponen guinya kalo pakai qtcreator, setelah searching, nemu postingan maknyus dari thejim di http://www.qtcentre.org/threads/28335-QT-Creator-won-t-recognize-a-slot

Langkah-langkah untuk menambahkannya:

To add a custom slot for a button:

  1. Switch to signal/slot edit mode (F4).
  2. Click and drag on the button to initiate the signal/slot editor.
  3. In the left pane, select your “clicked()”/”triggered()”/whatever signal.
  4. Below the right pane, click the “edit” button.
  5. Below the top pane, click the “Add” button, and enter the call for your custom slot.
  6. Click OK, and your custom slot should now be in the right pane.
  7. 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”.

Qt Code:

Switch view

  1. void on_calculateButton_clicked();

To copy to clipboard, switch view to plain text mode 

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.

 
Leave a comment

Posted by on January 5, 2012 in Uncategorized

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: