Qt设计器,缺少“转到插槽”在上下文菜单中? [英] Qt Designer, missing "go to slot" in context menu?
问题描述
我一直在观看YouTube上的Qt教程系列,其中作者展示了当按下按钮时如何调用一个功能。 他右键点击Qt Creator IDE中的按钮,选择了转到插槽,从那里他选择将触发生成的函数的信号。由于我习惯使用Netbeans开发,我只是试图使用嵌入式Qt设计器来跟随他的例子。不幸的是,当我右键点击我的按钮或任何小部件时,没有转到插槽...条目。我可以,当然,为我的主窗口创建一个新的插槽,然后连接按钮的信号,它,但做一个单一的功能,似乎更方便和干净对我。有没有办法解决是,或者如果不是,至少一个方法来完全通过代码,而不必为主窗口添加一个新的插槽为每个按钮服务器不同的目的?感谢您的帮助。
I've been watching a Qt tutorial series on YouTube, in which the author shows how to call a function, when a button is pressed. He right-clicked on the button in Qt Creator IDE and chose "Go to slot", from where he chose the signal which would fire the generated function. Since I am used to develop with Netbeans, I simply tried to follow his example using the embedded Qt Designer. Unfortunately, there is no "Go to slot..." entry when I right-click on my button or any widget. I could, of course, create a new slot for my main window and then connect the button's signal to it, but doing it with a single function just seems way more convenient and clean to me. Is there a way to fix is, or if not, at least a way to do with via code entirely, without having to add a new slot to the main window for every single button that servers a different purpose? Thanks for your help.
推荐答案
虽然我不知道为什么Netbeans中的QtDesigner不提供此功能,该功能的作用:它只是在您的widget类中创建一个具有特殊名称的插槽。请注意,它不添加connect语句。它使用自动连接功能,其工作方式如下:
While I don't know why the QtDesigner in Netbeans doesn't provide this feature, I know what the feature does: It just creates a slot with a special name in your widget class. Note that it does not add a connect statement. It uses the automatic connection feature, which works like this:
对于符合此模式的每个插槽:
For each slot which name matches this pattern:
void on_X_Y(...)
它将连接到名为名为
。因此,如果您有一个名为 X
的对象的Y 按钮
的QPushButton,并且您希望处理按
的信号,签名之后:
it will be connected to the signal named Y
of the object named X
. So if you have a QPushButton named button
, and you want to handle the signal pressed
, simply create a slot with the following signature:
void on_button_pressed()
如果你想知道这个槽如何连接到信号:这发生在 setupUi()结束处的
ui _... h
code>:
If you wonder how this slot gets connected to the signal: This happens in the ui_...h
file at the end of setupUi()
:
QMetaObject::connectSlotsByName(WidgetName);
这篇关于Qt设计器,缺少“转到插槽”在上下文菜单中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!