Get Image URL From String - c++

I am trying to figure out a way to get a image url from a web page source.
I can get the web page source into a string and parse it line by line to find the line with the URL.
However, I haven't been able to figure out a good way to pull just the URL from the line.
I'm think this can be done with QRegExp, but have been unable to figure out how to use it.
Line I am trying to parse
<img width="980" height="1515" id="mainImg" src="//test/123.jpg" alt="test">
Final Working Code
void MainWindow::on_btnDownload_clicked()
{
QString url = "http://test.foo.com";
QUrl qURL = url;
QNetworkAccessManager manager;
QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url)));
QEventLoop event;
connect(response,SIGNAL(finished()),&event,SLOT(quit()));
event.exec();
QString html = response->readAll();
QStringList str;
str = html.split("\n");
//qDebug() << url;
for (int i = 0; i < str.size(); ++i){
if(str.at(i).contains("id=\"mainImg\"", Qt::CaseInsensitive)){
QString pic;
pic = str.at(i);
pic = pic.remove(QRegExp("<img[^>]*src=['|\"]", Qt::CaseInsensitive));
pic = pic.remove(QString::fromStdString("//"), Qt::CaseInsensitive);
pic = pic.remove('"');
pic = pic.remove("'");
pic = pic.remove('<');
pic = pic.remove('>');
pic = pic.remove(';');
pic = pic.left(pic.length()-1);
//qDebug() << str.at(i);
qDebug() << pic;
}
}
qDebug() << "Lines: " << str.size();
}

Related

How can I get qjsonvalue to string?

What should i do to get output for example: Bid value is 2248.48?
Here is code:
QNetworkRequest request = QNetworkRequest(QUrl("https://api.30.bossa.pl/API/GPW/v2/Q/C/_cat_name/WIG20?_t=1637005413888"));
QNetworkReply* reply = m_manager.get(request);
QObject::connect(reply, &QNetworkReply::finished, [reply]() {
QByteArray rawData = reply->readAll();
QString textData(rawData);
// qDebug() << textData;
QJsonDocument doc = QJsonDocument::fromJson(textData.toUtf8());
auto rootObj = doc.object();
auto _d = rootObj.value("_d").toArray();
auto _t = _d[0].toObject().value("_t").toArray();
auto _quote = _t[0].toObject().value(QString("_quote"));
qDebug() << _quote;
eply->deleteLater();
Now i get QJsonValue (string, "2248.48)
when i tried this:
QJsonObject root = _t[0].toObject().value(QString("_quote"));
qDebug() << root;
QJsonValue value = obj.value(QString("_quote"));
qDebug() << "Bid value is" << value.toString();;
https://api.30.bossa.pl/API/GPW/v2/Q/C/_cat_name/WIG20?_t=1637005413888
{"message":"OK","_quote_date":null,"_type":"C","_symbol":["WIG20"],"_d":[{"_h":"Własne - 22 listopada 2021 16:42","_hs":"Własne","_max_quote_dtm":"22 listopada 2021","_max_quote_dtm_lc":"22 listopada, 16:42","_ret_quote_dtm":"2021-11-22","_t":[{"_symbol":"WIG20","_symbol_short":"WIG20","_group":"X1","_isin":"PL9999999987","_quote_date":"2021.11.22","_quote_time":"16:42","_time":"16:42","_phase":"Sesja","_quote_max":"2262.74","_quote_min":"2237.64","_quote_open":"2251.08","_quote_ref":"2248.18","_quote_imp":"2254.37","_bid_size":null,"_bid_volume":null,"_bid_orders_nr":null,"_ask_size":null,"_ask_volume":null,"_ask_orders_nr":null,"_volume":null,"_open_positions":null,"_quote_volume":null,"_transactions_nr":null,"_turnover_value":841977698,"_quote":"2254.10","_step":"2","_type_of_instrument":"0","_settlement_price":null,"_change_proc":0.26,"_change_pnts":5.9200,"_30d_change_max":2449.6400,"_30d_change_min":2221.6800,"_change_type":"_change_proc","_quote_type":"_quote","_debut":"0","_live":"1","_sw_symbol_short":0,"_is_indice":"1","_change":"+0.26","_change_suffix":"%","_change_max_min":"+1.12","_change_close_open":"+0.13","_change_settl_ref":null}]}],"_i":[null],"_count":1,"_d_fx":{"_h":null,"_hs":null,"_max_quote_dtm":null,"_max_quote_dtm_lc":null,"_t":[]}}
I got erros "QJsonValue to non-scalar type QJsonObject requested"
You either want _quote.toString() (first listing) or root.toString() (second listing)

Progress bar for copying files

I am trying to create a progress bar for a file copy in Qt. This is as close as I could find, however I believe this doesn't work because according to the Qt class documentation:
Unlike other QIODevice implementations, such as QTcpSocket, QFile does
not emit the aboutToClose(), bytesWritten(), or readyRead() signals.
This implementation detail means that QFile is not suitable for
reading and writing certain types of files, such as device files on
Unix platforms.
How can I do something like this? I don't know how to implement my own signals.
Here is my code:
void Replicator::anotbaandbFile(QDir source, QDir target)
{
source.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
target.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
qDebug() << "Scanning: " << source.path();
QStringList sourceFileList = source.entryList();
QStringList targetFileList = target.entryList();
for (int aCount = 0; aCount < sourceFileList.count(); aCount++)
{
bool found = false;
for (int bCount = 0; bCount < targetFileList.count(); bCount++)
if (sourceFileList.at(aCount) == targetFileList.at(bCount))
found = true;
if (found == false)
{
sourceFile = new QFile(source.absolutePath()+"/"+sourceFileList.at(aCount));
targetFile = new QFile(target.absolutePath()+"/"+sourceFileList.at(aCount));
progressBar->setMinimum(0);
progressBar->setMaximum(sourceFile->size());
written = 0;
connect(sourceFile,SIGNAL(bytesWritten(qint64)),SLOT(onWrite(qint64)));
sourceFile->copy(targetFile->fileName());
//QFile::copy(source.absolutePath()+"/"+sourceFileList.at(aCount), target.absolutePath()+"/"+sourceFileList.at(aCount));
qDebug() << source.absolutePath()+"/"+sourceFileList.at(aCount) << " " << target.absolutePath()+"/"+sourceFileList.at(aCount);
}
}
}
and
void Replicator::onWrite(qint64 w)
{
written += w;
progressBar->setValue( written );
}
new code modified from above
if (found == false)
{
sourceFile = new QFile(source.absolutePath()+"/"+sourceFileList.at(aCount));
targetFile = new QFile(target.absolutePath()+"/"+sourceFileList.at(aCount));
progressBar->setMinimum(0);
progressBar->setMaximum(sourceFile->size());
QByteArray buffer;
for (int count = 0; !(buffer = sourceFile->read(1000000)).isEmpty(); count+=1000000)
{
targetFile->write(buffer);
progressBar->setValue(count);
}
//targetFile->write(buffer);
//QFile::copy(source.absolutePath()+"/"+sourceFileList.at(aCount), target.absolutePath()+"/"+sourceFileList.at(aCount));
qDebug() << "copying " << sourceFile->fileName() << " to " << targetFile->fileName();
}
You can simply copy large files by portions of fixed size, than count portions already copied and callculate of percentage of work by dividing it to overal count of portions.
int iWorkPercentage = (int)(((float)iPortionsProcessed / (float)iOveralPortions) * 100);

Gumbo HTML text inside A

I'm using Gumbo to parse a web page in CP1251. I've converted the text into UTF-8 and sent it to gumbo parser. I have problem with getting text inside A link with
node->v.text.text
I get strange symbols on the output while the source is correctly displayed in the console.
I am using Qt 5.2 and libiconv for converting purposes.
Need I convert node text to local code page or what am I doing wrong?
Getting page in CP1251
QByteArray barrData = pf->getData();
size_t dstlen = 1048576;
char buf[dstlen];
memset((char*)buf, 0, dstlen);
char* pIn = barrData.data();
char* pOut = (char*)buf;
size_t srclen = barrData.size();
iconv_t conv = iconv_open("UTF-8", "CP1251");
iconv(conv, &pIn, &srclen, &pOut, &dstlen);
iconv_close(conv);
GumboOutput* output = gumbo_parse(buf);
parsePage(output->root);
gumbo_destroy_output(&kGumboDefaultOptions, output);
Parsing
if (node->v.element.tag == GUMBO_TAG_DIV && (_class = gumbo_get_attribute(&node->v.element.attributes, "class")))
{
if (QString(_class->value) == "catalog-item-title")
{
qDebug() << "parsePage: found product, parsing...";
GumboVector* children = &node->v.element.children;
for (int i = 0; i < children->length; ++i)
{
GumboNode* node = static_cast<GumboNode*>(children->data[i]);
GumboAttribute* href;
GumboAttribute* id;
if (node->v.element.tag == GUMBO_TAG_A &&
(href = gumbo_get_attribute(&node->v.element.attributes, "href"))
)
{
char buf[1024];
memset(buf, 0, 1024);
int i = node->v.text.original_text.length;
memcpy(buf, node->v.text.original_text.data, i);
QString strTitle = buf;
Q_ASSERT(node->v.text.original_text.length > 0);
qDebug() << "parsePage: found product" << strTitle << href->value;
break;
}
}
}
}
Source page text:
<div class="catalog-item-title">Измир 2</div>
I have smoked up examples at last. The text is contained inside child node.
if (node->v.element.tag == GUMBO_TAG_A &&
(href = gumbo_get_attribute(&node->v.element.attributes, "href"))
)
{
QString strTitle;
GumboNode* title_text = static_cast<GumboNode*>(node->v.element.children.data[0]);
if (title_text->type == GUMBO_NODE_TEXT)
{
strTitle = title_text->v.text.text;
}
qDebug() << "parsePage: found product" << strTitle << href->value;
break;
}

Proper way to handle multiple QNetworkRequests

im grabbing url's from a list and then sending each to a QNetworkRequest and recieving the HTML back to process. I however have thousands of requests to process. So my application kept hanging until i stopped it from producing all these requests at once.
Is this the rite way to handle the que for a great number of requests?
I tried using a Qqueue of Urls, which i would then link to a SLOT which was triggered after each QNetworkReply reponse.
Create the job list and add to que
QQueue<QString> jobs;
for (int i = 1; i <= totalPages; i++){
QString pageUrl = url + "&page=" + QString::number(i);
jobs.enqueue(pageUrl);
}
qDebug() << "Total Jobs : " << jobs.count() << endl;
for (int i = 0; i < 5; i++){
processQueue();
}
then inside the getHtml function
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkRequest *getHtmlRequest = new QNetworkRequest(pageUrl);
getHtmlRequest = new QNetworkRequest(url);
getHtmlRequest->setRawHeader( "User-Agent", "Mozilla/5.0 (X11; U; Linux i686 (x86_64); "
"en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1" );
getHtmlRequest->setRawHeader( "charset", "utf-8" );
getHtmlRequest->setRawHeader( "Connection", "keep-alive" );
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyGetPageHtmlFinished(QNetworkReply*)));
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processQueue()));
manager->get(*getHtmlRequest);
which triggers
void checkNewArrivalWorker::processQueue(){
if (jobs.isEmpty()){
qDebug() << "Jobs Completed" << endl;
emit finished();
} else {
QString pageUrl = jobs.dequeue();
QString pageNumber = pageUrl.mid(pageUrl.indexOf("page=") + 5);
getHtml(pageUrl, pageNumber);
}
}

QTableView printing

I am new on QT an I try to print out from QTableView
How can I do this?
Thank a lot
Here is a variation of the first answer that gets rid of the intermediate file.
QString strStream;
QTextStream out(&strStream);
const int rowCount = pPublic->tableView->model()->rowCount();
const int columnCount = pPublic->tableView->model()->columnCount();
out << "<html>\n"
"<head>\n"
"<meta Content=\"Text/html; charset=Windows-1251\">\n"
<< QString("<title>%1</title>\n").arg(strTitle)
<< "</head>\n"
"<body bgcolor=#ffffff link=#5000A0>\n"
"<table border=1 cellspacing=0 cellpadding=2>\n";
// headers
out << "<thead><tr bgcolor=#f0f0f0>";
for (int column = 0; column < columnCount; column++)
if (!pPublic->tableView->isColumnHidden(column))
out << QString("<th>%1</th>").arg(pPublic->tableView->model()->headerData(column, Qt::Horizontal).toString());
out << "</tr></thead>\n";
// data table
for (int row = 0; row < rowCount; row++) {
out << "<tr>";
for (int column = 0; column < columnCount; column++) {
if (!pPublic->tableView->isColumnHidden(column)) {
QString data = pPublic->tableView->model()->data(pPublic->tableView->model()->index(row, column)).toString().simplified();
out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString(" "));
}
}
out << "</tr>\n";
}
out << "</table>\n"
"</body>\n"
"</html>\n";
QTextDocument *document = new QTextDocument();
document->setHtml(strStream);
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer, NULL);
if (dialog->exec() == QDialog::Accepted) {
document->print(&printer);
}
delete document;
here is my solution of a problem. May be it's too complex... But here you could find more solutions!
1). first I have saved table data to HTML page file:
bool CRefViewerDlg::createHtmlTableFromModel() {
// make a html-dump of table view
if (tableView) {
const QString htmlFileName = QString("%1/%2").arg(qApp->applicationDirPath()).arg("myTable.html");
QFile file(htmlFileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
MSG(QString("Can`t create file %1").arg(htmlFileName));
return false;
}
QTextStream out(&file);
const xbLong rowCount = tableView->model()->rowCount();
const xbLong columnCount = tableView->model()->columnCount();
out << "<html>\n"
"<head>\n"
"<meta Content=\"Text/html; charset=Windows-1251\">\n"
<< QString("<title>%1</title>\n").arg(refTitleName)
<< "</head>\n"
"<body bgcolor=#ffffff link=#5000A0>\n"
"<table border=1 cellspacing=0 cellpadding=2>\n";
// headers
out << "<tr bgcolor=#f0f0f0>";
for (xbLong column = 0; column < columnCount; column++)
if (!tableView->isColumnHidden(column))
out << QString("<th>%1</th>").arg(tableView->model()->headerData(column, Qt::Horizontal).toString());
out << "</tr>\n";
file.flush();
// data table
for (xbLong row = 0; row < rowCount; row++) {
out << "<tr>";
for (xbLong column = 0; column < columnCount; column++) {
if (!tableView->isColumnHidden(column)) {
QString data = tableView->model()->data(tableView->model()->index(row, column)).toString().simplified();
out << QString("<td bkcolor=0>%1</td>").arg((!data.isEmpty()) ? data : QString(" "));
}
}
out << "</tr>\n";
}
out << "</table>\n"
"</body>\n"
"</html>\n";
file.close();
}
return true;
}
2). after I have saved html content to file, it was opened in html view window, where I could print the document with QTextBrowser class:
void CLiveListDlg::on_printPageToolButton_clicked() {
#ifndef QT_NO_PRINTER
QTextBrowser *editor = static_cast<QTextBrowser* >(textBrowser);
QPrinter printer;
QPrintDialog *dialog = new QPrintDialog(&printer, this);
dialog->setWindowTitle(tr("Print Document"));
if (editor->textCursor().hasSelection())
dialog->addEnabledOption(QAbstractPrintDialog::PrintSelection);
if (dialog->exec() != QDialog::Accepted)
return;
editor->print(&printer);
#endif
}
How about this one?
Traverse through the model of your QTableView let's say QStandardItemModel. Obtain the each and individual texts of the items available in the QStandardItemModel.
Now using QTextCursor, insert the obtained texts from your model into QTextDocument. You can make use of the examples given here to insert text into QTextDocument.
After the completion of inserting into QTextDocument, you can print the contents available in the QTextDocument through
void QTextDocument::print ( QPrinter * printer ) const
The thing you have to make sure is that you should be able to traverse through each items so that you can able obtain all the item text from the model.
Hope it helps..