Add button click handler in Qt project, Visual Studio

10,309

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.

Share:
10,309
Alex F
Author by

Alex F

Updated on June 13, 2022

Comments

  • Alex F
    Alex F almost 2 years

    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?