Undefined link in a simple Qt program

I am a beginner creating a program in the creator of Qt. I made a button that should open Google Chrome using QtProcess::execute(), but I get the following errors:

F:\Users\Amol-2\Desktop\Imp Docs\C++ apps\build-QtMainLProject-Desktop_Qt_5_2_0_MinGW_32bit-Debug\debug\moc_mainwindow.cpp:71: error: undefined reference to `MainWindow::buttonClickHandler()'`
:-1: error:  ld returned 1 exit status

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_openChrome_clicked()
{
    QString exeloc = "F:\\Users\\Amol-2\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
    QProcess::execute(exeloc);
}

mainwindow.h:

    namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void buttonClickHandler();


public slots:
    void on_openChrome_clicked();

private:
    Ui::MainWindow *ui;
};

What am I doing wrong?

+3
source share
1 answer

You have this in your file .h:

public slots:
    void buttonClickHandler();

This is a method declaration, and it is a Qt slot, so Qt moc will generate a code that references it (call it for connected signals, etc.). And then the linker tries to link this code to create your application in binary. But you don't have a defintion method (actual code) anywhere, it seems.

3 possible fixes:

1.

.h, , , .

2.

defionition, .h:

public slots:
    void buttonClickHandler() { /* add code if you want some */ }

( -).

3.

.cpp, :

void MainWindow::buttonClickHandler() {
    // your code here
}
+4

All Articles