Qt Designer:如何向按钮添加自定义插槽和代码
我使用 Qt4 Designer,我希望当我单击“是”按钮时,一些代码将会执行。当我单击“否”时,将执行其他一些代码。我该怎么做呢?
I use Qt4 Designer and I want that when I click on the "yes" button, some code will execute. And when I click on the "no", some other code will be execute. How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单击
编辑信号/时隙
工具。为您的按钮创建连接。为此,请用鼠标左键按下设计器中的按钮来选择它。将鼠标移动到主窗口中的某个位置以创建与主窗口的连接(就像一条带有接地连接的红线)。
释放鼠标按钮后,将出现
配置连接
对话框。在此对话框中,在左侧文本控件(发送者)中选择一个信号,例如
pressed()
。然后按右侧文本控件(接收者)中的
编辑
。出现MainWindow 信号/槽
对话框。在插槽面板中添加一个新插槽(绿色十字)。出现文本
slot1()
。双击它来编辑该行并写入函数的名称doit_when_yes_ispressed()
。接受。现在,在
配置连接
对话框中,您将在右侧文本控件中看到您的函数。选择并接受。现在在设计器中,您可以在小部件中看到信号和您的函数。
Click on the
Edit Signal/Slots
tool.Create a connection for your button. For this, select your button in the designer by pressing on it with the left button of the mouse. Move the mouse to some place in the main window to create a connection with the main window (it is like a red line with a earth (grounding) connection).
When you release the mouse button, the
Configure Connection
dialog appears.In this dialog select a signal in the left text control (the sender), for example,
pressed()
.Then press
edit
in the right text control (the receiver). A dialog for theSignals/Slots of MainWindow
appears.In the slot panel add a new slot (green cross). The text
slot1()
appears. Double click on it to edit the line and write instead the name of your functiondoit_when_yes_ispressed()
. Accept.Now in the
Configure Connection
dialog you will see your function in the right text control. Select and Accept.In the designer now you can see the signal and your function in the widget.
该信号的自定义插槽声明和定义将添加到 *.cpp 和 *.h 文件中。它的名称将自动生成。
更新:
抱歉,我没有注意到这个问题是关于Python & QtDesigner 本身,我正在考虑 QtCreator IDE 中的设计器模式。然而,这对于正在寻找 Qt/C++ 信息的人来说仍然可能有用,所以我留下答案。
Your custom slot declaration and definition for that signal will be added to *.cpp and *.h files. Its name will be generated automatically.
upd:
Sorry, I didn't notice that the question is about Python & QtDesigner itself, I was thinking of the designer mode in QtCreator IDE. However, this still may be useful for someone who is looking for Qt/C++ info, so I leave the answer.