Qt code does not compile when I try to connect a single signal to a slot

I am new to Qt. I am trying to implement a really simple calculator program. Just trying to place the button, and when it is clicked, I want it to print "Hello, World!" to the next line Edit. It works fine when I have only one button, but when I add a second, it does not compile. And since I am coding a calculator, I need these buttons.

Here are the errors:

C:\Users\user\Desktop\Calc\build-Calc-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\moc_mainwindow.o:-1: In function `ZN10MainWindow18qt_static_metacallEP7QObjectN11QMetaObject4CallEiPPv': C:\Users\user\Desktop\Calc\build-Calc-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\moc_mainwindow.cpp:75: error: undefined reference to `MainWindow::on_pushButton_clicked()' C:\Users\user\Desktop\Calc\build-Calc-Desktop_Qt_5_3_0_MinGW_32bit-Debug\debug\moc_mainwindow.cpp:76: error: undefined reference to `MainWindow::on_pushButton_2_clicked()' 

here is the MainWindow method:

 void MainWindow::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a) { if (_c == QMetaObject::InvokeMetaMethod) { MainWindow *_t = static_cast<MainWindow *>(_o); switch (_id) { case 0: _t->on_pushButton_clicked(); break; case 1: _t->on_pushButton_2_clicked(); break; case 2: _t->on_pushButton_11_clicked(); break; default: ; } } Q_UNUSED(_a); } 

and this is how I make the connection:

 #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } // this is the problematic part void MainWindow::on_pushButton_11_clicked() { ui->lineEdit->setText("Hello, World!"); } 

Does anyone know how to fix this? Thank you for your time.

+6
source share
1 answer

// This is the problematic part

void MainWindow :: on_pushButton_11_clicked ()

Really.

You were unable to complete the following two methods:

 MainWindow::on_pushButton_clicked() { ui->lineEdit->setText("Hello, World 2!"); } 

and

 MainWindow::on_pushButton_2_clicked() { ui->lineEdit->setText("Hello, World 3!"); } 

So it seems you have three slots, not two. You will need to implement the rest in accordance with your desire. Please note that the texts above are only placeholders for any actions that you plan to do in it.

+5
source

All Articles