C++ 如何在 qt4 设计器中创建自定义插槽?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/165637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a custom slot in qt4 designer?
提问by user24560
Whenever I use the signal/slot editor dialog box, I have to choose from the existing list of slots. So the question is how do I create a custom named slot?
每当我使用信号/槽编辑器对话框时,我必须从现有的槽列表中进行选择。所以问题是如何创建自定义命名插槽?
采纳答案by David Dibben
Unfortunately this is not possible in Qt4.
不幸的是,这在 Qt4 中是不可能的。
In Qt3 you could create custom slots which where then implemented in the ui.h file. However, Qt4 does not use this file so custom slots are not supported.
在 Qt3 中,您可以创建自定义插槽,然后在 ui.h 文件中实现。但是,Qt4 不使用此文件,因此不支持自定义插槽。
There is some discussion of this issue over on QtForum
QtForum上有一些关于这个问题的讨论
回答by Rob Smallshire
This does seem to be possible in the version of Qt Designer 4.5.2, but it can'tbe done from the Signal/Slot Editor dock-widget in the main window.
这在 Qt Designer 4.5.2 版本中似乎是可能的,但不能通过主窗口中的 Signal/Slot Editor 停靠小部件来完成。
This is what worked for me
这对我有用
- Switch to Edit Signals/Slotsmode (F4)
- Drag and drop from the widget which is to emit the signal, to the widget which is to receive the signal.
- A Configure Connectiondialog appears, showing the signals for the emitting widget, and the slots for the receiving widget. Click Edit...below the slots column on the right.
- A Signals/Slots of ReceivingWidgetdialog appears. In here its is possible to click the plus icon beneath slots to add a new slot of any name.
- You can then go back and connect to your new slot in the Configure Connectiondialog, or indeed in the Signal/Slot Editor dockwidget back in the main window.
- 切换到编辑信号/插槽模式 (F4)
- 从要发出信号的小部件拖放到要接收信号的小部件。
- 出现配置连接对话框,显示发射小部件的信号和接收小部件的插槽。单击右侧插槽列下方的编辑...。
- 一个信号/ ReceivingWidget的插槽对话框出现。在这里,可以单击插槽下方的加号图标以添加任何名称的新插槽。
- 然后,您可以返回并连接到配置连接对话框中的新槽,或者实际上是在主窗口中的信号/槽编辑器 Dockwidget 中。
Caveat: I'm using PyQt, and I've only tried to use slots added in this way from Python, not from C++, so your mileage may vary...
警告:我正在使用 PyQt,并且我只尝试使用从 Python 中以这种方式添加的插槽,而不是从 C++ 中添加的插槽,因此您的里程可能会有所不同......
回答by raigon
right click on the main window and select "change signals and slots" and add a new slot. It will appear in your signal slot editor.
右键单击主窗口并选择“更改信号和插槽”并添加一个新插槽。它将出现在您的信号槽编辑器中。
回答by James Dalton
I am able to do it by:
我可以通过以下方式做到:
In MainWindow.h, add the line:
在 MainWindow.h 中,添加以下行:
public slots:
void example();
in the MainWindow class.
在 MainWindow 类中。
In MainWindow.cpp
在 MainWindow.cpp 中
void MainWindow::example() {
<code>
}
回答by shoosh
This doesn't seem to be possible in a simple way.
这似乎不可能以简单的方式实现。
The designer only allows you to promote existing widgets to your own custom widgets. yet it doesn't allow you to connect the signals and slots of the class of promoted widgets.
设计器只允许您将现有小部件提升为您自己的自定义小部件。但它不允许您连接升级小部件类的信号和插槽。
The way this is possible is creating a plugin for the designer as is described hereand in the pages that follow it.
可行的方法是为设计器创建一个插件,如此处及其后的页面所述。
The normal course of action is to promote a widget to your own class and then to connect it manually in your own code. this process is described here
正常的操作过程是将小部件提升到您自己的类,然后在您自己的代码中手动连接它。这个过程在这里描述
回答by Pierre
It is not possible to do it, because it means you would add a slot to an existing Qt class like QPushButton
which is not really the way to go.
这是不可能的,因为这意味着您将向现有的 Qt 类添加一个插槽,QPushButton
这并不是真正可行的方法。
You should create your own QWidget
eventually by subclassing an existing one. Then integrating it into Qt Designer as a plugin as suggested. Having your own class allows you to add/modifiy the signals/slots available as you want.
您QWidget
最终应该通过继承现有的来创建自己的。然后按照建议将其作为插件集成到 Qt Designer 中。拥有自己的类允许您根据需要添加/修改可用的信号/插槽。
回答by Caleb Huitt - cjhuitt
Don't forget about the slot auto-connection features. There are a few drawbacks, like having to rename your function if you rename your widget, but we use those a lot at my company.
不要忘记插槽自动连接功能。有一些缺点,例如如果您重命名小部件,则必须重命名您的函数,但我们在我的公司经常使用这些。
回答by Henrik Hartz
You can use the magic slot format of
您可以使用魔术插槽格式
void on_objectName_signal() {
// slot code here, where objectname is the Qt Designer object name
// and the signal is the emission
}
The connection to this method is established by the method connectSlotsByNameand whenever the signal is emitted, this slot is invoked.
与此方法的连接由方法connectSlotsByName建立,每当发出信号时,都会调用此插槽。
回答by l0kix2
Maybe it'll help.
也许它会有所帮助。
By default you have to choose from the existing list of slots. But you can add slot by right-clicking at you object in the list at right side of designer and choose "slot/signals" and add your custom slot/signal. After that, you can choose it in signal/slot editor.
默认情况下,您必须从现有的插槽列表中进行选择。但是您可以通过右键单击设计器右侧列表中的对象并选择“插槽/信号”并添加自定义插槽/信号来添加插槽。之后,您可以在信号/插槽编辑器中选择它。
回答by snow
click the widget by right button
通过右键单击小部件
promote the widget into a class you defined
将小部件提升为您定义的类
click the widget by right button again
再次通过右键单击小部件
you will see that signal and slot is editable
你会看到信号和插槽是可编辑的