如何在 qt4 设计器中创建自定义插槽?

发布于 2024-07-06 19:02:02 字数 60 浏览 8 评论 0原文

每当我使用信号/槽编辑器对话框时,我都必须从现有的槽列表中进行选择。 那么问题是如何创建自定义命名插槽?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

我一向站在原地 2024-07-13 19:02:02

右键单击主窗口并选择“更改信号和槽”并添加新槽。
它将出现在您的信号槽编辑器中。

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.

故事↓在人 2024-07-13 19:02:02

这在 Qt Designer 4.5.2 版本中似乎是可能的,但它不能通过主窗口中的信号/槽编辑器停靠小部件来完成。

这对我有用

  1. 切换到编辑信号/槽模式(F4)
  2. 从要发出信号的小部件拖放到要接收信号的小部件。
  3. 出现一个配置连接对话框,显示发送小部件的信号以及接收小部件的插槽。 单击右侧插槽列下方的编辑...
  4. 将出现“ReceivingWidget 的信号/时隙”对话框。 在这里,可以单击插槽下方的加号图标来添加任意名称的新插槽。
  5. 然后,您可以返回并在“配置连接”对话框中连接到新插槽,或者在主窗口中的“信号/插槽编辑器”停靠小部件中连接到新插槽。

警告:我正在使用 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

  1. Switch to Edit Signals/Slots mode (F4)
  2. Drag and drop from the widget which is to emit the signal, to the widget which is to receive the signal.
  3. A Configure Connection dialog appears, showing the signals for the emitting widget, and the slots for the receiving widget. Click Edit... below the slots column on the right.
  4. A Signals/Slots of ReceivingWidget dialog appears. In here its is possible to click the plus icon beneath slots to add a new slot of any name.
  5. You can then go back and connect to your new slot in the Configure Connection dialog, or indeed in the Signal/Slot Editor dockwidget back in the main window.

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...

生生漫 2024-07-13 19:02:02

不幸的是,这在 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

丢了幸福的猪 2024-07-13 19:02:02

我可以通过以下方式做到这一点:

在 MainWindow.h 中,添加行:

public slots:
     void example();

在 MainWindow 类中。

在MainWindow.cpp中

void MainWindow::example() {
     <code>
}

I am able to do it by:

In MainWindow.h, add the line:

public slots:
     void example();

in the MainWindow class.

In MainWindow.cpp

void MainWindow::example() {
     <code>
}
抠脚大汉 2024-07-13 19:02:02

这似乎不可能以简单的方式实现。

设计器仅允许您将现有小部件升级为您自己的自定义小部件。 但它不允许您连接升级小部件类的信号和插槽。

实现这一点的方法是为设计器创建一个插件,如下所示 此处描述及其后面的页面。

正常的操作过程是将小部件提升到您自己的类,然后在您自己的代码中手动连接它。 此处描述了此过程

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

原谅我要高飞 2024-07-13 19:02:02

这是不可能的,因为这意味着您需要向现有的 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.

物价感观 2024-07-13 19:02:02

不要忘记插槽自动连接功能。 有一些缺点,例如如果您重命名小部件,则必须重命名您的函数,但我们在我的公司经常使用这些缺点。

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.

泪之魂 2024-07-13 19:02:02

您可以使用魔术槽格式

void on_objectName_signal() {
// slot code here, where objectname is the Qt Designer object name
// and the signal is the emission
}

与此方法的连接是通过方法 connectSlotsByName 并且每当发出信号时,都会调用此插槽。

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 connectSlotsByName and whenever the signal is emitted, this slot is invoked.

定格我的天空 2024-07-13 19:02:02

也许会有帮助。

默认情况下,您必须从现有的插槽列表中进行选择。 但是您可以通过右键单击设计器右侧列表中的对象来添加插槽,然后选择“插槽/信号”并添加自定义插槽/信号。 之后,您可以在信号/槽编辑器中选择它。

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.

最笨的告白 2024-07-13 19:02:02

单击右键的小部件

将小部件提升为您定义的类,

再次单击右键的小部件

您将看到信号和槽是可编辑的

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文