QT5 QWebEngineCallback and toHtml(), How do I implement this in my code? - c++

I am loading a webpage through QWebEngineView which I named myWeb. `
myWeb->load(QUrl("https://www.google.com"));
I want to grab the HTML source code from a website with myWeb->page()->toHtml(What goes here?);
Not sure how to satisfy toHtml's parameter. It says toHtml accepts..
const QWebEngineCallback<const QString &> &resultCallback
I have tried making a...
const QWebEngineCallback<const QString &>myCallback
and parameterizing toHtml with myCallBack
I use qDebug() with myCallBack but it outputs false
I want the HTML source code as a QString.

Related

QUrl doesn't work with parts from text file (Qt c++)

I want to make an url with some parts extracted from a text file.
I have the following code:
crack_order = new QFile("orders.txt"); //I have QFile *crack_order in headers
crack_order->open(QIODevice::ReadOnly);
while(!crack_order->atEnd()){
QStringList orders;
orders = QString(crack_order->readAll()).split('\n',QString::SkipEmptyParts);
foreach (QString order, orders){
ui->debugtextbox->setText(order); //until here, it works fine!
url = "http://www.example.com/customers/orders/"+order+"/another/thing.txt";
it does not work, the url request doesn't performed correctly. HOWEVER, if I put the following variable:
"QString order = "12233";
before "url", it works!
Then, I guess the problem is that it's getting the "order" from a text file, and QUrl doesn't like it.
Any idea?
Thanks in advanced.

How to get website content from QWebEnginePage?

I installed the newest version of Qt (on Webkit, Qt5.2 had WTFcrash). I try to get content of my website when the page is loaded (and it is):
QString sHtml;
view.page()->toHtml([&](const QString& result){sHtml = result;qDebug() << result;});
But sHtml is empty, and debug not called. What am I doing wrong?
You're not doing anything wrong, you're just calling an asynchronous function :
Asynchronous method to retrieve the page's content as HTML, enclosed
in HTML and BODY tags. Upon successful completion, resultCallback is
called with the page's content.
The HTML won't be available directly after the call to toHtml(). Instead, you can use some signals and slots to overcome this :
protected slots:
void handleHTML(QString sHTML);
signals:
void getHTML(QString sHTML);
void yourClass::yourFunction()
{
connect(this, SIGNAL(getHTML(QString)), this, SLOT(handleHTML(QString)));
view->page()->toHtml([this](const QString& result) mutable {emit getHTML(result);});
}
void yourClass::handleHTML(QString sHTML)
{
qDebug()<< "The HTML is :" << sHTML;
}
Found it, toPlainText work properly. Still don't know why toHtml doesn't.

Passing values through links in QT

I'm writing a program in C++ and qt4 that is supposed to generate a various number (hundreds )of clickable links in a QTextBrowser depending on data from input files.
The idea is that when the user clicks one of these links a value will be passed to a function named 'on_QTextBrowser_anchorClicked(QUrl)'.
I have created a QTextBrowser which displays HTML code and I manage to create different links to every element added. The problem lies within passing the Url defined in href="URL" to the QUrl.
When setOpenLinks for the QTextBrowser is "true", and I print the URL I get the correct result. But I can't pass this URL (which is a value and not a real URL) to a function.
When setOpenLinks is "false", the anchorClicked(Url) function passes "" as the Url and here I would like to have the URL printed when setOpenLinks=true.
How can I achieve this? Is there a better way (probably is) to connect a various number (approx. between 50-1000) of generated links to a function using QTextBrowser.
My code:
Compassindex.cpp
void CompassIndex::on_seqBrowser_anchorClicked(QUrl input)
{
QString Input = input.toString();
QByteArray nByte = Input.toUtf8();
std::cerr<<Input.data(); //Print Url on the screen to ensure value is passed
}
void CompassIndex::readfile()
{
QString Line;
Int number_out=0;
... //Imports data that will be printed in the QTextBrowser
... //loop for creating links for n number of elements
Line="<a href=\"";
Line+=number_out; //the value (fake Url) that I want to pass to anchorClicked()
Line+="\">";
Line+=nameArr[n]; //inserts the name for the link
Line+="</a>";
number_out++;
...
ui->seqBrowser->insertHtml(Line);
... //end of loop
}
Many thanks for your reply!
I'm using QTextBrowser to provide an actionable user interface for pqConsole. You can read more about in this answer.
I pass around Prolog invocations, and all seems to be working. My anchorClicked slot is as simple as
void ConsoleEdit::anchorClicked(const QUrl &url) {
query_run(url.toString());
}
Note I also have defined (as dummy)
void ConsoleEdit::setSource(const QUrl &name) {
qDebug() << "setSource" << name;
}
I don't touch setOpenLinks(), that defaults to true according to the docs.

Qt - Download Images from web

I am trying to write an application using Qt/C++ to download images from web. The program will be provided a list of URLs as a text file, and should open each URL and download all images in each website.
In order to do so, I subclass QNetworkAccessManager and override createRequest method/function like below:
QNetworkReply* clsNAM::createRequest(Operation op,const QNetworkRequest & req,
QIODevice * outgoingData )
{
return new clsNR(QNetworkAccessManager::createRequest(_op,_req,_outgoingData));
}
My subclass of QNetworkReply class, clsNR, identifies the type of the reply from its raw header, something like:
bool clsNR::isImage()
{
if(this->rawHeader("Content-Type").toLower().startsWith("image"))
return true;
else
return false;
}
As I get readyRead signal (which means there is data available to read) ,I store the coming in a QbyteArray. I have connected a slot in clsNR to save the image when the QNetworkReply is finished. It looks like:
void clsNR::slotCloseConnection()
{
if(isImage())
{
QImage image;
bool bool1 = image.loadFromData(ContentData);
QString name(QDir::homePath());
name.append(this->url().path());
bool bool2 = image.save(name,0,-1);
}
}
There is something wrong with my saving. bool1 is true, but bool2 is false. The reason I'm using the url path for the name is to generate unique name for images. When I print out the name, it's something like:
"/home/guest/images/splash/3.jpg"
So My questions would be:
What I'm doing wrong with QImage?
Can I use QFile and QStream to write down the images instead?
I'll add something else, like Mat said in your comment. If you need to check if the directory exists use Qdir::exists() and if you need to create it use QDir::mkdir()

Qt html parsing does not find any tags

I am writing a crawling application. Somewhere in the code Ive got:
//normally the HTML is obtained from web with QNetworkAccessManager & QNetworkReply:
//QString htmlCode = this->reply->readAll();
//exemplary test HTML
QString htmlCode =QString("<html><body>test3</body></html>");
QWebPage page;
QWebFrame * frame = page.mainFrame(); //->setHtml(htmlCode);
frame->setHtml(htmlCode);
QWebElement document = frame->documentElement();
QWebElementCollection links = document.findAll("a");
foreach (QWebElement e, links) {
qDebug() << "exemplary link:" << e.toPlainText();
}
I do realize, that there's been like a milion of topics about parsing html in qt here, but i have no idea, what is wrong here...
Mmm... I'm not sure the setHtml()works fully synchronously, i.e. I think the frame content is not fully parsed at that time and thus the DOM content is not yet available.
You should try to connect to void QWebFrame::loadFinished ( bool ok ) and do your DOM crawling there.