读书人

自定义信号报错有关问题

发布时间: 2012-12-17 09:31:40 作者: rapoo

自定义信号报错问题。
编译总是出现类似报错:

undefined reference to `myThread::RecDir(QString);

RecDir( QString ) 是我自定义的一个信号。


#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>

class myThread : public QThread
{

public:

void run() ;

bool FindFile( QString path );

signals:

void RecDir( QString Dir );

void RecFile( QString File );
};

#endif // MYTHREAD_H


.CPP


void myThread::run()
{
FindFile( path );

}

bool myThread::FindFile(QString path)
{
QDir Dir(path);

if (!Dir.exists())
return false;

Dir.setFilter(QDir::Dirs|QDir::Files);

Dir.setSorting(QDir::DirsFirst);

QFileInfoList list = Dir.entryInfoList();

int i=0;

do{
QFileInfo fileInfo = list.at(i);

if(fileInfo.fileName()=="." || fileInfo.fileName()==".."){

i++;

continue;
}

bool bisDir=fileInfo.isDir();

if(bisDir){

Dirs.push_back( fileInfo.fileName() );

emit RecDir( fileInfo.filePath() );

FindFile(fileInfo.filePath());

}else{

emit RecFile( fileInfo.filePath() );

Files.push_back( fileInfo.filePath() );
}

i++;
}

while(i<list.size());
}



不是我不愿意给分,是我只有28分,愿意拿27出来求解。
[最优解释]
我记得QThread好像不是集成于QObject的
那你这个地方应该这么写

class myThread : public QThread,public QObject
{
Q_OBJECT
public:
...
};

[其他解释]
QDir::Files);



Dir.setSorting(QDir::DirsFirst);

QFileInfoList list = Dir.entryInfoList();

int i=0;

do{
QFileInfo fileInfo = list.at(i);

if(fileInfo.fileName()=="."
[其他解释]

引用:
我记得QThread好像不是集成于QObject的
那你这个地方应该这么写

class myThread : public QThread,public QObject
{
Q_OBJECT
public:
...
};

+1
必须要有Q_OBJECT宏
[其他解释]
引用:
我记得QThread好像不是集成于QObject的
那你这个地方应该这么写

class myThread : public QThread,public QObject
{
Q_OBJECT
public:
...
};


是的,是这个问题,不过又出了另一个报错 'QObject' is an ambiguous base of

在main.cpp中报错的。

#include <QtGui/QApplication>
#include "widget.h"
#include "myThread.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
myThread th;
th.start();
QObject::connect( &th, SIGNAL(RecDir(QString)), &w, SLOT(slot_RecDir(QString)));
QObject::connect( &th, SIGNAL(RecFile(QString)), &w, SLOT(slot_RecFiles(QString)));
w.show();

return a.exec();
}

[其他解释]

我看了下
应该是我整错了 QThread是继承自QObject的
建议你还是定义下myThread的构造函数
myThread::mythread(QObject *parent):QThread(parent)
{
}
[其他解释]
引用:

我看了下
应该是我整错了 QThread是继承自QObject的
建议你还是定义下myThread的构造函数
myThread::mythread(QObject *parent):QThread(parent)
{
}


您能给我一个聊天工具的联系方式不
[其他解释]
引用:

我看了下
应该是我整错了 QThread是继承自QObject的
建议你还是定义下myThread的构造函数
myThread::mythread(QObject *parent):QThread(parent)
{
}

我没别的意思,这样交流太不方便,我想给把源码给您看看,这样方便些。
[其他解释]
有几个问题~
1 run不要写成public, 最好写成protected的
2 如果子线程想要收到信号,必须在run中。exec()。
3 signal slot是基于信号槽的。所以必须要用Q_OBJECT。
4 仔细检查拼写哈。
[其他解释]
引用:
有几个问题~
1 run不要写成public, 最好写成protected的
2 如果子线程想要收到信号,必须在run中。exec()。
3 signal slot是基于信号槽的。所以必须要用Q_OBJECT。
4 仔细检查拼写哈。

我都改过了,但是还是有报错,这东西都折腾我两天了亲。
我是黔驴技穷了。
------其他解决方案--------------------



#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>

class myThread : public QThread
{
Q_OBJECT
public:
myThread();

protected:

void run() ;

bool FindFile( QString path );

signals:

void RecDir( QString Dir );

void RecFile( QString File );
};

#endif // MYTHREAD_H


.cpp


#include <vector>
#include <QFileDialog>
#include "qdir.h"
#include "myThread.h"
#include "widget.h"

std::vector<QString>Dirs;
std::vector<QString>Files;

myThread::myThread()
{

}
void myThread::run()
{
FindFile( path );

emit RecDir("a");
}

bool myThread::FindFile(QString path)
{
QDir Dir(path);

if (!Dir.exists())
return false;

Dir.setFilter(QDir::Dirs
[其他解释]
fileInfo.fileName()==".."){

i++;

continue;
}

bool bisDir=fileInfo.isDir();

if(bisDir){

Dirs.push_back( fileInfo.fileName() );

//emit RecDir( fileInfo.filePath() );

FindFile(fileInfo.filePath());

}else{

//emit RecFile( fileInfo.filePath() );

Files.push_back( fileInfo.filePath() );
}

i++;
}

while(i<list.size());
}
这是源码,求解

[其他解释]
报啥错?错误提示是啥?
[其他解释]
引用:
报啥错?错误提示是啥?




#include <vector>
#include <QFileDialog>
#include "qdir.h"
#include "myThread.h"
#include "widget.h"

std::vector<QString>Dirs;
std::vector<QString>Files;



myThread::myThread()
{

}
void myThread::run()
{
FindFile( path );

}

bool myThread::FindFile(QString path)
{
QDir Dir(path);

if (!Dir.exists())
return false;

Dir.setFilter(QDir::Dirs
[其他解释]
QDir::Files);

Dir.setSorting(QDir::DirsFirst);

QFileInfoList list = Dir.entryInfoList();

int i=0;

do{
QFileInfo fileInfo = list.at(i);

if(fileInfo.fileName()=="."
[其他解释]
fileInfo.fileName()==".."){

i++;

continue;
}

bool bisDir=fileInfo.isDir();

if(bisDir){

Dirs.push_back( fileInfo.fileName() );

emit RecDir( fileInfo.filePath() );

FindFile(fileInfo.filePath());

}else{

emit RecFile( fileInfo.filePath() );

Files.push_back( fileInfo.filePath() );
}

i++;
}

while(i<list.size());
}



myThread.h:7: undefined reference to `vtable for myThread'
debug/myThread.o: In function `myThread':

myThread.cpp:10: undefined reference to `vtable for myThread'

undefined reference to `vtable for myThread'

undefined reference to `myThread::RecDir(QString)'

读书人网 >QT开发

热点推荐