windows 在 Qt 项目、Visual Studio 中添加按钮单击处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7091748/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:53:07  来源:igfitidea点击:

Add button click handler in Qt project, Visual Studio

c++windowsqt

提问by Alex F

I have Qt SDK and Visual Studio Qt Add-In working in VS2008. I created Qt UI project with main window class MainWindow. Double-click on mainwindow.ui opens Qt Designer. Then I added push button to the window, and called it pushButton. In Signals-Slots mode I managed to connect button's clicked signal with MainWindow ButtonClicked slot. Signal/Slot editor looks like this:

我有 Qt SDK 和 Visual Studio Qt Add-In 在 VS2008 中工作。我使用主窗口类 MainWindow 创建了 Qt UI 项目。双击 mainwindow.ui 打开 Qt Designer。然后我在窗口中添加了按钮,并将其命名为按钮。在 Signals-Slots 模式下,我设法将按钮的点击信号与 MainWindow ButtonClicked 插槽连接起来。信号/插槽编辑器如下所示:

Sender   pushButton
Signal   clicked()
Receiver MainWindowClass
Slot     ButtonClicked()

mainwindow.ui file was changed, reflecting this new information. However, mainwindow.cpp and mainwindow.h remain unchanged. I expect to see the place where I can add my own code. So, I added this code manually:

mainwindow.ui 文件已更改,反映了此新信息。但是,mainwindow.cpp 和 mainwindow.h 保持不变。我希望看到我可以添加自己的代码的地方。所以,我手动添加了这段代码:

// mainwindow.h
...
protected slots:
    void ButtonClicked();

// mainwindow.cpp
void MainWindow::ButtonClicked()
{
    QMessageBox msgBox;
    msgBox.setText("Clicked");
    msgBox.exec();
}

It works, but I wonder whether this is correct way to do this. Is slot declaration and implementation supposed to be added manually, or I am missing something?

它有效,但我想知道这是否是正确的方法。插槽声明和实现是否应该手动添加,或者我遗漏了什么?

采纳答案by useraged

If you use signal/slot editor, you have to add these codes manually. Old Qt Add-In was automatically add these if you double click on a button from designer. Now Qt Designer is a seperate application. Double clicking is not possible.

如果您使用信号/槽编辑器,则必须手动添加这些代码。如果您双击设计器中的按钮,旧的 Qt 插件会自动添加这些。现在 Qt Designer 是一个单独的应用程序。无法双击。

Also you can use automatic connections. With automatic connections you dont need to connect signals with slots. Functions which has special naming convention, automatically called. Like on_okButton_clicked.

您也可以使用自动连接。使用自动连接,您不需要用插槽连接信号。具有特殊命名约定的函数,自动调用。喜欢on_okButton_clicked