我不断收到错误错误:未定义引用'同时努力学习在Qt中发展。该程序旨在通过线程GUI进行练习并使用此信号和插槽功能。
标题文件:
mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QObject>
#include <QThread>
class MyThread : public QThread
{
Q_OBJECT
public:
MyThread(QObject *parent = 0);
~MyThread();
void run();
bool Stop;
signals:
void numberchange(int);
};
#endif // MYTHREAD_H
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "mythread.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
MyThread *mthread;
private slots:
void on_startbtn_clicked();
void onNumberChange(int);
void on_stop_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
源文件: mythread.cpp
#include "mythread.h"
#include <QMutex>
MyThread::MyThread(QObject *parent): QThread(parent)
{
Stop = true;
}
MyThread::~MyThread()
{
}
void MyThread::run()
{
QMutex mutex;
static int i = 0;
for(;i < 10000; i++)
{
mutex.lock();
if(this->Stop)
break;
mutex.unlock();
emit numberchange(i);
this->msleep(1000);
}
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "mythread.h"
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mthread = new MyThread(this);
connect(mthread, &MyThread::numberchange, this, &MainWindow::onNumberChange); // I get an error on this line for the numberchanged function, both numberchange and onNumberChange have the same arguments
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_startbtn_clicked()
{
mthread->start();
}
void MainWindow::on_stop_clicked()
{
mthread->Stop = false;
}
void MainWindow::onNumberChange(int number)
{
ui->label->setText(QString::number(number));
}
错误:
C:\Qt\Tools\QtCreator\bin\Threaded_GUI\mainwindow.cpp:12: error: undefined reference to `MyThread::numberchange(int)'
C:\Qt\5.4\mingw491_32\include\QtCore\qobject.h:239: error: undefined reference to `MyThread::staticMetaObject'
C:\Qt\Tools\QtCreator\bin\Threaded_GUI\mythread.cpp:4: error: undefined reference to `vtable for MyThread'
C:\Qt\Tools\QtCreator\bin\Threaded_GUI\mythread.cpp:9: error: undefined reference to `vtable for MyThread'
C:\Qt\Tools\QtCreator\bin\Threaded_GUI\mythread.cpp:24: error: undefined reference to `MyThread::numberchange(int)'
collect2.exe:-1: error: error: ld returned 1 exit status
我不知道为什么我在第一次
中的连接功能时出现错误