get HTML from QWebEnginePage in QWebEngineView using Lamda - c++

I want to get the HTML code of a web page opened in QWebEngineView I use toHtml() function in QWebEnginePage Class like this
QWebEnginePage *page = ui->widget->page();
QString HTML = "";
page->toHtml([&HTML](QString html){qDebug() << "code \n\n\n" << html;});
the HTML code of html page appeared in qDebug good without problem
the problem here is when I want to use HTML string outside the function when I show the size of the HTML varible it is equal to zero and empty
so I tried this
QWebEnginePage *page = ui->widget->page();
QString HTML = "";
page->toHtml([&HTML](QString html){HTML = html;}); // crash
qDebug() << "i want to use HTML here outside the function = " << HTML;
but the app crash show so what should I do so I put the HTML data in the HTML variable so I can use it outside the function
Thanks in advance

Your problem is caused by the fact that the lambda is run asynchronously. So it is really called after you have exited the method in which you call toHtml method and that also explains the crash - HTML is a local variable within the method which has already exited so the lambda just randomly corrupts the memory used to be occupied by HTML variable.
What you want to do here is to synchronize things i.e. block your method until the lambda is executed. It can be done with QEventLoop but that would need to involve sending a special signal from the lambda to indicate the fact that the lambda finished executing. So it would look somewhat like this (non-tested):
class MyClass: public QObject
{
Q_OBJECT
public:
MyClass(QWebEnginePage & page, QObject * parent = 0);
void notifyHtmlReceived();
QString getHtml();
void setHtml(const QString & html) { m_html = html; }
Q_SIGNALS:
void htmlReceived();
private Q_SLOTS:
void requestHtmlFromPage();
private:
QWebEnginePage & m_page;
QString m_html;
};
MyClass::MyClass(QWebEnginePage & page, QObject * parent) :
QObject(parent),
m_page(page)
{}
void MyClass::notifyHtmlReceived()
{
emit htmlReceived();
}
QString MyClass::getHtml()
{
QEventLoop loop;
QObject::connect(this, SIGNAL(htmlReceived()), &loop, SLOT(quit()));
// Schedule the slot to run in 0 seconds but not right now
QTimer::singleShot(0, this, SLOT(requestHtmlFromPage()));
// The event loop would block until the lambda receiving the HTML is executed
loop.exec();
// If we got here, the html has been received and the result was saved in m_html
return m_html;
}
void MyClass::requestHtmlFromPage()
{
m_page.toHtml([this](QString html)
{
this->setHtml(html);
this->notifyHtmlReceived();
});
}

Related

Test method with Signals and Slots

I have the following two simple methods in a Qt Console Application:
void Webservice::getFile(PostElement el)
{
parameter = &el;
QUrl url(PATH);
QUrlQuery query;
query.addQueryItem(el.getParam(), el.getValue());
url.setQuery(query);
request.setUrl(url);
manager->get(request);
connect(manager, SIGNAL(finished(QNetworkReply*)), this,
SLOT(downloadCompleted(QNetworkReply*)));
}
void Webservice::downloadCompleted(QNetworkReply *reply)
{
QByteArray b = reply->readAll();
qDebug() << "Download completed!";
QFile file("/tmp/" + parameter->getValue());
file.open(QIODevice::WriteOnly);
file.write(b);
file.close();
reply->deleteLater();
}
I created a QTest project to test getFile method.
#include <QtTest>
#include <QCoreApplication>
// add necessary includes here
#include "webservice.h"
class WebserviceTest : public QObject
{
Q_OBJECT
public:
WebserviceTest();
~WebserviceTest();
private:
Webservice ws;
private slots:
void initTestCase();
void cleanupTestCase();
void getFile();
};
WebserviceTest::WebserviceTest()
{
}
WebserviceTest::~WebserviceTest()
{
}
void WebserviceTest::initTestCase()
{
}
void WebserviceTest::cleanupTestCase()
{
}
void WebserviceTest::getFile()
{
PostElement el{"file", "myPic.png"};
ws.getFile(el); // it should wait for the slot to be executed
}
QTEST_MAIN(WebserviceTest)
#include "tst_webservicetest.moc"
As you can see, it has signals and slots. If I run that one on my original project, I see that downloadCompleted executes without issues (because it is using the Event Loop). But in the test project the slot is not called. I googled and found some examples using QSignalSpy, but at the moment I have not executed the slot successfully.
How to tell my test to "wait" for the slot to be completed?
That's exactly the purpose of QSignalSpy, it basically spins new QEventLoop that waits for signal to be executed. Your tests starts and asynchronous operation but it doesn't wait for it to finish so it will destruct objects while the operation is ongoing.
Qt test also has some built-in timeouts that prevents tests to be stuck which are stopped when QSignalSpy is used. In your case you need to emit new signal from your class Webservice indicating that download is finished, e.g. (you can also pass arguments to the signal) if needed:
class Webservice : public QObject {
Q_OBJECT
// Constructors etc.
signals:
void finished();
}
Then at the end of your void Webservice::downloadCompleted(QNetworkReply *reply):
emit finished();
and in your test something like this:
PostElement el{"file", "myPic.png"};
// Prepare spy
QSignalSpy spy(&ws, SIGNAL(finished));
ws.getFile(el);
// Wait for result - by default this waits 5 seconds
spy.wait();
// Check if we had a timeout or actually signal was raised
QCOMPARE(spy.count(), 1);

QT C++ download multiple files with progress total

I realized downloading multiple files, but I don't know how to implement the total progress bar of the download, that is common.
My code:
QNetworkAccessManager manager;
QList<QNetworkReply *> currentDownloads;
void MainWindow::checkUpdate()
{
QStringList files;
files << "http://cavexp.net/uploads/game/Theugry/zips/resourcepacks.zip"
<< "http://cavexp.net/uploads/game/Theugry/zips/resourcepacks.zip";
doDownload(files);
}
void MainWindow::doDownload(const QVariant& v)
{
if (v.type() == QVariant::StringList) {
foreach (QString url, v.toStringList()) {
QNetworkReply* reply = manager.get(QNetworkRequest(QUrl(url)));
connect(&manager, SIGNAL(downloadProgress(qint64, qint64)),
this, SLOT(updateDownloadProgress(qint64, qint64)));
currentDownloads.append(reply);
}
}
}
void MainWindow::downloadFinished(QNetworkReply *reply)
{
currentDownloads.removeAll(reply);
reply->deleteLater();
}
void MainWindow::updateDownloadProgress(qint64 bytesRead, qint64 totalBytes)
{
ui->progressBar->setMaximum(totalBytes);
ui->progressBar->setValue(bytesRead);
}
I would be grateful for any help and hints! Thank you.
You probably need to iterate over your 'currentDownloads' list and connect to each one's signal downloadProgress. Then your slot(s) will be called from all of them. In that slot(s) you'll have to sum up all information coming as parameters of QNetworkReply::downloadProgress signal.
You can create a dedicated object for each QNetworkReply instance of your currentDownloads list so that you know from to which file a coming signal belongs, but if I am not mistaking you can also use single slot for all of them and then there is a meta function in Qt that will tell you from which sender the signal came.
P.S. In response to your request for small example here is "straight-forward" approach (without using QSignalMapper or QObject::sender()):
Implement a class "ProgressListenner" something like this (beware I am writing pseudo-code and you'll need to add/fix some necessarily stuff to make it actually working):
class ProgressListenner
{
public:
ProgressListenner() : _lastKnownReceived(0), _lastKnownTotal(0){}
qint64 _lastKnownReceived;
qint64 _lastKnownTotal;
slots:
onDownloadProgress ( qint64 bytesReceived, qint64 bytesTotal )
{
_lastKnownReceived = bytesReceived;
_lastKnownTotal = bytesTotal;
}
}
Than after your line QList<QNetworkReply *> currentDownloads; add QList<ProgressListenner*> downloadListenners;. Inside your foreach each time you are adding new QNetworkReply object to currentDownloads also:
1. create new instance of ProgressListenner and add it to downloadListenners.
2. connect signal of that particular QNetworkReply to that corresponding ProgressListenner's slot: connect(reply, SIGNAL(downloadProgress(qint64, qint64)), pListenner, SLOT(onDownloadProgress (qint64, qint64)));
This way every time some QNetworkReply will fire it's progress signal, slot of corresponding ProgressListenner will be called.
Next step is sum up numbers from all downloads. One simple way is:
1. Create one more function in ProgressListenner class and make it static (important). Let say the name of function is CommonProgress.
2. At the end of onDownloadProgress function call also call CommonProgress
3. In CommonProgress function (taking care about thread safety!) iterate over all elements of downloadListenners and sum up their _lastKnownReceived and _lastKnownTotal. Do the necessarily arithmetic... Don't forget that bytesTotal can be -1!!!

Post HTTP request from qt5 using qml

Hi everyone,
I am trying to send the http post request from my qt app. I have read alot and still struggling to get some concepts of signals and slots. Would be nice if somebody can help me out from here..
here is my qml code snippet:
TextField { id: u_name; placeholderText: userText(); Layout.fillWidth: true; style: StyleTextField {} }
TextField { id: p_text; echoMode: TextInput.Password; Layout.fillWidth: true; style: StyleTextField {} }
Button {
id: signInButton
text: "Sign In";
style: StyleButton {}
Layout.fillWidth: true;
//Layout.alignment: Qt.AlignTop;
signal esLoginClicked()
onClicked: {
if (u_name.text.length) Settings.userText = u_name.text;
if (p_text.text.length) Settings.passText = p_text.text;
signInButton.esLoginClicked().connect(esLogin(u_name.text, p_text.text));
page_stack.pop();
}
}
Here I am trying to get username and password from user and want to pass it to slot "esLogin" that I have declared in my header file using signal esLoginCLicked() which I have created here only. My header files looks like this...
Q_OBJECT
Q_PROPERTY(QString userText READ userText WRITE setUserText NOTIFY userTextChanged)
Q_PROPERTY(QString passText READ passText WRITE setPassText NOTIFY passTextChanged)
public:
static esQuickSettings *instance(void);
public:
QString userText(void);
QString passText(void);
// void esLoginClicked(void);
// void esLoginClicked(const QString& userText, const QString passText);
public:
void setUserText(const QString& user);
void setPassText(const QString& passt);
void esLogin(const QString& userText, const QString& passText);
signals:
void userTextChanged(void);
void passTextChanged(void);
but somehow I am not able to make it work and missing some key concept here to make signal and slot work.
P.S: I want to take input from QML and put in slot which will have the definition in cpp file respective to header.
There are (at least) two ways to address this issue, but I will only let you know one of them based on the comment discussion.
Connect the QML signal to the C++ slot.
main.qml
...
Button {
id: signInButton
// This is necessary for finding this nested item in C++
objectName: "SignInButtonObjectName"
...
}
...
main.cpp
...
QQmlEngine engine;
QQmlComponent component(&engine, "main.qml");
QObject *object = component.create();
QObject *childObject = object->findChild<QObject*>("SignInButtonObjectName");
Foo foo;
QObject::connect(childObject, SIGNAL(esLoginClicked(const QString&, const QString&)), &foo, SLOT(esLogin(const QString&, const QString&)));
...
The other approach would be to call the C++ slot in your qml code when the signal happens to be emitted which is probably even simpler. In that case, you would make the method below either Q_INVOKABLE or even better: a slot.
void esLogin(const QString& userText, const QString& passText);
Then, you would need to make sure that this method is exposed to qml via context properties, namely: you would make the class a context property which would be available to qml for calling like foo.esLogin() in your desired qml signal handler.

launching a program inside another program

I'm trying to get Qt to launch another Qt program when a button is clicked.
Here is my code.
void Widget::launchModule(){
QString program = "C:\A2Q1-build-desktop\debug\A2Q1.exe";
QStringList arguments;
QProcess *myProcess = new QProcess(this);
myProcess->start(program, arguments);
myProcess->waitForFinished();
QString strOut = myProcess->readAllStandardOutput();
}
So it is supposed to save into the QString strOut. First of all I am having an error with the QString program line I don't understand how to point this to the program as all examples of QProcess I have looked at use / and this doesn't make sense to me. Also with the syntax of the program string correct, will this work?
Thanks
In a C/C++ string literal, you must escape all backward slashes.
It's really bad to use the waitForX() functions in Qt. They block your GUI and make your application unresponsive. From a user experience point of view, it truly sucks. Don't do it.
You should code in asynchronous style, with signals and slots.
My other answer provides a rather complete example how asynchronous process communications might work. It uses QProcess to launch itself.
Your original code could be modified as follows:
class Window : ... {
Q_OBJECT
Q_SLOT void launch() {
const QString program = "C:\\A2Q1-build-desktop\\debug\\A2Q1.exe";
QProcess *process = new QProcess(this);
connect(process, SIGNAL(finished(int)), SLOT(finished()));
connect(process, SIGNAL(error(QProcess::ProcessError)), SLOT(finished()));
process->start(program);
}
Q_SLOT void finished() {
QScopedPointer<Process> process = qobject_cast<QProcess*>(sender());
QString out = process->readAllStandardOutput();
// The string will be empty if the process failed to start
... /* process the process's output here */
// The scoped pointer will delete the process at the end
// of the current scope - right here.
}
...
}

How to force Qt to update GUI from not main thread

I'm fighing since last week with problem caused by update of QPlainTextEdit. I'm trying to create separate from QMainWindow Dialog window with QPlainTextEdit inside. The problem begins when I try to use appendHtml signal (also tried with appendText), text that is placed is not visible unless marked by by mouse. Repainting or updating cause in program crash or no visible action.
Simplified code of QDialog with QPlainTextEdit header:
namespace Ui {
class LogWindow;
}
class LogWriter: public QDialog
{
Q_OBJECT
QMutex print_lock;
public:
class Log{
Q_OBJECT
const static int MAX_SIZE = 100;
bool to_terminal;
QString color;
QMutex *print_lock;
QPlainTextEdit *text_place;
QVector< QPair<QString,time_t> > history;
LogWriter * obj;
public:
bool print;
Log(bool _print,QString _color,LogWriter *obj_ = NULL)
{print = _print; color = _color; obj = obj_;}
void setLock(QMutex *print_lock_){print_lock = print_lock_;}
void setTextField(QPlainTextEdit *_text) {text_place = _text;}
Log& operator<<(QString &a);
Log& operator<<(const char* a);
};
static LogWriter* getInstance()
{
static LogWriter instance; // Guaranteed to be destroyed.
// Instantiated on first use.
return &instance;
}
~LogWriter();
Log LOW,MEDIUM,HIGH;
Ui::LogWindow *ui;
signals:
void signalLogAppend(QString);
};
Simplified code of methods definitions:
LogWriter::LogWriter(QWidget * parent): QDialog(parent) {
ui = new Ui::LogWindow;
ui->setupUi(this);
LOW.setLock(&print_lock);
MEDIUM.setLock(&print_lock);
HIGH.setLock(&print_lock);
connect(this,SIGNAL(signalLogAppend(QString)),ui->plainTextEdit,
SLOT(appendHtml(QString)),Qt::DirectConnection);
}
LogWriter::Log& LogWriter::Log::operator<< (QString &s){
history.push_front(qMakePair(s,time(NULL)));
if(history.size() > MAX_SIZE) history.pop_back();
if(print){
//print_lock->lock();
QString text = "<font color=\"";
text += color + "\">";
text += s + "</font>";
//cout << text.toStdString() << endl;
//text_place->appendHtml(text);
//text_place->repaint();
emit (obj)->signalLogAppend(text);
//print_lock->unlock();
}
return *this;
}
I have two separate ui files (first for main window, second for log window).
I have to use log window all across my program (something about 10 threads), and I stucked on this issue. My question is - is it possible to force GUI update without using main thread and if not - what else possibilities I have. If possible I would rather avoid reconstructing all my code - it would take me some time to do it. Right now logging is super easy - I ony need:
LogWindow *log = LogWindow::getInstance();
log->MEDIUM << "something";
As additional info I add QTCreator warning:
QObject::connect: Cannot queue arguments of type 'QTextBlock'
(Make sure 'QTextBlock' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'QTextCursor'
(Make sure 'QTextCursor' is registered using qRegisterMetaType().)
If I understand your code correctly, you're trying to log from a background thread and are using a direct connection to pass the signal to the GUI thread? That's not going to work, you have to send the signal via the default connection so Qt can figure out that it's a cross-thread signal and pass it across threads accordingly (ie, via the message loop on the foreground thread).
In Qt, any GUI interaction has to happen in the Main/foreground thread otherwise bad things happen as you discovered. You can certainly send a signal from a background thread to trigger a GUI update - I do this all the time - but you need to ensure that you're using the correct connection for it. The direct connection results in a direct function call and is not going to work for you in this case.
In your code, the problem is the call to connect() - you explicitly specify the connection mode for the signal to slot connection when you should just use the default setting. If you set the connection explicitly to Qt::DirectConnection, the underlying code will execute a direct call to the specified slot, which means that you end up calling the slot in the thread context of the signal. You don't want that because the signal is triggered in a background thread.
You can't pass arbitrary types/classes to signals and slots. The list is limited and not all Qt classes are in the list. To add types/classes to the list of those that can be passed to a signal/slot, you must call qRegisterMetaType for that class. I recommend calling it in the constructor of the class you're trying to pass to a signal like this:
MyClass::MyClass() : MyParentClass()
{
static int reg = qRegisterMetaType<MyClass>("MyClass");
}
The static int ensures the registration is only called once and before any instance of MyClass could ever be used.