Qt Creator. Change code generating template for slots

亡梦爱人 提交于 2021-01-28 01:48:48

问题


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?


回答1:


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




回答2:


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'



来源:https://stackoverflow.com/questions/28987152/qt-creator-change-code-generating-template-for-slots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!