Skip to content

Instantly share code, notes, and snippets.

@Tosainu
Created January 12, 2015 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tosainu/24f9fd66dc28c987f836 to your computer and use it in GitHub Desktop.
Save Tosainu/24f9fd66dc28c987f836 to your computer and use it in GitHub Desktop.
#include <QMessageBox>
#include "jsobj.h"
JsObj::JsObj(QObject *parent) : QObject(parent) {}
void JsObj::hogeSlot() {
QMessageBox::information(nullptr, "JsObj::hogeSlot()", "Gochiusa!!");
}
#ifndef JSOBJ_H
#define JSOBJ_H
#include <QtCore>
class JsObj : public QObject {
Q_OBJECT
public:
JsObj(QObject *parent = nullptr);
public slots:
void hogeSlot();
};
#endif
#include <QApplication>
#include "mainwindow.h"
auto main(int argc, char **argv) -> int {
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
#include <QtWebKitWidgets>
#include "mainwindow.h"
#include "jsobj.h"
MainWindow::MainWindow() {
auto jo = new JsObj();
auto webview = new QWebView(this);
webview->setHtml(R"(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
html {
font: 16px/1.6 Roboto, 'Droid Sans', 'Hiragino Kaku Gothic ProN', sans-serif;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Tosainu likes <button onclick="jsobj.hogeSlot();">Click!</button></p>
</body>
</html>
)");
webview->page()->mainFrame()->addToJavaScriptWindowObject("jsobj", jo);
this->setCentralWidget(webview);
}
MainWindow::~MainWindow() {}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow();
~MainWindow();
};
#endif
QT += core gui webkitwidgets
TEMPLATE = app
TARGET = QWebView_html-to-cpp
INCLUDEPATH += .
HEADERS += mainwindow.h jsobj.h
SOURCES += main.cc mainwindow.cc jsobj.cc
CONFIG += c++11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment