Hello All,
In my application I am getting Page Source of requested page urls and assigning to QString than applying RegExp on Page Source data to fetch particular information.
My problem is some pages have Chinese and Japanese words which I need to get as it is and use them.
But when I am getting reply and assigning to QString or saving to file I got either ????? or some unpredictable characters instead of that Chinese or Japanese word.
I need to get that Chinese and Japanese word as it is for display in my application and also save to in a file.
Here is my code:
void MainWindow::on_btnGetSource_clicked()
{
QString url=ui->txtPageUrl->text();
QNetworkAccessManager *qname=new QNetworkAccessManager(this);
QNetworkReply *reply;
QNetworkRequest *request=new QNetworkRequest(QUrl(url));
request->setRawHeader( "Accept-Charset", "win1251,utf-8;q=0.7,*;q=0.7" );
request->setRawHeader( "charset", "utf-8" );
reply=qname->get(*request);
QObject::connect(reply,SIGNAL(finished()),this,SLOT(replyFinished()));
}
void MainWindow::replyFinished()
{
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
QString htmlString=QString::fromUtf8(reply->readAll());
reply->deleteLater();
//Code to Browse file and save pagesource string into file
QString outputFilename = QFileDialog::getSaveFileName(
this,
tr("Save Document"),
QDir::currentPath(),
tr("Document files (*.txt );;All files (*.*)"
));
try
{
QFile outputFile(outputFilename);
outputFile.open(QIODevice::WriteOnly | QIODevice::Text);
/* Check it opened OK */
if(!outputFile.isOpen())
{
qDebug("Unable to open file,Please try again.");
return;
}
QTextStream data( &outputFile );
data<<htmlString;
/* Close the file */
outputFile.close();
qDebug("Source Code Saved Successfully.");
}
catch(...)
{
qDebug("Unable to open file,Please try again.");
}
//End of save
//Append page source to palin text edit
ui->txtPageSource->appendPlainText(htmlString);
}
I tried with setting of QtCreator editor in FileEncoding (Default Encoding set UTF-8) but nothing changed.
Please help me out that how can I deal with this.
Thanks in Advance.
Zain
↧