开发者

Add button click handler in Qt project, Visual Studio

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 B开发者_StackOverflow社区uttonClicked 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜