如何在 qt4 设计器中创建自定义插槽?
每当我使用信号/槽编辑器对话框时,我都必须从现有的槽列表中进行选择。 那么问题是如何创建自定义命名插槽?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
右键单击主窗口并选择“更改信号和槽”并添加新槽。
它将出现在您的信号槽编辑器中。
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.
这在 Qt Designer 4.5.2 版本中似乎是可能的,但它不能通过主窗口中的信号/槽编辑器停靠小部件来完成。
这对我有用
警告:我正在使用 PyQt,并且我只尝试使用通过 Python(而不是 C++)以这种方式添加的插槽,因此您的情况可能会有所不同......
This does seem to be possible in the version of Qt Designer 4.5.2, but it can't be done from the Signal/Slot Editor dock-widget in the main window.
This is what worked for me
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...
不幸的是,这在 Qt4 中是不可能的。
在 Qt3 中,您可以创建自定义插槽,然后在 ui.h 文件中实现。 但是,Qt4 不使用此文件,因此不支持自定义插槽。
QtForum 上对此问题进行了一些讨论
Unfortunately this is not possible in 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.
There is some discussion of this issue over on QtForum
我可以通过以下方式做到这一点:
在 MainWindow.h 中,添加行:
在 MainWindow 类中。
在MainWindow.cpp中
I am able to do it by:
In MainWindow.h, add the line:
in the MainWindow class.
In MainWindow.cpp
这似乎不可能以简单的方式实现。
设计器仅允许您将现有小部件升级为您自己的自定义小部件。 但它不允许您连接升级小部件类的信号和插槽。
实现这一点的方法是为设计器创建一个插件,如下所示 此处描述及其后面的页面。
正常的操作过程是将小部件提升到您自己的类,然后在您自己的代码中手动连接它。 此处描述了此过程
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 here and 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
这是不可能的,因为这意味着您需要向现有的 Qt 类添加一个槽,例如 QPushButton,这并不是真正的正确方法。
您最终应该通过对现有 QWidget 进行子类化来创建自己的
QWidget
。 然后按照建议将其作为插件集成到 Qt Designer 中。 拥有自己的类允许您根据需要添加/修改可用的信号/槽。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.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.不要忘记插槽自动连接功能。 有一些缺点,例如如果您重命名小部件,则必须重命名您的函数,但我们在我的公司经常使用这些缺点。
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.
您可以使用魔术槽格式
与此方法的连接是通过方法 connectSlotsByName 并且每当发出信号时,都会调用此插槽。
You can use the magic slot format of
The connection to this method is established by the method connectSlotsByName and whenever the signal is emitted, this slot is invoked.
也许会有帮助。
默认情况下,您必须从现有的插槽列表中进行选择。 但是您可以通过右键单击设计器右侧列表中的对象来添加插槽,然后选择“插槽/信号”并添加自定义插槽/信号。 之后,您可以在信号/槽编辑器中选择它。
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.
单击右键的小部件
将小部件提升为您定义的类,
再次单击右键的小部件
您将看到信号和槽是可编辑的
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