简单Qt程序中未定义的引用。

时间:2022-11-21 05:30:06

I'm a beginner making a program in Qt creator. I made a button that should open Google Chrome using QtProcess::execute(), but I'm getting following errors:

我是一个初学者在Qt创建程序。我做了一个按钮,应该使用QtProcess来打开谷歌Chrome::execute(),但是我有以下错误:

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:

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:

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?

我做错了什么?

1 个解决方案

#1


4  

You have this in your .h file:

你在。h文件中有这个:

public slots:
    void buttonClickHandler();

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

它是一个方法声明,它是一个Qt插槽,因此Qt moc将生成引用它的代码(为连接信号调用)。然后链接器尝试链接这些代码来创建应用程序二进制文件。但是,您在任何地方都没有方法定义(实际代码)。

3 possible fixes:

3可能的修复:

1.

Remove that slot declaration from the .h file, since you don't seem to be using it.

从.h文件中删除这个slot声明,因为您似乎没有使用它。

2.

Add defionition by changing above snippet to this in the .h file:

通过在.h文件中更改上面的代码片段,添加挑战。

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

This turns the declaration into a definition (of inline member function).

这将声明变成了一个定义(内联成员函数)。

3.

Alternatively, add method definition to the .cpp file, like you have for your other methods:

或者,向.cpp文件添加方法定义,就像您对其他方法一样:

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

#1


4  

You have this in your .h file:

你在。h文件中有这个:

public slots:
    void buttonClickHandler();

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

它是一个方法声明,它是一个Qt插槽,因此Qt moc将生成引用它的代码(为连接信号调用)。然后链接器尝试链接这些代码来创建应用程序二进制文件。但是,您在任何地方都没有方法定义(实际代码)。

3 possible fixes:

3可能的修复:

1.

Remove that slot declaration from the .h file, since you don't seem to be using it.

从.h文件中删除这个slot声明,因为您似乎没有使用它。

2.

Add defionition by changing above snippet to this in the .h file:

通过在.h文件中更改上面的代码片段,添加挑战。

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

This turns the declaration into a definition (of inline member function).

这将声明变成了一个定义(内联成员函数)。

3.

Alternatively, add method definition to the .cpp file, like you have for your other methods:

或者,向.cpp文件添加方法定义,就像您对其他方法一样:

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