Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Creator. Change code generating template for slots

I'm using Qt Creator 3.2.1 based on Qt 5.3.2

When in the designer, you can right click on a pushButton and select "Go to slot ...". A dialogue box opens allowing you to select a slot ... for example Clicked().
Then you will receive autogenerated method

void MyClass::on_Button_clicked()
{}

But I'm using code style in which the method should be named onButtonClicked().
How can I change default code generating template to satisfy me needs?

like image 629
Temak Avatar asked Jul 10 '26 05:07

Temak


2 Answers

Qt Designer generated UIs support a feature called Automatic Connections. This will automatically connect signals and slots based on a given naming convention. This is

on_{Sender Object Name}_{Signal}

Every mehtod with this signature and matching Widget/Signal in the UI file will automatically be connected as a slot.

This mechanism is hardcoded in Qt, you cannot change it.

However, you can manually connect signals/slots in the Designer:

  • either go to the signal/slot view (shortcut F4) and drag/drop the objects to select the singals and slots
  • or open the Signal/Slot editor (usually in the lower right area) and add a connection by entering sender, signal, receiver and slot

With these methods, you can choose a slot with any name

Update:
When using a custom widget with your own signals/slots that are not known to QtDesigner, you can add them in Signal/Slot drag and drop method mentioned above. Simply click on the "Change..." button in the popup editor, and add the signature of your signal/slot.

You have to make sure that the concrete Class instantiating the UI actually has these signals/slots.

This is only possible on the root-widget of the UI or placeholder widgets

like image 61
king_nak Avatar answered Jul 13 '26 14:07

king_nak


I don't think you can change the default name. But what you can do is right click on the generated slot and select 'Refactor->Convert to camel case'

like image 44
avb Avatar answered Jul 13 '26 15:07

avb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!