Title pretty much says what I need, currently i get 3 messages 2 errors and one warning (I get them on the connect(....) line.
D:\Programming\C++\qtWebkit\webkit.h:27: error: C2061: syntax error: identifier 'page'
D:\Programming\C++\qtWebkit\webkit.h:27: error: C4430: missing type specifier - int assumed. Note: C++ does not support default-int
D:\Programming\C++\qtWebkit\webkit.h:27: warning: C4183: 'connect': missing return type; assumed to be a member function returning 'int'
Code from header.
#ifndef WEBKIT_H
#define WEBKIT_H
#include <QApplication>
#include <QWebEnginePage>
#include <QEventLoop>
#include <QVariant>
#include <QString>
class Webkit
{
public:
void loadUrl(std::string url);
void executeJS(std::string js);
std::string html() { return __html.toStdString(); }
std::string oldHtml() { return __oldHtml.toStdString(); }
std::string jsResult() { return __jsResult.toStdString(); }
protected slots:
void _pageLoaded(bool ok);
private:
QWebEnginePage page;
connect(page, &QWebEnginePage::loadFinished, this, &Webkit::_pageLoaded);
};
#endif // WEBKIT_H
#ifndef WEBKIT_H
#define WEBKIT_H
#include <QApplication>
#include <QWebEnginePage>
#include <QEventLoop>
#include <QVariant>
#include <QString>
class Webkit
{
public:
void loadUrl(std::string url);
void executeJS(std::string js);
std::string html() { return __html.toStdString(); }
std::string oldHtml() { return __oldHtml.toStdString(); }
std::string jsResult() { return __jsResult.toStdString(); }
protected slots:
void _pageLoaded(bool ok);
private:
QString __html;
QString __oldHtml = "None";
QString __jsResult = "None";
QWebEnginePage page;
connect(page, &QWebEnginePage::loadFinished, this, &Webkit::_pageLoaded);
QEventLoop loop;
void _jsResult(const QVariant &v);
void _htmlLoaded(QString html);
};
#endif // WEBKIT_H
To copy to clipboard, switch view to plain text mode
Code from cpp
#include "webkit.h"
#include <QUrl>
#include <QString>
#include <iostream>
void Webkit::executeJS(std::string js){
__oldHtml = __html;
page.
runJavaScript(QString().
fromStdString(js
),
&Webkit
::_jsResult
);
loop.exec();
}
void Webkit
::_jsResult
(const QVariant &v
){ __jsResult = v.toString();
_pageLoaded(true);
}
void Webkit::loadUrl(std::string url) {
loop.exec();
}
void Webkit::_pageLoaded(bool ok){
page.toHtml(&Webkit::_htmlLoaded);
}
void Webkit
::_htmlLoaded
(QString html
){ __html = html;
loop.quit();
}
int main(int argc, char *argv[])
{
Webkit webkit;
webkit.loadUrl("http://google.com");
std::cout << webkit.html() << std::endl;
return app.exec();
}
#include "webkit.h"
#include <QUrl>
#include <QString>
#include <iostream>
void Webkit::executeJS(std::string js){
__oldHtml = __html;
page.runJavaScript(QString().fromStdString(js), &Webkit::_jsResult);
loop.exec();
}
void Webkit::_jsResult(const QVariant &v){
__jsResult = v.toString();
_pageLoaded(true);
}
void Webkit::loadUrl(std::string url) {
page.setUrl(QUrl(QString().fromStdString(url)));
loop.exec();
}
void Webkit::_pageLoaded(bool ok){
page.toHtml(&Webkit::_htmlLoaded);
}
void Webkit::_htmlLoaded(QString html){
__html = html;
loop.quit();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Webkit webkit;
webkit.loadUrl("http://google.com");
std::cout << webkit.html() << std::endl;
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Code from .pro
TEMPLATE = app
QT = core webenginewidgets
CONFIG += c++11
SOURCES += \
webkit.cpp
HEADERS += \
webkit.h
TEMPLATE = app
QT = core webenginewidgets
CONFIG += c++11
SOURCES += \
webkit.cpp
HEADERS += \
webkit.h
To copy to clipboard, switch view to plain text mode
Is my connect statement wrong? can't i initlize a QWebEnginePage there? Also tried to make Webkit inherit from QWebEnginePage but still couldn't get it to work.
Bookmarks