当前位置: 技术问答>linux和unix
QT编程遇到的编译错误
来源: 互联网 发布时间:2015-11-27
本文导语: 编译我的源文件MyThread.cpp时报错如下,总说connect()没声明,可是信号和槽的连接用声明么? MyThread.cpp: In constructor `MyThread::MyThread()': MyThread.cpp:16: error: `connect' undeclared (first use this function) MyThread.cpp:16: error: (Each und...
编译我的源文件MyThread.cpp时报错如下,总说connect()没声明,可是信号和槽的连接用声明么?
MyThread.cpp: In constructor `MyThread::MyThread()':
MyThread.cpp:16: error: `connect' undeclared (first use this function)
MyThread.cpp:16: error: (Each undeclared identifier is reported only once for each function it appears in.)
源代码如下:
/*MyThread.h*/
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include
#include
#include
#include
#include
/**
*MyThread.h文件摹拟用户访问Web Server,向web 服务器发送request请求包,这里通过
*get()请求实现对web服务器进行负载测试
*/
class QHttp;
class QFile;
class QMessageBox;
class MyThread: public QThread
{
Q_OBJECT
public:
MyThread();
void run();
void setData(int port,QString& host,int sleeptime,QString& filename);
void init();
void stop();
private:
QString host,filename;
int sleeptime,port;
volatile bool stopped;
QHttp http;
QFile file;
private slots:
void httpDone(bool error);
};
#endif
///////////////////////////////////////////
//MyThread.cpp
#include "MyThread.h"
/**
*缺省的参数:端口号为8080,主机为localhost,运行时间为600秒.每隔100
*ms向web服务器发送请求
*/
MyThread::MyThread()
:QThread()
{
port=8080;
host="localhost";
sleeptime=100;//ms
stopped=false;
filename="test";
// signals and slots connections
connect( &http,SIGNAL(done(bool)),this,SLOT(httpDone(bool))); //::::::::::: 报错的地方
}
void MyThread::run()
{
init();
while(!stopped)
{
if (!file.open(IO_WriteOnly)) {
QMessageBox::warning(new QDialog(), tr("HTTP Get"),
tr("Cannot write file %1n%2.")
.arg(file.name())
.arg(file.errorString()));
return;
}
http.get("/index.jsp",&file);
msleep(sleeptime);
http.closeConnection();
}
if(stopped)
{
file.close();
}
}
void MyThread::setData(int port,QString& host,int sleeptime,QString &filename)
{
this->port=port;
this->host=host;
this->sleeptime=sleeptime;
this->filename=filename;
}
void MyThread::init()
{
http.setHost(host,port);
file.setName(filename);
}
void MyThread::stop()
{
stopped=true;
}
void MyThread::httpDone(bool error)
{
if(error)
QMessageBox::warning(new QDialog(), tr("HTTP Get"),tr("Error while fetching file with""HTTP: %1.").arg(http.errorString()));
file.close();
}
MyThread.cpp: In constructor `MyThread::MyThread()':
MyThread.cpp:16: error: `connect' undeclared (first use this function)
MyThread.cpp:16: error: (Each undeclared identifier is reported only once for each function it appears in.)
源代码如下:
/*MyThread.h*/
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include
#include
#include
#include
#include
/**
*MyThread.h文件摹拟用户访问Web Server,向web 服务器发送request请求包,这里通过
*get()请求实现对web服务器进行负载测试
*/
class QHttp;
class QFile;
class QMessageBox;
class MyThread: public QThread
{
Q_OBJECT
public:
MyThread();
void run();
void setData(int port,QString& host,int sleeptime,QString& filename);
void init();
void stop();
private:
QString host,filename;
int sleeptime,port;
volatile bool stopped;
QHttp http;
QFile file;
private slots:
void httpDone(bool error);
};
#endif
///////////////////////////////////////////
//MyThread.cpp
#include "MyThread.h"
/**
*缺省的参数:端口号为8080,主机为localhost,运行时间为600秒.每隔100
*ms向web服务器发送请求
*/
MyThread::MyThread()
:QThread()
{
port=8080;
host="localhost";
sleeptime=100;//ms
stopped=false;
filename="test";
// signals and slots connections
connect( &http,SIGNAL(done(bool)),this,SLOT(httpDone(bool))); //::::::::::: 报错的地方
}
void MyThread::run()
{
init();
while(!stopped)
{
if (!file.open(IO_WriteOnly)) {
QMessageBox::warning(new QDialog(), tr("HTTP Get"),
tr("Cannot write file %1n%2.")
.arg(file.name())
.arg(file.errorString()));
return;
}
http.get("/index.jsp",&file);
msleep(sleeptime);
http.closeConnection();
}
if(stopped)
{
file.close();
}
}
void MyThread::setData(int port,QString& host,int sleeptime,QString &filename)
{
this->port=port;
this->host=host;
this->sleeptime=sleeptime;
this->filename=filename;
}
void MyThread::init()
{
http.setHost(host,port);
file.setName(filename);
}
void MyThread::stop()
{
stopped=true;
}
void MyThread::httpDone(bool error)
{
if(error)
QMessageBox::warning(new QDialog(), tr("HTTP Get"),tr("Error while fetching file with""HTTP: %1.").arg(http.errorString()));
file.close();
}