我在VS2008中使用Qt SDK和Visual Studio Qt加载项。 我用主窗口类MainWindow创建了Qt UI项目。 双击mainwindow.ui打开Qt Designer。 然后我在窗口添加了按钮,并将其称为pushButton。 在Signals-Slots模式下,我设法将按钮的点击信号与MainWindow ButtonClicked插槽连接。 信号/插槽编辑器如下所示:
Sender pushButton Signal clicked() Receiver MainWindowClass Slot ButtonClicked()mainwindow.ui文件已更改,反映了这一新信息。 但是,mainwindow.cpp和mainwindow.h保持不变。 我期待看到我可以添加自己的代码的地方。 所以,我手动添加了这段代码:
// mainwindow.h ... protected slots: void ButtonClicked(); // mainwindow.cpp void MainWindow::ButtonClicked() { QMessageBox msgBox; msgBox.setText("Clicked"); msgBox.exec(); }它有效,但我想知道这是否是正确的方法。 插槽声明和实现是否应该手动添加,或者我遗漏了什么?
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:
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.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?
最满意答案
如果使用信号/插槽编辑器,则必须手动添加这些代码。 如果您双击设计器中的按钮,则旧Qt加载项会自动添加这些。 现在Qt Designer是一个单独的应用程序。 双击是不可能的。
您也可以使用自动连接 。 通过自动连接,您无需使用插槽连接信号。 具有特殊命名约定的函数,自动调用。 喜欢on_okButton_clicked 。
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.
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.
更多推荐
发布评论