Wednesday, June 23, 2010

QtWebKit goes Mobile

Repost of Jesus' post, that I helped write.

There is a lot of effort being put into QtWebKit in order to make it attractive on the mobile front.

Among a tons of bug fixes and good performance improvements there are also lots of new features being developed, mainly geared toward mobile deployment.

The goal with this tutorial is to help you understand some of these new features and how to make the best of them. Or said in other words; how to create a good mobile web view that can be used on touch devices.

First we should make it clear that QGraphicsWebView is the way forward, so if you want to target mobile devices, it is bye bye QWebView. Why is that? Well, the QWebView is based on the QWidget system, thus it cannot easily support rotation, overlays, hardware accelerated compositing and tiling. If you need a QWidget anyway, you can always construct a QGraphicsView (which is a QWidget) with a QGraphicsWebView inside. This is more or less what we will start with.

Let's start with the most simple QGraphicsWebView based "browser" ever:

int main(int argc, char **argv)
{
QApplication app(argc, argv);
const int width = 640;
const int height = 480;

QGraphicsScene scene;

QGraphicsView view(&scene);
view.setFrameShape(QFrame::NoFrame);
view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

QGraphicsWebView webview;
webview.resize(width, height);
webview.load(QUrl("http://www.wouwlabs.com/blogs/jeez"));

scene.addItem(&webview);
view.resize(width, height);
view.show();

return app.exec();
}

Here we just bootstrap a QGraphicsView application and add a QGraphicsWebView to the scene.

It might seem a bit useless as you can only navigate through one website, but it serves well as a simple example. Notice that I'm disabling the scrollbars on the graphics view because QtWebKit handles scrolling and scrollbars automatically. This is due to scrolling optimizations and due to the fact that web authors can interact with the scrollbars for instance style them differently.

On touch-based mobile devices a feature known as tiling is often used. It is used due to the interaction model (touch) as well as a scrolling "optimization". With this optimization we will have to deal with scrolling ourselves and we thus will have to say good bye to the scrollbar styling. Not a big thing, as mobile browsers usually do not even show scrollbars, but use scroll indicators instead.

Tiling basically means that the contents of the viewport is separated into a grid of tiles, so that when you update some area, instead of just updating the area you actually update the whole tile. This gives a few advantages for scrolling as when you scroll you do not need to repaint the new visible area for each scroll step, as you update a row of tiles each time; tiles that are often only partly on the screen. This minimized all the paint calls that we have to do and makes it possible to make nicely kinetic scrolling a possibility.

Loading, layouting etc are blocking operations. Though barely noticeable on a Desktop machines, these operations can block for a long time on a mobile device, letting the user believe the application has become unresponsive and died. Scrolling which is done by using fingers will also stall and give a bad user experience.

One way to over come this issue, is to do all loading, layouting and painting (basically all non-UI related work) in another thread or process, and just blit the result from the web process/thread to the UI. When using tiles, you can blit any tile available when scrolling. When no tile is available you can show a checkerboard tile instead, not letting the scrolling wait for the tiles to be updated. This results in a responsive interface, with the only disadvantage that you from time to time might see checkerboard tiles.

Tiles also helps with zooming. Repainting at each zoom level change during a zoom animation is basically impossible on a mobile device (or desktop for that sake) and thus with tiling, you can stop the tiles from being updates and just scale the already existing tiles, and then at the end of the animation update tiles on top of the scaled ones.

For now we will ignore the blocking issue and concentrate on the tiling and the interaction model.


Resize to contents

When using tiling, we basically want the QGraphicsWebView to act as our contents, as it supports tiling a.o. things. In order for this we need to make it resize itself to the size of its contents. For this we will use QGraphicsWebView::resizesToContents.

From Qt 4.7 documentation: "If this property is set, the QGraphicsWebView will automatically change its size to match the size of the main frame contents. As a result the top level frame will never have scrollbars. It will also make CSS fixed positioning to behave like absolute positioning with elements positioned relative to the document instead of the viewport."

This setting, thus, removes the scrollbars for us on the main frame and makes our QGraphicsWebView resize itself to the size of its content.

Enabling it, is as easy as:

    webview.setResizesToContents(true); 

Qt 4.7 docs also says: "This property should be used in conjunction with the QWebPage::preferredContentsSize property. If not explicitly set, the preferredContentsSize is automatically set to a reasonable value."

If we are going to expand our mobile web view to the size of the contents of its contained page, then that is going to make the view a lot bigger that what can fit on the device's screen!


Using a view as the window to the contents

The idea is to have a custom widget which has a QGraphicsWebView as a class member. Remember that the QGraphicsWebView will be as big as its content's size, so this custom widget will serve as a window, as a viewport.

There is not much more to say here, and the following code snippet illustrates it well:

class MobileWebView : public QGraphicsWidget
{
Q_OBJECT

public:
MobileWebView(QGraphicsItem *parent = 0);
~MobileWebView();

bool mousePress(const QPoint &value);
void mouseMove(const QPoint &value);
void mouseRelease(const QPoint &value);

private:
QGraphicsWebView *webView;
};

In order to properly handle mouse events you must install an event filter on web view or stack it behind its parent object (search for QGraphicsItem::ItemStacksBehindParent). By doing this the mouse events will reach a MobileWebView instance before they reach the member QGraphicsWebView.

Keep in mind that you'll need to add some logic in order to distinguish different mouse events and gestures like a single click, double click, click-and-pan, etc. That is left as an exercise to the reader.

Also keep in mind that as stated earlier, scrolling will have to be implemented manually, just as zoom etc.


Adjusting how contents is laid out

When testing the above on a device, you will notice that many pages do not layout very nicely. In particular the width is larger than the width of the device!

The way web contents is laid out, is that the first the viewport width is used for fitting the contents. If the contents doesn't fit due to non-flexible element with a width larger than the viewport width, the min width possible will be used. As most pages are written with a desktop browser in mind, that makes only very few sites fit into the width of a mobile device.

QtWebKit has a way to force a layout to a given width/height. What really matters here is the width. If you layout a size to a given width, it will get that width and images etc might get cut of. The width/height is also used for laying out fixed elements, but when we resize the QGraphicsWebView to the size of the contents, fixed elements will not be relative to the view, which is the behaviour found on most mobile browsers.

From Qt 4.7 documentation: "If this property is set to a valid size, it is used to lay out the page."

We saw that this property is automatically set to a reasonable value when using QGraphicsWebView::resizesToContents.

As you can imaging, laying out with a smaller viewport can cause pages to break, and as thus, a default value has been chosen so that it almost breaks no pages while still making the sites fit. This value is 960x800.

If the device have a bigger resolution, this value can be changed using:

    webview.page()->setPreferredContentsSize(QSize(desiredWidth, desiredHeight)); 

You can play around with this and find your own magic number, but let's stick to this 960px wide for now.


The 'viewport' meta tag

As some sites do not work with 960 or want to have control on how the page is laid out, QtWebKit as well as Android, Firefox Mobile and the iPhone Safari supports a meta tag called viewport.

This one also deserves a whole blog post for itself. For now let's just say that this is a meta tag that Apple came up with to make a web page capable of "telling" the browser how it wants to be shown.

More information: http://developer.apple.com/safari/library/documentation/appleapplications/reference/safarihtmlref/articles/metatags.html and http://developer.apple.com/safari/library/documentation/appleapplications/reference/safariwebcontent/usingtheviewport/usingtheviewport.html

In QtWebKit trunk we already have support for this with a nice API. You must connect the signal from QWebPage::viewportChangeRequested(const ViewportHints& hints) to a slot of your mobile web view and use what is provided by QWebPage::ViewportHints to updated your viewport size, scale range, and so on.

This can be tricky and that's why I'm not going deeper on it right now. Since I know you are curious about it I'll leave you with one more exercise! So try to understand how the guys from MicroB and Firefox Mobile dealt with this:

http://hacks.mozilla.org/2010/05/upcoming-changes-to-the-viewport-meta-tag-for-firefox-mobile


Enabling the tiling

We haven't actually enabled tiling yet, so lets go ahead and do that. That is very simple as it is basically a setting:

    QWebSettings::globalSettings()->setAttribute(QWebSettings::TiledBackingStoreEnabled, true); 

Voila! Mind that if you are going to add animations to your zoom/scale or want to implement a fancy kinetic scrolling you might want to take a look at QGraphicsWebView::setTiledBackingStoreFrozen. With this you can avoid updates to your tiles during an animation, for instance.


Avoiding scrollable sub elements

One big issue with the above is that, iframes and sites using frames can contain scrollable sub elements. That doesn't work well with the touch interaction model, as you want a finger swipe to scroll the whole page and not end up just scrolling a sub frame. Most mobile browser work around this by enabling something called frame flattening.

Going straight to the point:

    QWebSettings::globalSettings()->setAttribute(QWebSettings::FrameFlatteningEnable, true); 

This will make all frames from a web page expand themselves to the size of their contents, keeping us free of scrollable subareas.

37 comments:

Enrico Ros said...

Congrats Kenneth, I think you got the architecture right! How many threads are you using here? Is there some little performance loss versus 'immediate' rendering?

winnie said...

moncler
moncler branson
moncler jackets
moncler jacken

bandage dress
herve leger bandage
herve leger

Dsquared sneaker for men
Dsquared2 jeans
Dsquared2

Moncler
Moncler Jacket
Moncler jackets

led said...

Christian Louboutin Tall Boots
Christian Louboutin Wedges
Christian Louboutin Pumps
Christian Louboutin Sandals
Jimmy Choo Shoes
Moncler Kid's Jackets
Moncler clothing wholesale
Wholesale Moncler Jackets
discount Moncler Jackets
http://www.shopmoncler.hk/moncler-jackets
http://www.utsource.net/BUK444-500B.html
http://www.utsource.net/MG50Q6ES41.html
http://www.utsource.net/UM150CDY-10.html
http://www.utsource.net/MP7002.html
http://www.utsource.net/TMS320F2812PGFA.html
http://www.utsource.net/CEM3374.html
http://www.utsource.net/2sj74.html

sweet said...

lucky to read your blog! It makes me have the courage to stick to it!
supra cuban
supra tk society

Anonymous said...

We hear of the mbt shoes clearance masque no more.Some time elapsed, after the death of our author, before any of his later productions appeared : two small editions of his minor pieces were at length sent to the press in 1640, coach bags outlet and in the subsequent year a wretched reprint of the first folio, and a second volume of the same km-, containing his dramatic pieces from 1612, several masques, and all that could be found of his occasional poetry, coach outlet coupon were published together. Several of the comedies appear to have been taken from the prompter's book, and surreptitiously printed (but not published) during the author's life; how the rest were procured I know not. cheap coach purses Such of his dramas as were revived at the Restoration were printed separately ; and in 1692 the whole of his writings were again collected, and published in one huge folio volume. The demand for his works must have been considerable for those days, since in mbt trainers 1716 the booksellers were encouraged to prepare another edition, which they gave the world in six volumes 8ro. This publication was merely a reprint of the old copy, and with this, defective as it was, the town was content till the year 1756, cheap coach bags when a more complete edition, in seven volumes, 8vo, was published by the Rev. Peter Whalley, LL. B.Mr. Whalley had received an academical education, and he was competent, in some measure, to the undertaking. He did little, however, for the poet ; the form of five finger shoes the old editions was rigidly observed, and though a few notes were subjoined, they were seldom of material import, and never explanatory of the author's general views, though they occasionally touched on his language. It is not a little remarkable that this gentleman, who was master of the Grammar School 5 finger shoes of Christ's Hospital, and must naturally have been somewhat conTenant with the ancient writers, should not have ventured on one remark of a literary nature, every thing of this kind, which occurs in his edition, being, coach outlet store online as I discovered with some surprise, taken from Upton and others.Whether Whalley was diffident of himself, or the gentlemen volunteered their assistance, I have no means of knowing ; but he availed himself occasionally of the aid coach purses outlet of Sympson and Scward, (the editors of Beaumont and Fletcher,) who led him astray, and where he would have been simply wrong, if left to himself, rendered him absurd. In one pleasant way of making notes, and swelling the bulk of the book, they all agreed.

abercrombiefitch said...

Most women would love to own an authentic cheap christian louboutin shoes The problem comes when people try to match that goal up with finding a Christian Louboutin Outletthat fits their budget. We have heard stories that it is possible to find discounted designer shoes, but it is easy to get frustrated when the cheapest shoes you are able to find are priced well into the hundreds of dollars. Christian Louboutin DiscountLet's cut right to the chase. However desirable all but the wealthiest women may find designer label shoes, Louboutin Outletmost of us are going to have at best one or two cheap christian louboutin clearance shoes if we insist on buying new at retail.Christian Louboutin 2011 Christian Louboutin Ankle Shoes if we want to have an assortment of such treasures, we're going to have to make our purchases of lower-priced, Christian Louboutin Heelsdare I say cheap shoes, while not sacrificing original designer heritage for them.Christian Louboutin Peep ToeOf course, when shopping for cheap cheap louis vuitton bags one can put oneself at the mercy of charlatans.Sad to say, there are those among us who have no qualms about passing off a counterfeit designer bag as the real deal, and perhaps having a good laugh at our expense.Recognizing that,Christian Louboutin Pumpsunless one has grown up in a family of purse collectors or had some kind of similar experience of being around genuine articles of quality, Christian Louboutin Sandalssome education is in order. Some people suggest going to christian louboutin outlet stores and examining the goods found there.Christian Louboutin Tall ShoesSuch a plan can be an advantage, Ghd Australia in the outlet store.
Five Fingers Shoes
Happy to see this article as it is just what I have looking for and I am looking forward to another great article from you. You may be interested in CHI us
Your space is really pretty, have no interest in to my chi for sale space to share it? thank you Oh.Buy Nike Shoes

Koon said...

good article and valuable post, thank for idiot sharing okkk
Counter Depth French Door Refrigerator
How to Get Into Acting
Breville Smart Oven
How to Impress Woman
Picking Lottery Numbers
Canon Powershot A800
Adidas Barricade
Adidas Barricade 6.0
Cuisinart TOB-195
Acne Conglobata
Acer 11.6 Netbook
500 payday loan
acne inversa
VIZIO M261VP
checkmate payday loans
Pengurusan Masa
How To Stop Panic Attacks

Counter Depth French Door Refrigerator
How to Get Into Acting
Breville Smart Oven
How to Impress Woman
Picking Lottery Numbers
Canon Powershot A800
Adidas Barricade 6.0
Cuisinart TOB-195
Acne Conglobata
Acer 11.6 Netbook
500 payday loan
acne inversa
VIZIO M261VP
checkmate payday loans
Pengurusan Masa

Shak Olreal said...

Congratulations Kenneth, I think he has the right architecture! How many threads are used? Is there a little performance loss compared to "immediate" representation?WOW ItemsWOW GearBuy WOW ItemsCheap WOW ItemsWOW Items GoldCheap Tera Gold Tera GoldTera Power LevelingTera AccountTera Items

jonno_081 said...

Hello, I'm having a bit of a problem with the webview control. In the QT designer, it works and I can see the webpage, etc. When I try to build the project in netbeans, it gives me an unfound library error.

Any advice?

John
PHP-Web-Host.com

Unknown said...

wholesale jordan shoes, christian louboutin shoes, Discount Louis Vuitton, ugg pas cher, uggs outlet, ugg boots, ugg australia, Bags Louis Vuitton, ugg, Christian Louboutin Daffodile, Christian Louboutin Bois Dore, cheap christian louboutin, Cheap Louis Vuitton Handbags, christian louboutin remise 50%, Discount LV Handbags, ugg soldes, uggs on sale, cheap jordans , christian louboutin, discount christian louboutin, Cheap LV Handbags, Air Jordan 11 Gamma Blue, cheap nike jordan shoes, discount nike jordans

werwer said...

Is usually a multi-OS hard disk drive checking software. Its objective is to find, check, diagnose and repair hard disk drive problems, show hard disk health, efficiency degradations and failures. Hard Disk Sentinel Key provides entire textual description, guidelines and displays/reports probably the most extensive data about the HDD inside the laptop or computer or within an exterior enclosure (USB / e-SATA). Many unique alerts and report solutions can be found to guarantee highest basic safety of your worthwhile data.
Hard Disk Sentinel monitors hard disk standing, including health and fitness, temperature

newcrackkey said...

office 2010 product key incorporate an implicit screen catch apparatus, foundation evacuation instrument, new shrewd craftsmanship formats, and creator authorizations. You can utilize these provisions with the assistance of the Microsoft Office 2010 item. The Office form of the past rendition replaces with a menu button that prompts a total window document menu, known as the Back Stage View, which gives more helpful admittance to the undertaking. Coordinated capacities, like printing and sharing. So this is one of the most mind-blowing office suites to use with the Microsoft Office 2010 key.

zia sir said...

nice post
Ummy video downloader Crack
HitmanPro.Alert Crack
Any Video Converter Ultimate Crack

Naeem S said...

I'm glad you like it as much as I did.
Sketch, a theme for your author, is here.
a classy purchase
A lack of interest in what you have to say.
You'll be back before then, and you'll be back a lot more if you help our expansion.
bandicam crack
rekordbox crack
vector magic crack
acronis true image crack

Sabir said...

And I appreciate your work, I'm a great blogger.
This article bothered me a lot.
I will bookmark your site and continue searching for new information.
hide all ip crack
wondershare pdfelement pro crack
microsoft office 2019 crack
auto tune pro crack

Crack Key said...



I like your all post. You Have Done really good Work On This Site. Thank you For The Information You provided. It helps Ma a Lot.
it Is Very Informative Thanks For Sharing. I have also Paid This sharing. I am ImPressed For With your Post Because This post is very beneficial for me and provides new knowledge to me. This is a cleverly written article. Good work with the hard work you have done I appreciate your work thanks for sharing it. It Is a very Wounder Full Post.

systweak advanced driver updater crack
systweak advanced driver updater crack
systweak advanced driver updater crack
systweak advanced driver updater crack
systweak advanced driver updater crack
systweak advanced driver updater crack
systweak advanced driver updater crack

unknown said...

This is a beneficial software for both you and myself.
There were no errors detected throughout the audit.
It's something you can take advantage of. I hope you find it enjoyable.
avg internet security crack
microsoft office 2019 crack
long tail pro platinum crack
adobe premiere pro cc crack

Unknown said...

I like your all post. You Have Done really good Work On This Site. Thank you For The Information You provided. It helps Me a lot.
it Is Very Informative Thanks For Sharing. I have also Paid This sharing. I am ImPressed For With your Post Because This post is very
is very beneficial for me and provides new knowledge to me. This is a cleverly
written article. Good work with the hard work you have done I appreciate your work thanks for sharing it. It Is very Wounder Full Post
I would like to thank you for the effort you put into writing this page.
I also hope that you will be able to check the same high-quality content later.
Really, your creative writing skills have inspired me to create my own blog now😉
red dead redemption crack
red dead redemption 2 pc game with crack
red dead redemption empress pc game with crack
red dead redemption 2 crack

Muhammad Sajjad Ali said...

You have a great site, but I wanted to know if you know.
Any community forum dedicated to these topics.
What was discussed in this article? I really want to be a part of it.
A society in which I can obtain information from others with knowledge and interest.
Let us know if you have any suggestions. I appreciate this!

magix acid pro crack
magix acid pro crack
magix acid pro crack
magix acid pro crack
magix acid pro crack
magix acid pro crack
magix acid pro crack
magix acid pro crack

gawgfwgsvgvgV said...

Oh my goodness! Impressive article dude! Thanks, However I am having troubles with your RSS. I don’t understand the reason why I am unable to join it. Is there anybody having the same RSS issues? Anyone that knows the answer will you kindly respond? Thanx!!
endless space awakening crack
adgaurd content blocker premium apk
simply good pictures crack
windows 7 ultimate crack
dragon age ultimate edition crack
kmsauto net portable for windows crack
abelssoft antilogger crack

Artghural said...

Thank you for your commitment to your website and the information you give.
It's always good to discover a new blog.
You did an excellent job!
avast pro antivirus crack
advanced systemcare ultimate crack
visual studio 2010 product key crack
voicemod pro crack

ISPR said...

archeage crack
free mp3 cutter and editor crack
av voice changer crack
apowersoft video download capture crack
ms officce 365 crack
home designer pro crack
screenpresso pro crack
reaver pro crack

Crack said...

Hi! Your Blog Writing is Very Wonderfull And Amazing. Thanks For Sharing...
ant download manager pro crack
driver genius pro crack
sidify music converter
4k video downloader crack
navicat premium crack

MURSALEEN ALI said...

wHello! Please know how much I enjoy your site and how much I look forward to the new content you offer.
Which of your blog posts should I pay attention to?
Inquiring minds are invited to share their knowledge of other online resources that could be of interest to me.
That's really kind of you.
avira internet security suite crack
coreldraw graphics suite 2017 crack
windows 7 home basic crack
avast premier crack
easeus data recovery wizard pro crack

Sony Pitter said...

Wow! Finally, I've located a website that provides useful study aids. As soon as I get it installed on my computer, It works flawlessly and is a breeze to use, so I'm thrilled to have gotten it for free.https://cracksys.com/foxit-pdf-editor-crack/

Unknown said...

I am very impressed with your post because this post is very beneficial for me and provide a new knowledge to me.
Thanks for sharing this post is an excellent article. Keep it up. I use the same blogging platform that you have and have.
it Is Very Informative Thanks For Sharing. I have also Paid This sharing. I am ImPressed For With your Post Because This post is very.
ms office 2007 product key
movavi video editor crack
protected folder crack
avg secure vpn
camtasia 9

crackadvise said...

I guess I am the only one who came here to share my very own experience. Guess what
I am using my laptop for almost the past 2 years, but I had no idea of solving some basic issues.
I do not know how to Free Download But thankfully, I recently visited a website named Crackadvise.Crack

ReaConverter Pro Crack

EF Commander Crack

Directory Monitor Pro Crack

Recover My Files Crack

VueScan Pro Crack

PlayOn Crack

softwear said...

I guess I am the only one who came here to share my very own experience. Guess what!? I am using my laptop for almost the past 2 years, but I had no idea of solving some basic issues. I do not know how to Crack Softwares Free Download But thankfully, I recently visited a website named xxlcrack.net/
Hard Disk Sentinel Pro Crack
ProfiCAD Crack

Maharpc said...

Thanks for the really awesome content share with us.. we are really helpful for your kind share.
maharpc

Muhammad Irfan said...

Nice & Impressive! I have not ever seen this type of blog Plz continue informative blog like this
ultraiso premium edition crack

millionck said...

download sony vegas pro 20 crack

jackhen said...

PowerISO Serial Key is a fantastic and ground-breaking CD and DVD photo recording technique. Power is a record pressure, which gives you to make and modify your photo documents. The clients can mount their photo documents as an inner virtual pressure.

Maharpc said...

Foxit Phantompdf Full crack is a software program developed by Foxit Software that allows users to create, edit, convert, and secure PDF documents. Some of the key features of Foxit PhantomPDF include:

Pet Spoiler said...

Nice expert pet groomers
dog mobile pet grooming dubai

professional movers and packers said...

keep it u ,good.
professional packers
professional movers

Professional Movers and packers in Abu dhabi said...

Good content.
professional movers and packers in abu dhabi
professional villa movers in abu dhabi

health said...
This comment has been removed by the author.