Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
一个小例子:
class libcurlParser : public QThread
{
    Q_OBJECT
public:
    
static size_t process(void* buffer,size_t size,size_t n,void *user);
    QString html;
public:
    libcurlParser(QObject 
*parent= 0);
    
~libcurlParser();
public:
    
void execute(const QString& url);
public slots:
    
void finishSlot();
    
void timeOut();
signals:
    
void finish();
protected:
    
void run();
private:
    CURL
*  curl;
};

size_t libcurlParser::process(void* buffer,size_t size,size_t n,void *user)
{
    libcurlParser
* parser = (libcurlParser*)user;
    parser
->html += (char*)buffer;
    
return size*n;
}

libcurlParser::libcurlParser(QObject 
*parent)
    : QThread(parent)
{   
    connect(
this,SIGNAL(finish()),this,SLOT(finishSlot()));
    curl 
= curl_easy_init();
}

libcurlParser::
~libcurlParser()
{    
    curl_easy_cleanup(curl);
}

void libcurlParser::execute(const QString& url)
{   
    html.clear();
    curl_easy_setopt(curl,CURLOPT_URL,url.toAscii().data());
    curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,
&process);
    curl_easy_setopt(curl,CURLOPT_WRITEDATA,
this);
    start();
}

void libcurlParser::run()
{   
    CURLcode code 
= curl_easy_perform(curl);
    std::cout
<<"exe:"<<"code"<<std::endl;
    emit finish();
    
//std::cout<<curl_easy_strerror(code)<<std::endl;
    
//QTimer::singleShot(0, this, SLOT(timeOut()));
    
//exec();
}

void libcurlParser::finishSlot()
{
    std::cout
<<"finishSlot()"<<std::endl;
    QFile file(
"htm.htm");
    file.open(QIODevice::WriteOnly);
    file.write(html.toLatin1());
    file.close();
}

void libcurlParser::timeOut()
{
    emit finish();
}
posted on 2012-05-27 17:07 ccsdu2009 阅读(1341) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理