使用的QT图形界面,用libcurl获取的网页,在之中遇见了很多问题,一直想用c++类封装一下libcurl,发现c++很不到家啊。索性用了友元函数。
先贴上代码吧
main.cpp
View Code
#include < stdio.h > #include < stdlib.h > #include < unistd.h > #include < curl / curl.h > #include < curl / types.h > #include < curl / easy.h > #include < QtGui / QApplication > #define mydebug 0 using namespace std;#include " Dict.h " FILE * fp; // 定义FILE类型指针 size_t write_data( void * ptr, size_t size, size_t nmemb, void * stream){ int written = fwrite(ptr, size, nmemb, (FILE * )fp); return written;} int getpage(QString aUrl){ CURL * curl; QString mUrl = "" ; mUrl.append( " http://dict.baidu.com/s?wd= " ); mUrl.append(aUrl); mUrl.append( " &tn=dict " ); curl_global_init(CURL_GLOBAL_ALL); curl = curl_easy_init(); QByteArray ba = mUrl.toLatin1(); const char * c_str2 = ba.data(); curl_easy_setopt(curl,CURLOPT_URL,c_str2); if ((fp = fopen( " /home/archy/newfile " , " w " )) == NULL) { curl_easy_cleanup(curl); exit( 1 ); } curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_perform(curl); curl_easy_cleanup(curl); return 0 ;} int main( int argc, char * argv[]){ QApplication a(argc, argv); Dialog w; w.show(); return a.exec();}
Dict.h
#ifndef DICT_H #define DICT_H #include < QtGui > #include " translate.h " #include " one.h " class QPushButton; class QLineEdit; class QTextEdit; class Dialog : public QDialog{ Q_OBJECT public : Dialog(QWidget * parent = 0 ); ~ Dialog(); friend int getpage(QString aUrl); QString getResult(); private slots: void clearItem(); void searchItem(); void UpdateClearBtn(); void UpdateSearchBtn(); private : QPushButton * mClearBtn; QPushButton * mSearchBtn; QLineEdit * mItem; QTextEdit * mResult; one * aone;}; #endif // DICT_H
Dict.cpp
View Code
1 #include < QtGui / QHBoxLayout > 2 #include < QtGui / QVBoxLayout > 3 #include " Dict.h " 4 #include < QMessageBox > 5 6 extern int getpage(QString aUrl); 7 8 9 10 Dialog::Dialog(QWidget * parent) 11 : QDialog(parent) 12 { 13 // init class of translate 14 // aone = new one(); 15 // init controls 16 mClearBtn = new QPushButton( " Clear " ); 17 mSearchBtn = new QPushButton( " Search " ); 18 19 mClearBtn -> setFlat( 1 ); 20 mClearBtn -> setFixedSize( 50 , 30 ); 21 mSearchBtn -> setFlat( 1 ); 22 mSearchBtn -> setFixedSize( 50 , 30 ); 23 24 mItem = new QLineEdit; 25 mResult = new QTextEdit; 26 27 28 29 connect(mClearBtn,SIGNAL(clicked()), this ,SLOT(clearItem())); 30 connect(mSearchBtn,SIGNAL(clicked()), this ,SLOT(searchItem())); 31 32 33 34 QHBoxLayout * aHbox = new QHBoxLayout; 35 aHbox -> addWidget(mClearBtn); 36 aHbox -> addWidget(mItem); 37 aHbox -> addWidget(mSearchBtn); 38 39 QVBoxLayout * aVbox = new QVBoxLayout; 40 aVbox -> addLayout(aHbox); 41 aVbox -> addWidget(mResult); 42 setLayout(aVbox); 43 } 44 45 46 47 QString Dialog::getResult() 48 { 49 FILE * fp; 50 51 char tem; 52 char buffer[ 2000 ],final[ 200 ]; 53 int line = 1 ,counti = 0 ,token = 0 ,finalcount = 0 ; 54 55 fp = fopen( " /home/archy/newfile " , " r " ); 56 57 while ((tem = fgetc(fp)) != EOF) 58 { 59 if (tem == ' \n ' ) 60 { 61 line ++ ; 62 if (line == 427 ) 63 { 64 break ; 65 } 66 } 67 // line++; 68 } 69 70 fgets(buffer, 2000 ,fp); 71 72 while (buffer[counti] != ' \n ' && counti < 2000 ) 73 { 74 if (buffer[counti] == ' < ' ) 75 { 76 token = 0 ; 77 } 78 else if (buffer[counti] == ' > ' ) 79 { 80 token = 1 ; 81 } 82 else if (buffer[counti] != ' > ' && buffer[counti] != ' < ' && token == 1 ) 83 { 84 final[finalcount] = buffer[counti]; 85 finalcount ++ ; 86 } 87 counti ++ ; 88 } 89 90 final[finalcount] = ' \0 ' ; 91 92 // QMessageBox msgBox; 93 // msgBox.setText(final); 94 // msgBox.exec(); 95 96 // 97 QString string = QString(QLatin1String(final)); 98 // QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312")); 99 QTextCodec::setCodecForCStrings(QTextCodec::codecForName( " UTF-8 " )); 100 return string ; 101 } 102 103 104 void Dialog::clearItem() 105 { 106 mItem -> setText( "" ); 107 } 108 109 void Dialog::searchItem() 110 { 111 112 113 if (mItem -> text() != "" ) 114 { 115 getpage(mItem -> text()); 116 QString astring = getResult(); 117 118 // QMessageBox msgBox; 119 // msgBox.setText(astring); 120 // msgBox.exec(); 121 122 mResult -> setText(astring); 123 } 124 } 125 126 127 Dialog:: ~ Dialog() 128 { 129 130 }
还有一个问题就是乱码的问题。
因为是提取的网页,只能一个char一个char那样提取,现在还是没有想到什么其他办法提取。之后在char[] 转 QString 的时候,总是乱码。
哪位博友知道怎么解决的话,请给点线索。