Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add button click handler in Qt project, Visual Studio

Tags:

c++

windows

qt

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?

like image 734
Alex F Avatar asked Aug 17 '11 11:08

Alex F


1 Answers

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.

like image 79
useraged Avatar answered Sep 28 '22 06:09

useraged