Last active
February 3, 2017 15:40
-
-
Save fernandoc1/835edf6508445b6bc001390d6bc1e02e to your computer and use it in GitHub Desktop.
QtWebKit bridge example for compiling on Ubuntu 16.04 with Qt5
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
| cmake_minimum_required(VERSION 3.0) | |
| set(CMAKE_INCLUDE_CURRENT_DIR ON) | |
| set(CMAKE_AUTOMOC ON) | |
| set(CMAKE_AUTORCC ON) | |
| #find_package(Qt5Widgets) | |
| #find_package(Qt5Network) | |
| #find_package(Qt5WebkitWidgets) | |
| #find_package(Qt5Concurrent) | |
| find_package(Qt5 COMPONENTS Core Widgets Concurrent Network WebKitWidgets REQUIRED) | |
| add_executable(Modeler main.cpp mainwindow.cpp test_obj.cpp modeler.qrc) | |
| #target_link_libraries(Modeler Qt5::Widgets Qt5::Network Qt5::WebkitWidgets Qt5::Concurrent) | |
| target_link_libraries(Modeler Qt5::Widgets Qt5::Network Qt5::Concurrent Qt5::WebKitWidgets) |
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 "mainwindow.h" | |
| #include <QApplication> | |
| int main(int argc, char* argv[]) | |
| { | |
| QApplication app(argc, argv); | |
| MainWin win; | |
| win.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 "mainwindow.h" | |
| #include "test_obj.hpp" | |
| #include <QStandardPaths> | |
| #include <QWebFrame> | |
| #include <QWebElementCollection> | |
| #include <QNetworkDiskCache> | |
| TestObj testObj(0); | |
| /* | |
| * Default Constructor | |
| */ | |
| MainWin::MainWin(QWidget * parent) : QWebView(parent) | |
| { | |
| QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true); | |
| m_network = new QNetworkAccessManager(this); | |
| m_cache = new QNetworkDiskCache(this); | |
| m_cache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/modeler.cache"); | |
| m_cache->setMaximumCacheSize(1000000); //set the cache to 10megs | |
| m_network->setCache(m_cache); | |
| page()->setNetworkAccessManager(m_network); | |
| // Signal is emitted before frame loads any web content: | |
| QObject::connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), | |
| this, SLOT(addJSObject())); | |
| // qrc:// URLs refer to resources. See imagenalayzer.qrc | |
| QUrl startURL = QUrl("qrc:/resources/index.html"); | |
| //QUrl startURL = QUrl("http://localhost:8000/"); | |
| // Load web content now! | |
| setUrl(startURL); | |
| } | |
| void MainWin::addJSObject() { | |
| // Add pAnalyzer to JavaScript Frame as member "testFunctionCpp". | |
| page()->mainFrame()->addToJavaScriptWindowObject(QString("testObj"), &testObj); | |
| } |
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 <QWebView> | |
| class ImageAnalyzer; | |
| class QNetworkDiskCache; | |
| class MainWin : public QWebView | |
| { | |
| Q_OBJECT | |
| public: | |
| explicit MainWin(QWidget * parent = 0); | |
| private: | |
| QNetworkAccessManager * m_network; | |
| QNetworkDiskCache * m_cache; | |
| private slots: | |
| void addJSObject(); | |
| }; | |
| #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
| <!DOCTYPE RCC><RCC version="1.0"> | |
| <qresource> | |
| <file>resources/bootstrap.min.css</file> | |
| <file>resources/bootstrap.min.js</file> | |
| <file>resources/bootstrap-theme.min.css</file> | |
| <file>resources/index.html</file> | |
| <file>resources/jquery-3.1.1.min.js</file> | |
| <file>resources/theme.css</file> | |
| </qresource> | |
| </RCC> |
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 "test_obj.hpp" | |
| TestObj::TestObj() | |
| { | |
| std::cout << "Default constructor" << std::endl; | |
| } | |
| TestObj::TestObj(QObject* parent) | |
| : QObject(parent) | |
| { | |
| std::cout << "Parent constructor" << std::endl; | |
| } | |
| void testMethod() | |
| { | |
| std::cout << "Method" << std::endl; | |
| } |
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 _TEST_OBJ_HPP_ | |
| #define _TEST_OBJ_HPP_ | |
| #include <iostream> | |
| #include <QObject> | |
| class TestObj : public QObject | |
| { | |
| Q_OBJECT | |
| public: | |
| TestObj(); | |
| TestObj(QObject* parent=0); | |
| //Q_INVOKABLE void testMethod(); | |
| void testMethod(); | |
| float getValue() {std::cout << "Reading value" << std::endl; return 111.11;} | |
| Q_PROPERTY(float value READ getValue); | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment