SlideShare a Scribd company logo
QtWebKit



1
Contents
• Classes
• Features
• Exposing Widgets
• W ki with J
  Working i h JavaScript
                  S i




2
WebKit

    • Provides a Web browser engine
        • Easy to embed WWW content into an application
        • Content may be enhanced with native controls
    • QtWebKit module provides facilities for rendering of
        • HyperText Markup Language (HTML),
        • Extensible HyperText Markup Language (XHTML) and
        • Scalable Vector Graphics (SVG) documents,
        • Styled using Cascading Style Sheets (CSS) and scripted with JavaScript
    • Based on the Open Source WebKit engine
        • www.webkit.org
               ebkit org




3
High-Level Architecture

                          QWebView,
                          QW bVi
        QtWebKit API      QWebPage,
                         QWebFrame, …



                            WebCore
        WebKit Engine
                         JavaScript Core



      Platform/Network    QtCore, QtXml,
         Adaptation         QtNetwork




4
QWebView and QWebPage Classes
• QWebView inherited from QWidget                                    QWebView
     • R d
       Renders th contents of a QWebPage
               the   t t f
     • Easy to embed wherever a widget could be used                 QWebPage

         QWebView *view = new QWebView( parent );                    QWebFrame
         view->load(QUrl(“http://www.nokia.com”));
         view >show();
         view->show();


    • QWebPage provides access to the document structure in a page
        • F
          Frames
        • Navigation history
        • Undo/redo stack for editable content
    • Each QWebPage has one QWebFrame object as its main frame
        • HTML documents can be nested using multiple frames




5
QWebView

    • All
      Allows viewing and editing of Web documents
              i i      d diti     fW bd        t
       • view.back(); view.forward(); view.reload();
    • Easy to embed Web content anywhere
         y                        y
    • Three essential signals
       • loadStarted()
       • loadProgress() – emits a value b t
                             it     l between 0 100
                                                0…100
       • loadFinished() – view loaded completely
    • Settings can be specified with QWebSettings
            g          p             Q         g
       • Font, is JavaScript enabled, are plugins enabled, auto load images, etc.
       • Use QWebSettings::globalSettings() to modify global settings, and
         QWebPage::settings() to modify page specific settings (overrides
       • QW bP            tti      () modif page-specific             (o errides
         global settings for the given page)




6
QGraphicsWebView
• Counterpart of QWebView in the Graphics View Framework
• Enables displaying web content in a graphics view
• Many of the functions and signals are the same as in QWebView
    • M k porting existing QWebView-using code t QGraphicsWebView easy
      Makes  ti     i ti      b i     i     d to      hi    b i
    • Classes like QWebPage, QWebFrame etc. are used in the background here as well




7
Basic Services - Examples
• HTML to PDF

      QPrinter p(QPrinter::HighResolution);
      p.setOutputFormat(QPrinter::PdfFormat);
      p.setOutputFileName(outputFile);
      webview->print(&p);


• HTML to image

      QImage img(size(), QImage Fo mat ARGB32)
             img(si e() QImage::Format_ARGB32);
      img.fill(Qt::transparent);
      QPainter p(&img);
      webview->page()->mainFrame()->render(&p);
      img.scale(0.5, 0.5);




8
Exposing Widgets – 1(3)
• QtWebKit can place QWidgets and QGraphicsWidgets in the Web
  environment
     i      t
    • Use HTML/JavaScript to control UI and infrastructure
• Approaches
    • Host widget inside Web environment
    • Expose a C++ object to JavaScript environment
    • …or execute JavaScript from C++ side
• Why would you want to do this?
    • Simple updates (redeploy a script file)
    • Easy to reuse Web design for Look & Feel
    • Easier interaction with web services




9
Exposing Widgets – 2(3)

     • QWidgets can be exposed to the Web environment by
         • Using the HTML tag <object> with a specific MIME-type in the Web page, and
         • Subclassing QWebPage and implementing QWebPage::createPlugin()

     <!-- mywebsite html -->
          mywebsite.html
     <object type="application/x-qt-plugin"
       classid="mylabel“ width="300" height=“300“/>


     // mywebpage.cpp
     QObject* MyWebPage::createPlugin(const QString& classid,
       const QU l& url, const QSt i Li t& paramNames,
           t QUrl&   l      t QStringList&      N
       const QStringList& paramValues )
     {
       if (classid == “mylabel”) return new QLabel(“Test Label”);
                       mylabel )            QLabel( Test Label );
       return 0;
     }



10
Exposing Widgets – 3(3)

 • The same effect as in the previous slide can also be achieved by
     • Using the HTML tag <object> with any custom MIME-type, and
     • Subclassing QWebPluginFactory and implementing functions create() and
       plugins()
 • Note that in this case there is no need to subclass QWebPage!
     • Just tell the page which factory to use:
       QWebPage::setPluginFactory()
     • The same plugin can be reused with multiple QWebPages
 • N t also that plugins() i currently only called when J
   Note l th t                is      tl   l  ll d h JavaScript programs
                                                          S i t
   access global plugins or mimetypes objects
     • Still, implement it p p y to g
            , p            properly guarantee compatibility in the future
                                                 p        y




11
Working with Exposed Objects 1(2)
• QObjects can expose meta-object information to the JavaScript environment
     • P
       Properties can b read and altered
              ti      be   d d lt d
     • Slots can be called
     • Connect statements can be made


• Use QWebFrame::addToJavaScriptWindowObject() to make your object
  accessible from JavaScript
     • This should be done each time the JavaScript context is cleared, i.e. just before a
       new URL is loaded
     • A useful signal t monitor:
             f l i   l to   it
       QWebFrame::javaScriptWindowObjectCleared()




12
Working with Exposed Objects 2(2)

     // SomeFile.cpp
                  pp
     // MyTwitObject derives from Qobject and has a slot function
     // called updateStatus(QString s)
     MyTwitObject* myTwit = new MyTwitObject();
     frame->addToJavaScriptWindowObject(“twit”, myTwit);


     // MyWebSite.html
     <script type=”text/javascript”>
     function setStatus() {
         twit.updateStatus(“Some status”);   // Slot function call
     }
     </script>



13
Interoperating with JavaScript

     • We already know that each Web frame has a JavaScript context
     • JavaScript code can be executed from C++ using
       QWebFrame::evaluateJavaScript()
         • Returns a QVariant
         • Can be used to dynamically manipulate the contents of a Web page!

      // A simple calculation, result.toInt() == 2
      QVariant result = frame->evaluateJavaScript(“1+1;”);


      // Parse information from an HTML element on a Web page,
                                                         p g
      // userName.toString() == ”Bugs Bunny”
      QWebFrame *frame = view->currentFrame();
      QVariant userName = frame->evaluateJavaScript(
        “document.getElementById(“userName”).value”);




14
QWebElement
• A convenience class for accessing and traversing DOM elements in a
  QWebFrame
     • Provides pretty much the same functionality as JavaScript functions such as
       getElementById() etc.
     • G t a reference to a QWebElement i t
       Get     f       t                 instance using
                                                      i
       QWebFrame::documentElement() in your code
• Contains plenty of useful functions: firstChild(), nextSibling(),
  findAll()…
   i
     • Argument to findAll() is a CSS selector statement
     • Convenience class QWebElementCollection can also be used; simply a list class
                                                               ;    py
       that contains QWebElements
     QWebElement document = frame->documentElement();
     // Find all <span> elements
     QWebElementCollection allSpans = document.findAll("span");
     // Find all <span> elements within a <p class=intro> element
     QWebElementCollection introSpans = document.findAll( p.intro span");
                                        document findAll("p intro span );


15
Summary
• QtWebKit enables full AJAX application functionality and embedding Web content
  anywhere i th Qt application
      h    in the        li ti
• Exposing custom Qt GUI components is easy
• QObjects can be exposed too!
                  exposed,




16

More Related Content

PDF
Multimedia in WebKitGtk+, past/present/future
PDF
WebKit, HTML5 media and GStreamer on multiple platforms
PDF
GStreamer support in WebKit. what’s new?
PDF
Efficient multimedia support in QtWebKit on Raspberry Pi (GStreamer Conferenc...
PDF
WebKit and GStreamer
PDF
Ewebkit basic (Web rendering enging of EFL)
PDF
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...
PDF
WebKit2 And You (GUADEC 2013)
Multimedia in WebKitGtk+, past/present/future
WebKit, HTML5 media and GStreamer on multiple platforms
GStreamer support in WebKit. what’s new?
Efficient multimedia support in QtWebKit on Raspberry Pi (GStreamer Conferenc...
WebKit and GStreamer
Ewebkit basic (Web rendering enging of EFL)
GStreamer-VAAPI: Hardware-accelerated encoding and decoding on Intel hardware...
WebKit2 And You (GUADEC 2013)

What's hot (19)

PDF
HTML5 Apps on AGL Platform with the Web Application Manager (Automotive Grade...
PDF
WebKitGtk+ Project
PDF
Hw accelerated webkitgtk+ on raspberry pi
PDF
Chromium Ozone
PDF
Reaching the multimedia web from embedded platforms with WPEWebkit
PDF
Introduction to QtWebKit
PDF
Embedding Chromium into AGL demo platform with WAM
PDF
Hybrid Desktop/Web applications with WebKitGTK+ (COSCUP 2010)
PDF
HTML5 on the AGL demo platform with Chromium and WAM (AGL AMM March 2021)
PDF
Essential parts to implement own Ozone backend
PDF
WPEWebKit, the WebKit port for embedded platforms (Linaro Connect San Diego 2...
PDF
JooinK - DevFest Piemonte 2013
ODP
How to use WebKitGtk+
PDF
WebKit for Wayland (Web Engines Hackfest 2014)
PDF
Pairing WebKit and Wayland for Linux-Based Embedded Web Content Presentation ...
PDF
Kiosk-mode browser using Chromium Embedded Framework (CEF)
PDF
WebKit Clutter Port Present and Future; WebKitGtk Status and Roadmap to WebKi...
PDF
Contributions to an open source project: Igalia and the Chromium project
PDF
Overview of the open source Vulkan driver for Raspberry Pi 4 (XDC 2020)
HTML5 Apps on AGL Platform with the Web Application Manager (Automotive Grade...
WebKitGtk+ Project
Hw accelerated webkitgtk+ on raspberry pi
Chromium Ozone
Reaching the multimedia web from embedded platforms with WPEWebkit
Introduction to QtWebKit
Embedding Chromium into AGL demo platform with WAM
Hybrid Desktop/Web applications with WebKitGTK+ (COSCUP 2010)
HTML5 on the AGL demo platform with Chromium and WAM (AGL AMM March 2021)
Essential parts to implement own Ozone backend
WPEWebKit, the WebKit port for embedded platforms (Linaro Connect San Diego 2...
JooinK - DevFest Piemonte 2013
How to use WebKitGtk+
WebKit for Wayland (Web Engines Hackfest 2014)
Pairing WebKit and Wayland for Linux-Based Embedded Web Content Presentation ...
Kiosk-mode browser using Chromium Embedded Framework (CEF)
WebKit Clutter Port Present and Future; WebKitGtk Status and Roadmap to WebKi...
Contributions to an open source project: Igalia and the Chromium project
Overview of the open source Vulkan driver for Raspberry Pi 4 (XDC 2020)
Ad

Similar to Petri Niemi Qt Web Kit (20)

PDF
Qt & Webkit
PPTX
Liferay (DXP) 7 Tech Meetup for Developers
PDF
Hybrid Apps (Native + Web) using WebKit
PDF
Hybrid Apps (Native + Web) using WebKit
PDF
Hybrid Apps (Native + Web) via QtWebKit
PDF
Moving to the Client - JavaFX and HTML5
PDF
Run-time of Node.js : V8 JavaScript Engine
PPTX
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
PDF
Javascript
PDF
Best Practices in Qt Quick/QML - Part III
 
PPTX
Moving to the Client - JavaFX and HTML5
PPTX
JavaFX and HTML5 - Like Curds and Rice
PPTX
MVC 6 - the new unified Web programming model
PDF
Web Components v1
ZIP
Building Web Apps Sanely - EclipseCon 2010
PDF
The MEAN stack
PPTX
Java web application development
PPTX
Best Practices in Qt Quick/QML - Part I
 
PDF
PDF
JavaONE 2012 Using Java with HTML5 and CSS3
Qt & Webkit
Liferay (DXP) 7 Tech Meetup for Developers
Hybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) using WebKit
Hybrid Apps (Native + Web) via QtWebKit
Moving to the Client - JavaFX and HTML5
Run-time of Node.js : V8 JavaScript Engine
Improving Your Selenium WebDriver Tests - Belgium testing days_2016
Javascript
Best Practices in Qt Quick/QML - Part III
 
Moving to the Client - JavaFX and HTML5
JavaFX and HTML5 - Like Curds and Rice
MVC 6 - the new unified Web programming model
Web Components v1
Building Web Apps Sanely - EclipseCon 2010
The MEAN stack
Java web application development
Best Practices in Qt Quick/QML - Part I
 
JavaONE 2012 Using Java with HTML5 and CSS3
Ad

More from NokiaAppForum (13)

PPT
Toshl.com - Od ideje do končnega izdelka
PDF
PDF
Hello Mobile
PDF
Alexander Oswald The Future of Maemo and Symbian
PDF
Dominik Gusenbauer Qt Mobility
PDF
Petri Niemi Qt Advanced Part 2
PDF
Petri Niemi Qt Advanced Part 1
PDF
Andreas Jakl, Qt Symbian Maemo Quickstart
PPTX
Mobile Web Development from Scratch
PDF
Wolfgang Damm Wikitude
PDF
Miha Lesjak Mobilizing The Web with Web Runtime
PDF
Jure Sustersic Monetization through Ovi Services
PDF
Andreas Jakl Software Development on Nokia Deviceswith Qt
Toshl.com - Od ideje do končnega izdelka
Hello Mobile
Alexander Oswald The Future of Maemo and Symbian
Dominik Gusenbauer Qt Mobility
Petri Niemi Qt Advanced Part 2
Petri Niemi Qt Advanced Part 1
Andreas Jakl, Qt Symbian Maemo Quickstart
Mobile Web Development from Scratch
Wolfgang Damm Wikitude
Miha Lesjak Mobilizing The Web with Web Runtime
Jure Sustersic Monetization through Ovi Services
Andreas Jakl Software Development on Nokia Deviceswith Qt

Petri Niemi Qt Web Kit

  • 2. Contents • Classes • Features • Exposing Widgets • W ki with J Working i h JavaScript S i 2
  • 3. WebKit • Provides a Web browser engine • Easy to embed WWW content into an application • Content may be enhanced with native controls • QtWebKit module provides facilities for rendering of • HyperText Markup Language (HTML), • Extensible HyperText Markup Language (XHTML) and • Scalable Vector Graphics (SVG) documents, • Styled using Cascading Style Sheets (CSS) and scripted with JavaScript • Based on the Open Source WebKit engine • www.webkit.org ebkit org 3
  • 4. High-Level Architecture QWebView, QW bVi QtWebKit API QWebPage, QWebFrame, … WebCore WebKit Engine JavaScript Core Platform/Network QtCore, QtXml, Adaptation QtNetwork 4
  • 5. QWebView and QWebPage Classes • QWebView inherited from QWidget QWebView • R d Renders th contents of a QWebPage the t t f • Easy to embed wherever a widget could be used QWebPage QWebView *view = new QWebView( parent ); QWebFrame view->load(QUrl(“http://www.nokia.com”)); view >show(); view->show(); • QWebPage provides access to the document structure in a page • F Frames • Navigation history • Undo/redo stack for editable content • Each QWebPage has one QWebFrame object as its main frame • HTML documents can be nested using multiple frames 5
  • 6. QWebView • All Allows viewing and editing of Web documents i i d diti fW bd t • view.back(); view.forward(); view.reload(); • Easy to embed Web content anywhere y y • Three essential signals • loadStarted() • loadProgress() – emits a value b t it l between 0 100 0…100 • loadFinished() – view loaded completely • Settings can be specified with QWebSettings g p Q g • Font, is JavaScript enabled, are plugins enabled, auto load images, etc. • Use QWebSettings::globalSettings() to modify global settings, and QWebPage::settings() to modify page specific settings (overrides • QW bP tti () modif page-specific (o errides global settings for the given page) 6
  • 7. QGraphicsWebView • Counterpart of QWebView in the Graphics View Framework • Enables displaying web content in a graphics view • Many of the functions and signals are the same as in QWebView • M k porting existing QWebView-using code t QGraphicsWebView easy Makes ti i ti b i i d to hi b i • Classes like QWebPage, QWebFrame etc. are used in the background here as well 7
  • 8. Basic Services - Examples • HTML to PDF QPrinter p(QPrinter::HighResolution); p.setOutputFormat(QPrinter::PdfFormat); p.setOutputFileName(outputFile); webview->print(&p); • HTML to image QImage img(size(), QImage Fo mat ARGB32) img(si e() QImage::Format_ARGB32); img.fill(Qt::transparent); QPainter p(&img); webview->page()->mainFrame()->render(&p); img.scale(0.5, 0.5); 8
  • 9. Exposing Widgets – 1(3) • QtWebKit can place QWidgets and QGraphicsWidgets in the Web environment i t • Use HTML/JavaScript to control UI and infrastructure • Approaches • Host widget inside Web environment • Expose a C++ object to JavaScript environment • …or execute JavaScript from C++ side • Why would you want to do this? • Simple updates (redeploy a script file) • Easy to reuse Web design for Look & Feel • Easier interaction with web services 9
  • 10. Exposing Widgets – 2(3) • QWidgets can be exposed to the Web environment by • Using the HTML tag <object> with a specific MIME-type in the Web page, and • Subclassing QWebPage and implementing QWebPage::createPlugin() <!-- mywebsite html --> mywebsite.html <object type="application/x-qt-plugin" classid="mylabel“ width="300" height=“300“/> // mywebpage.cpp QObject* MyWebPage::createPlugin(const QString& classid, const QU l& url, const QSt i Li t& paramNames, t QUrl& l t QStringList& N const QStringList& paramValues ) { if (classid == “mylabel”) return new QLabel(“Test Label”); mylabel ) QLabel( Test Label ); return 0; } 10
  • 11. Exposing Widgets – 3(3) • The same effect as in the previous slide can also be achieved by • Using the HTML tag <object> with any custom MIME-type, and • Subclassing QWebPluginFactory and implementing functions create() and plugins() • Note that in this case there is no need to subclass QWebPage! • Just tell the page which factory to use: QWebPage::setPluginFactory() • The same plugin can be reused with multiple QWebPages • N t also that plugins() i currently only called when J Note l th t is tl l ll d h JavaScript programs S i t access global plugins or mimetypes objects • Still, implement it p p y to g , p properly guarantee compatibility in the future p y 11
  • 12. Working with Exposed Objects 1(2) • QObjects can expose meta-object information to the JavaScript environment • P Properties can b read and altered ti be d d lt d • Slots can be called • Connect statements can be made • Use QWebFrame::addToJavaScriptWindowObject() to make your object accessible from JavaScript • This should be done each time the JavaScript context is cleared, i.e. just before a new URL is loaded • A useful signal t monitor: f l i l to it QWebFrame::javaScriptWindowObjectCleared() 12
  • 13. Working with Exposed Objects 2(2) // SomeFile.cpp pp // MyTwitObject derives from Qobject and has a slot function // called updateStatus(QString s) MyTwitObject* myTwit = new MyTwitObject(); frame->addToJavaScriptWindowObject(“twit”, myTwit); // MyWebSite.html <script type=”text/javascript”> function setStatus() { twit.updateStatus(“Some status”); // Slot function call } </script> 13
  • 14. Interoperating with JavaScript • We already know that each Web frame has a JavaScript context • JavaScript code can be executed from C++ using QWebFrame::evaluateJavaScript() • Returns a QVariant • Can be used to dynamically manipulate the contents of a Web page! // A simple calculation, result.toInt() == 2 QVariant result = frame->evaluateJavaScript(“1+1;”); // Parse information from an HTML element on a Web page, p g // userName.toString() == ”Bugs Bunny” QWebFrame *frame = view->currentFrame(); QVariant userName = frame->evaluateJavaScript( “document.getElementById(“userName”).value”); 14
  • 15. QWebElement • A convenience class for accessing and traversing DOM elements in a QWebFrame • Provides pretty much the same functionality as JavaScript functions such as getElementById() etc. • G t a reference to a QWebElement i t Get f t instance using i QWebFrame::documentElement() in your code • Contains plenty of useful functions: firstChild(), nextSibling(), findAll()… i • Argument to findAll() is a CSS selector statement • Convenience class QWebElementCollection can also be used; simply a list class ; py that contains QWebElements QWebElement document = frame->documentElement(); // Find all <span> elements QWebElementCollection allSpans = document.findAll("span"); // Find all <span> elements within a <p class=intro> element QWebElementCollection introSpans = document.findAll( p.intro span"); document findAll("p intro span ); 15
  • 16. Summary • QtWebKit enables full AJAX application functionality and embedding Web content anywhere i th Qt application h in the li ti • Exposing custom Qt GUI components is easy • QObjects can be exposed too! exposed, 16