After looking through the QML code for a couple of applications, I’ve gotten the impression that Cascades application developers have some different habits than those writing QML with the QtQuick elements. While my sample size was pretty small, I think these several points may prove particularly useful to BlackBerry 10 application developers as they write their QML UIs. These tips are focused on enhancing the performance of your QML programs, both in terms of application speed and development/maintenance speed.

Avoid declaring variant properties

The variant type seems pretty useful in QML, and it’s certainly versatile, but don’t over use it. Using variant when you could use a more specific type means that you get less compile-time help and that you pay a performance penalty (to get the real value into and out of a QVariant all the time). If the variable is always a number, use int or real types. If it’s always a string, call it a string.

A common usage of variant is to store controls, but you can use any type exposed QML as the type for your property. For example you can use ‘property Container delayedPart: null’ to hold an item you instantiated dynamically, so long as the root item of the component was a Container or a type based on a Container. Just keep in mind that 0 is a number, not a reference, so use null instead of zero for the initial values of these types. Any type QML knows can be used, not just Container, so there’s no need to use variant for your TextStyleDefinitions or Labels either. There is a bug in Qt 4 where you will still need to use the variant type when passing object references in a QML defined signal handler, but aside from that bug most usages of the variant type can be replaced with a more specific and appropriate type.

Assuming the root item in the component definition is a Container{}. Any object type exposed to QML can be used, not just Container, pick the right one for your component.

Do not use Javascript files just for storing properties

Related to avoiding variant properties, I’ve seen a similar problem with storing properties in JavaScript files. Again this leads to inefficiency in having to wrap and unwrap script values, and again you lose the type information. There’s another problem if you try to alter the properties, as most JavaScript files have a new instance loaded for each QML file that uses them. For better and safer code store these properties in a QML object. This allows for typed properties and makes the instance management a lot clearer and harder to get wrong.

Avoid using .connect

Like the variant type, the connect method on a function is sometimes necessary but not intended to be used a lot. The problem here is not one of performance, but of readability. By connecting signals in this manner you mess up the declarative flow of the code. Normally you should be using the signal handers on the objects themselves instead of adding imperative code into an unrelated signal handler (like creationCompleted). For example you could use

This leads to less and clearer code. Save the connect method for when the object emitting the signal was created in C++ and so cannot be accessed from QML.

Use ComponentDefinition for repeated items

If you need to create many similar items, create them dynamically from a ComponentDefinition instead of copying and pasting the QML code for it. Just add a property int index to the top item in the component, and then add some code like the following:

Any slight changes needed between the instances can be based off the index property, and the maintainability is immensely better than if those components had been copied and pasted over and over again.

Use ControlDelegate (or ComponentDefinition) for delayed loading

A good tactic for improving your application start time is to delay loading of any parts of the UI which are not essential for immediate use. It’s easy to do this using ControlDelegate, just wrap the elements you can wait for like so

And after the essential elements are loaded, set delegateActive to true. Once your control is loaded, it will function the same as before.

Let the UI flow through QML

Cascades is not purely QML APIs and it’s quite easy to drop down to C++ if you want to. Often this is a good way to do expensive logic faster. Normally though you should only be moving business logic to C++, not UI logic. For UI logic it’s usually better to keep it all in QML for two reasons. The first is that it’s easier to develop and maintain, as the entire UI in both appearance and flow is defined in one place. You should architect your application so that the UI flow is controlled at a high level from QML. If you need a complex C++ controller class or initialization logic, expose that to QML and control it from QML (by calling a function or by just creating it) to perform the complex logic, instead of having it managed in C++ and emit a signal to QML when it’s done. This places the high level UI flow just in QML, making it a lot easier to follow and manage, despite complex logic still being performed in C++. A common method of doing this is to expose a single QObject to the root context, containing all the application global methods and properties, which are then invoked, queried, or bound to from QML.

Note that because the app object is exposed from C++, you have to use the connect function to hook up the signal.

The second reason to keep your UI in QML is that you can actually lose some performance benefits when passing messages back and forth from the QML file to your C++ class. If you just need to handle some touch events in a way that doesn’t have a pre-made gesture you can store a couple of state variables inside the QML file instead of creating a C++ controller class. It saves you both development and run-time costs of hooking up the touch events from QML into the special object, and it keeps all the application touch handling code in one place.

Use your own URI

I’ve seen some code that exposes its C++ types like this:

qmlRegisterType<MyType>("bb.cascades", 1, 0, "MyType");

Do not do this. Features are being added to the QML language to prevent you from injecting types into existing imports, meaning that this code will break in the future. But more importantly, this is avoiding the whole point of the module import system which leads to a lot of cases where your code can break even sooner! It also makes the code easier to understand and debug if proper imports are used. This can be as simple as:

qmlRegisterType<MyType>("App", 1, 0, "MyType");

And then import App 1.0 in your QML file.

CFA comment: Even though using your own namespace is correct, the fact is that you will currently break the momentics preview if you use your own URI. We recommend that during development you use bb.cascades and once you are done with the UI you change it to your own namespace.

So there’s a few tips for writing faster QML with Cascades. Faster to write, faster to run, and faster to maintain. All worthy goals.

On June 6th, more than 500 developers across China attended the long-awaited Qt China Developer Day 2013 in Beijing. It’s extremely exciting to see how familiar Qt developers are with BlackBerry 10 platform. As soon as registration started, the BlackBerry booth was flooded with developers!

Mike Zhou from our Developer Relations team presented on overview of the recent developments in Qt on BlackBerry 10 and discussed future plans. Coming all the way from Europe, our very own Qt Expert Vladimir Minenko also joined Mike on stage to co-present the technical session on “Developing with Qt for BlackBerry 10”. The room was packed to the brim with eager developers wanting to learn how to build a BlackBerry 10 Cascades app using Qt.

Vlad and Mike showcased really cool Qt apps on BlackBerry 10 smartphones developed with Qt 5.0. Not only that, they showed the developers how easy it was by porting apps live on stage! Think converting from Qt to a BlackBerry 10 native app would be hard? Not at all when you joined the onsite coding demo session on how to easily convert their existing Qt app to Cascades based applications. Developers were very excited about BlackBerry 10 supporting the Qt framework and how easy the porting process is.

When Mike and Vlad announced “Bring your Qt app source code to BlackBerry booth to win a BlackBerry smartphone” challenge, the developers went wild! So they came forward, and so easily, they ported into BlackBerry 10.

Jonah Lin, BlackBerry Elite from China joined BlackBerry experts on stage to showcase 2 of his apps developed on Qt, and also shared his experience on commercialization and internalization.

QNX also showed their support to Qt onsite! Howard Zhang, the QNX Expert in China hosted the QNX Corner with hardware demos at the booth, sharing insights on QNX technology and future trends, attracting his fair share of keen and excited developers!

Qt developers often ask the following questions: Is there a porting guide to help me port my Qt apps to BlackBerry 10? What about porting Qt Quick to BlackBerry 10? How do I configure Qt Creator for BlackBerry 10?

The good news has arrived: you now have a porting guide covering the most commonly-encountered use cases. This guide provides the necessary information to developers of native mobile apps who fall in one of the following categories:

Developers who have Qt apps written in C/C++/ QML/JavaScript and intend to have their apps running on the BlackBerry 10 platform;

And finally, developers who want to take advantage of the Cascades UI framework and change their existing UI to the Cascades framework.

BlackBerry supports developers in all of the above described scenarios, and so this guide provides developers with best practices for each category. It also explains why a particular practice is considered the best one.

With all the excitement around BlackBerry 10 launch, one key request from our developer community is how to bring your existing BlackBerry Java applications to BlackBerry 10. As always, we at RIM developer relations constantly strive to break any barriers for developers.

Starting today, we will be publishing a series of blog posts specifically designed to help port your existing BlackBerry Java application to BlackBerry 10. As you may already be aware, BlackBerry Java is no longer supported on BlackBerry 10. Our goal here is to provide you all the info needed to quickly migrate your app functionality.

The BlackBerry 10 Native SDK enables you to develop applications in C/C++ and provides an open-source friendly environment for porting POSIX compliant code. It comes with the Cascades framework allowing you to build visually rich applications. The key focus of this development guide is specifically on BlackBerry 10 Cascades development. For more information on BlackBerry Native Development, please refer to http://developer.blackberry.com/native/

What is Cascades?

BlackBerry Cascades is built using the Qt framework, which allows Cascades to leverage the Qt object model, event model and threading model. BlackBerry Cascades enables you to develop your UI in C++, Qt Modeling Language (QML), or both. Qt Modeling Language (QML) is a declarative language that’s based on JavaScript and is included as part of Qt. You can use QML to describe the structure and behavior of a set of UI controls. BlackBerry Cascades uses a modified version of QML to let you create UIs for your apps.

One fundamental importance when looking to develop your BlackBerry Cascades application is deciding when to use QML or C++. You can use both of these languages in your application, but as a general rule of thumb you should use QML for your UI development and C++ for your business logic. Please refer to the below link more information on BlackBerry Cascades development and to download Native SDK and BlackBerry 10 Simulator. https://developer.blackberry.com/cascades/

API Feature Parity

One of the main challenges while migrating app functionality from BlackBerry Java to Cascades is finding the correct mapping of APIs. To simplify this process, below is an API Feature Parity table that covers the most common BlackBerry Java APIs and their corresponding equivalent API in Cascades.

As you can see from the above table image, most of the common Java APIs have a direct mapping in Cascades making it easy for you to port your existing BlackBerry Java application to BlackBerry 10.

Update – Brian Scheirer pointed out that there have been some awesome community samples too! -Ed.

For me, 2012 has definitely been a year of helping developers learn about our native and Cascades development tools. If you’re like me, you’re probably spending the holiday nights doing some coding – so I’ve put together this list of “The Best of Native Development for 2012″.

Cascades WebcastsThousands of developers from across the globe joined us for four webcasts to learn beginner to advanced Cascades concepts. We were able to interact with developers directly, answer questions, and get immediate feedback as samples and code were displayed. The format proved so successful that we’ve hosted many more webcasts on topics such as the BBM social platform, augmented reality apps, and OpenGL integration.

Porting GuidesIf you have existing content for another platform, we are making it easy to bring that content to the BlackBerry 10 platform. Being able to re-use code across platforms reduces development and maintenance costs. Distributing your application to multiple platforms opens you up to whole new market segments and increases your potential user base. There a number of informative porting guides available to help you build for BlackBerry 10.

Samples
The best and easiest way to learn to program is to look at sample code. For every feature that BlackBerry 10 offers there is a sample project showing you how to put it into action. Whether it’s integrating with BBM, using the Invocation Framework, getting sensor information, or communicating via Bluetooth, we’ve got you covered.

There are samples built right into the Momentics development environment, on the Cascades and Native microsites, and hosted on our github repo. And if you find a feature that we missed, or an API without a clear example then let us know on Twitter @BlackBerryDev or in our developer forums and we’ll add what you need to get started!

Stump the BlackBerry DevsIn August we launched a challenge to the BlackBerry Developer community to come up with a UI that would be difficult to implement in the Cascades UI framework. The Cascades tools development team would try to implement the submissions in a flexible manner that could handle multiple screen resolutions and form factors. They implemented three of the challenges and presented the solutions during the Unconference Day at BlackBerry Jam Americas. We were able to show that Cascades is robust framework that is able to create stunning interfaces even when faced with challenging design requirements. All the solutions are hosted on ourgithub repo.

If you have any Cascades or native dev questions – check out our forums and connect with me on Twitter.

Qt is an important part of our BlackBerry 10 Cascades framework. We were proud to be part of Qt Developer Days in Santa Clara, where we had our own booth set up to showcase the BlackBerry 10 OS and to speak to developers about the excitement surrounding the upcoming BlackBerry 10 launch event on January 30th (not long to go now!). Some developers did not really know what BlackBerry 10 was but after seeing the platform for themselves, they shared our excitement. This was a user interface that they had not seen before, and something a lot of the MeeGo community liked with its simple but powerful gestures.

Alec Saunders, VP Developer Relations at RIM, had a keynote speech where he presented our BlackBerry 10 ecosystem story. He also spoke to RIM’s involvement with Qt and how developers can be rewarded for porting their Qt applications to BlackBerry 10. BlackBerry World is a going to be a level playing ground for your applications to actually have a chance of being discovered and not lost in the crowd like on other platforms.

We seeded eager developers with BlackBerry 10 Dev Alpha testing devices for them to port over their existing MeeGo / Symbian Qt applications, and also provided technical support. For Qt Mobile Application Developers, BlackBerry 10 offers the ability to get a return on your existing development investments. Depending on the complexity of your application, the port over to BlackBerry 10 can really be a painless experience.

We do realize that not everyone can get to these events, and so we have the BlackBerry Qt Porting Program, an online offer so that eligible developers can get their hands on BlackBerry 10 Dev Alpha testing devices. For complete details, see the Official Program Terms and Conditions.

We also have an offer of a one-time donation of $10,000 USD to the Qt Project Hosting Foundation if 50 eligible Qt apps are ported to BlackBerry 10 by the Qt Community and then posted and available for sale in BlackBerry World by January 30, 2013 (1)

Apps must be submitted to BlackBerry World by January 20, 2013 and be approved.

50 Qt apps must be up for sale in BlackBerry World by January 30, 2013.

It was fantastic being part of Qt Developer Days in Santa Clara, but this is really just the start for us and by no means the end. We are actively working with Digia and the Qt foundation to further strengthen Qt and our relationship with it and also with you.

1) To be eligible, fifty (50) Qt Apps (“Apps”) must be submitted to BBAW by the Qt Community by 11:59:59 PM Eastern Time (“ET”) on January 20, 2013. Apps will be accepted beginning at 8:00 AM ET on December 8, 2012. Allow a minimum of ten (10) days for BBAW approval. Approved apps must be posted on BBAW for sale on or before January 30, 2013. Apps are not eligible if they simply contain a web launcher or shortcut or a web browser (“Excluded App”). RIM will confirm if Qt Community is eligible for $10,000 USD donation to the Qt Project Hosting Foundation (“Donation”) by February 28, 2013. Allow 8-12 weeks for delivery of Donation. Void where prohibited or restricted by law.

As a follow up to our latest blog post on how to write Qt apps for BlackBerry 10 and the recent Qt Developer Days, we are hosting a webcast this Friday December 14th at 9 am EST with Vladimir Minenko, Technical Manager, Qt, from RIM to give developers the opportunity to learn more about how Qt is used on BlackBerry 10 and how it can be used to develop apps on the platform. Vlad will talk about getting started with the development tools and show a demo of a standard Qt Quick application running on BlackBerry 10. He’ll also discuss what to expect while porting larger and more complex Qt applications and the differences between using pure Qt UI APIs versus Cascades to build your apps. There will be an opportunity for a Q&A session at the end so bring your questions! Finally, learn more about the Qt offers that we providing to our developers who are porting or developing new apps for BlackBerry 10.