Qt Creator.更改插槽的代码生成模板 [英] Qt Creator. Change code generating template for slots
问题描述
我正在使用基于Qt 5.3.2的Qt Creator 3.2.1
I'm using Qt Creator 3.2.1 based on Qt 5.3.2
在设计器中,您可以右键单击按钮,然后选择转到插槽...".打开一个对话框,您可以选择一个插槽...例如 Clicked()
.
然后您将收到自动生成的方法
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()
{}
但是我使用的代码样式应将该方法命名为 onButtonClicked()
.
如何更改默认代码生成模板以满足我的需求?
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?
推荐答案
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}
UI文件中具有此签名和匹配的Widget/Signal的每个方法都会自动作为插槽连接.
Every mehtod with this signature and matching Widget/Signal in the UI file will automatically be connected as a slot.
此机制在Qt中进行了硬编码,您无法更改.
This mechanism is hardcoded in Qt, you cannot change it.
但是,您可以在Designer中手动连接信号/插槽:
However, you can manually connect signals/slots in the Designer:
- 要么进入信号/插槽视图(快捷键F4),然后拖放对象以选择单数和插槽
- 或打开Signal/Slot编辑器(通常在右下方区域),然后通过输入发送器,信号,接收器和插槽来添加连接
使用这些方法,您可以选择任何名称的插槽
With these methods, you can choose a slot with any name
更新:
将自定义窗口小部件与QtDesigner未知的自己的信号/插槽一起使用时,可以在上述的信号/插槽拖放方法中添加它们.只需单击更改..."即可.按钮,然后添加信号/插槽的签名.
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.
您必须确保实例化UI的具体Class实际上具有这些信号/插槽.
You have to make sure that the concrete Class instantiating the UI actually has these signals/slots.
这仅在用户界面或占位符小部件的root-widget上可用
This is only possible on the root-widget of the UI or placeholder widgets
这篇关于Qt Creator.更改插槽的代码生成模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!