Last active
August 19, 2016 18:08
-
-
Save Tosainu/bd4dfae31c0af3c8a8ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QMessageBox> | |
#include "jsobj.h" | |
JsObj::JsObj(QObject *parent) : QObject(parent) {} | |
void JsObj::hogeSlot() { | |
emit hogeSignal(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef JSOBJ_H | |
#define JSOBJ_H | |
#include <QtCore> | |
class JsObj : public QObject { | |
Q_OBJECT | |
public: | |
JsObj(QObject *parent = nullptr); | |
signals: | |
void hogeSignal(); | |
public slots: | |
void hogeSlot(); | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include "mainwindow.h" | |
auto main(int argc, char **argv) -> int { | |
QApplication app(argc, argv); | |
MainWindow window; | |
window.show(); | |
return app.exec(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtWebKitWidgets> | |
#include "mainwindow.h" | |
#include "jsobj.h" | |
MainWindow::MainWindow() { | |
auto jo = new JsObj(); | |
auto button = new QPushButton("You also like"); | |
connect(button, SIGNAL(clicked()), jo, SLOT(hogeSlot())); | |
auto webview = new QWebView(this); | |
webview->page()->mainFrame()->addToJavaScriptWindowObject("jsobj", jo); | |
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> | |
<script> | |
var fugaSlot = function() { | |
location.href = 'http://myon.info/'; | |
}; | |
jsobj.hogeSignal.connect(fugaSlot); | |
</script> | |
</head> | |
<body> | |
<p>Please click the button below.</p> | |
</body> | |
</html> | |
)"); | |
auto layout = new QVBoxLayout(); | |
layout->addWidget(webview); | |
layout->addWidget(button); | |
auto mainwidget = new QWidget(); | |
mainwidget->setLayout(layout); | |
this->setCentralWidget(mainwidget); | |
} | |
MainWindow::~MainWindow() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MAINWINDOW_H | |
#define MAINWINDOW_H | |
#include <QMainWindow> | |
class MainWindow : public QMainWindow { | |
Q_OBJECT | |
public: | |
MainWindow(); | |
~MainWindow(); | |
}; | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QT += core gui webkitwidgets | |
TEMPLATE = app | |
TARGET = QWebView_cpp-to-html | |
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