C++ Practice with webkit.
by teckk from LinuxQuestions.org on (#5P7GP)
Everything is hard coded which is not ideal, but makes the code small, and shows how everything works. I decided to study C and C++ at the same time.
You'll need g++ and qt5 webkit installed.
wb.cpp
Code://Basic Qt5Webkit Web browser in c++
#include <QApplication>
#include <QWebView>
#include <QtWidgets>
#include <iostream>
//Browser Home Page
const char* HomePage = "https://www.linuxquestions.org/questions/";
//User Agent iphone10 safari
const char* AgentIphone = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_1 "
"like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) "
"Version/10.0 Mobile/14A403 Safari/602.1";
//User Agent Win10 firefox
const char* AgentWin10 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; "
"rv:90.0) Gecko/20100101 Firefox/90.0'";
//Change User Agent class
class UserAgent : public QWebPage
{
QString userAgentForUrl(const QUrl &Url) const
{
QString UA = AgentIphone; //User Agent here
return QString(UA);
}
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
app.setApplicationName(QString("My Browser")); //Browser name
app.setApplicationVersion(QString("Version .1a")); //Version
//Web Browser settings
QWebView View;
View.setPage(new UserAgent());
View.setZoomFactor(1.5);
View.settings()->setAttribute(QWebSettings::JavascriptEnabled, false);
View.settings()->setAttribute(QWebSettings::AutoLoadImages, false);
View.settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
View.settings()->setAttribute(QWebSettings::PluginsEnabled, false);
View.settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
View.settings()->setFontFamily(QWebSettings::StandardFont, "monospace");
View.settings()->globalSettings()->setFontSize(QWebSettings::MinimumFontSize, 18);
//Positional Arguments
QCommandLineParser cLineParser;
cLineParser.addPositionalArgument(QStringLiteral("Url"), QStringLiteral());
cLineParser.process(app);
QStringList posArgs = cLineParser.positionalArguments();
QUrl Url;
//Get args or open HomePage
if (posArgs.size() > 1)
{
std::cout << "Too many arguments!" << std::endl;
return 0;
} else if (posArgs.size() == 1)
{
Url = QUrl::fromUserInput(posArgs.at(0));
} else
{
Url = QUrl(HomePage);
}
//Set Url for WebView
View.setUrl(Url);
//Size window
View.resize(1400, 1000);
View.show();
return app.exec();
}wb.pro
Code:QT += webkit
QT += webkitwidgets
QT += network
QT += widgets
SOURCES = wb.cppqmake should make .qmake.stash and Makefile in the directory. Then compile with make
You'll need g++ and qt5 webkit installed.
wb.cpp
Code://Basic Qt5Webkit Web browser in c++
#include <QApplication>
#include <QWebView>
#include <QtWidgets>
#include <iostream>
//Browser Home Page
const char* HomePage = "https://www.linuxquestions.org/questions/";
//User Agent iphone10 safari
const char* AgentIphone = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_1 "
"like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) "
"Version/10.0 Mobile/14A403 Safari/602.1";
//User Agent Win10 firefox
const char* AgentWin10 = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; "
"rv:90.0) Gecko/20100101 Firefox/90.0'";
//Change User Agent class
class UserAgent : public QWebPage
{
QString userAgentForUrl(const QUrl &Url) const
{
QString UA = AgentIphone; //User Agent here
return QString(UA);
}
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
app.setApplicationName(QString("My Browser")); //Browser name
app.setApplicationVersion(QString("Version .1a")); //Version
//Web Browser settings
QWebView View;
View.setPage(new UserAgent());
View.setZoomFactor(1.5);
View.settings()->setAttribute(QWebSettings::JavascriptEnabled, false);
View.settings()->setAttribute(QWebSettings::AutoLoadImages, false);
View.settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
View.settings()->setAttribute(QWebSettings::PluginsEnabled, false);
View.settings()->setAttribute(QWebSettings::LocalStorageEnabled, false);
View.settings()->setFontFamily(QWebSettings::StandardFont, "monospace");
View.settings()->globalSettings()->setFontSize(QWebSettings::MinimumFontSize, 18);
//Positional Arguments
QCommandLineParser cLineParser;
cLineParser.addPositionalArgument(QStringLiteral("Url"), QStringLiteral());
cLineParser.process(app);
QStringList posArgs = cLineParser.positionalArguments();
QUrl Url;
//Get args or open HomePage
if (posArgs.size() > 1)
{
std::cout << "Too many arguments!" << std::endl;
return 0;
} else if (posArgs.size() == 1)
{
Url = QUrl::fromUserInput(posArgs.at(0));
} else
{
Url = QUrl(HomePage);
}
//Set Url for WebView
View.setUrl(Url);
//Size window
View.resize(1400, 1000);
View.show();
return app.exec();
}wb.pro
Code:QT += webkit
QT += webkitwidgets
QT += network
QT += widgets
SOURCES = wb.cppqmake should make .qmake.stash and Makefile in the directory. Then compile with make