Monday, November 17, 2014

Native UI in Qt on Android (without QtQuick.Controls)

TL;DR - Yes, you can do a fast, no-compromise native UI without QtQuick on Android, and you'll use a lot less resources than a full QtQuick app, at the cost of somewhat messy code.

Nowadays Qt for Android comes with a nice QtQuick-compatible set of native-looking Android controls in the form of QtQuick.Controls. This blog post is not about them :) Let's go a bit off the beaten path - can we create a Qt application with a graphical, performant, native UI without using QtQuick (or QWidgets)? What are the advantages and disadvantages to such an approach?

In a blog post Artem Marchenko lists a couple of approaches how a Qt based native-looking UI would be possible, but these all include QML in one form another, which is not quite what we're after in this particular case. Another interesting project is Ben Lau's QAndroidRunner which combines native UIs and QML. Here, however, we'll focus how far can you get without ever touching QML or QtQuick.

The key to using Android classes and resources is the QAndroidExtras module (it's an add-on module that has been around since Qt 5.2). The class I'm heavily relying on is QAndroidJNIObject which allows Java class/object instantiation and manipulation. Thus, the two includes we'll use to enable creating native objects are

#include <QtAndroidExtras/QAndroidJniObject>
#include <QtAndroidExtras/QtAndroid>

When it comes to Android UIs, the first stop is the Activity - on Android activities are how we interact with the user. This is effectively a window on which we put all our widgets and UI elements. Luckily, starting with Qt 5.3, we have a simple way of retrieving an activity with the QtAndroid::androidActivity function.

QAndroidJniObject activity = QtAndroid::androidActivity();

Let's get to the meat of the matter - we'll need to create native layout and widget objects and set their parameters from Qt. Here's how that looks like

QAndroidJniObject layout("android/widget/RelativeLayout", 
  "(Landroid/content/Context;)V", 
  activity.object());

The first parameter is the Java class name, the second the Java method signature (see here for a few more examples, "javap -s" is your friend), and the third parameter is the actual object used as the parameter (as activity is a QAndroidJniObject, we need to use the object() method). With the call above we create a Layout and assign it to the activity.

We can also call methods of our objects:

button.callMethod<void>("setTextColor", "(I)V", -1); // Color.WHITE

Putting all these together, we can create a basic, but full-fledged Android application:



...resulting in...


This "Hello world" code is clearly quite a bit more complicated than it's QML counterpart:



The complexity downside is quite obvious - it's hard to beat QML's brevity and clarity. Another, perhaps not immediately visible downside is that this particular style of C++ code is more error-prone - there is no type safety as the objects are generated dynamically, so on errors, it's easy to end up with NULL objects and segfaults.

Why would anyone use it, then? Let's take a look at resource consumption:

The APK size for the non-QML Android version of Hello World is 5,663,420 bytes, while the APK with the Controls included is 10,406,706. The difference could be even bigger, though, as the default android mkspec includes a few extra Qt modules. It should be possible to get the minimum APK size down to around 3MB. If you are using Ministro, this might not be as big of an issue, but for self-contained applications, or embedded, this can shave a few precious megabytes off of the application.

It's not just flash storage and network bandwidth we can save though - there is a memory-usage difference as well. While adb dumpsys meminfo is not a perfect way to measure memory usage, it is indicative:

40761 kB: org.qtproject.example.QtJavaHelloWorld
77531 kB: org.qtproject.example.QmlHelloWorld

While a ~35MB of minimum framework tax might not sound like much in the era of devices with several gigabytes of RAM, using a complex QML structure can inflate the difference further. On embedded and legacy devices every megabyte counts, so even this 35MB difference can help (plenty of low-end Android devices with 256-512MB of RAM).

There are other benefits to not using QtQuick controls - controls are a "lowest common denominator" approach designed to easy cross-platform development. It does not contain all UI widgets and elements, nor functionality offered by Android APIs. By using the approach as demonstrated, there is no compromise - the full UI arsenal is at our disposal, at the exact same performance as for regular Java apps.

Finally, the QtQuick controls version depends on the Qt version shipped with the application. In the non-Controls version we always get the native controls, with native styling and behavior, even if the platform release is newer than what our QtQuick.Controls version supports.

To summarize, the advantages to a Controls-less approach are:
  • Native UI performance
  • Smaller APK size (currently at least ~5MB less, potentially ~7MB)
  • Smaller memory footprint (35MB for Hello world, more as app complexity increases)
  • Full UI functionality available, regardless of Qt version
  • Styling always latest platform-native
Disadvantages

  • Not cross-platform
  • Significantly increased code complexity, especially with more complex UIs
  • Harder to debug due to dynamic nature and lack of tooling support

It's actually possible to mitigate some of the disadvantages, so stay tuned for further posts on this topic!

117 comments:

  1. Hi,I have one query regarding using JNI in android. Currently i am working on hybrid i.e Qt/QML application.I have separate .java file under android-sources folder.I am calling one static method in java class from c++. In that static method i’m setting new content using m_instance.setContentView(R.layout.main). As i am running this on UI thread, it perfectly displays content from mail.xml. But after completing my task i am not able to unload this view and go back to Qml view.So my question is that, can i use UI loaded in native activity as well as in QML as per my need or am i doing something wrong??

    ReplyDelete
  2. It is really a great work and the way in which u r sharing the knowledge is excellent.
    Thanks for helping me to understand basic concepts. As a beginner in android programming your post help me a lot.Thanks for your informative article.
    Android Training in velachery | Android Training institute in chennai

    ReplyDelete
  3. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Android Training in chennai | Android Training

    ReplyDelete
  4. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune

    ReplyDelete
  5. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune

    ReplyDelete
  6. I am really enjoying reading your well written articles.
    It looks like you spend a lot of effort and time on your blog.Keep Doing.
    data analytics training in bangalore
    Data Science Courses in Bangalore
    Best Data Science Courses in Bangalore
    data analytics courses in bangalore

    ReplyDelete
  7. I think this is the best article today about the future technology. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future
    oneplus mobile service centre in chennai
    oneplus mobile service centre`
    oneplus service center near me
    oneplus service
    oneplus service centres in chennai
    oneplus service center velachery
    oneplus service center in vadapalani

    ReplyDelete
  8. My manju - if you want girls mobile numbers then this website is best for you . you can visit on this website and get their information and you also can meet with thrm and go for a date . click here to use our website --- online dating website

    ReplyDelete
  9. Whatscr - many peoples want to join random whatsapp groups . as per your demand we are ready to serve you whatsapp group links . On this website you can join unlimited groups . click and get unlimited whatsapp group links

    ReplyDelete
  10. Whatscr - many peoples want to join random whatsapp groups . as per your demand we are ready to serve you whatsapp group links . On this website you can join unlimited groups . click and get unlimited whatsapp group links

    ReplyDelete
  11. Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
    Thanks & Regards,
    VRIT Professionals,
    No.1 Leading Web Designing Training Institute In Chennai.

    ReplyDelete
  12. Thanks for sharing valuable information.It will help everyone.keep Post.
    Dhankesari

    ReplyDelete
  13. Thanks for sharing valuable information.It will help everyone.keep Post.
    Kerala Lottery guessing

    ReplyDelete
  14. Go Health Science is the best resource to get all kinds of Knowledge about Health and Science updates on Healthy Life ideas.

    ReplyDelete
  15. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Selenium Training in Electronic City

    ReplyDelete
  16. I have read your blog and I gathered some needful information from your blog. Keep update your blog. Awaiting for your next update. Thanks
    DedicatedHosting4u.com

    ReplyDelete
  17. This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
    Android Application development
    Web application

    ReplyDelete
  18. This is a fantastic idea! I like it a lot because it's super easy for the audience to see the value of opting in. wonderful and amazing post very use full your post thanks for sharing your article
    Android Application development
    Web application

    ReplyDelete
  19. https://bloginhindi.in/online-paise-kaise-kamaye/

    ReplyDelete
  20. Coin master is one of the famous and grossing games right now. you can spin the wheel and believe in fortune to get a lot of Spins. But you end up getting small amounts of spins. For those who are searching for Coin Master unlimited spins, this article is very useful to them.

    https://meditips.in/coin-master-free-spins/

    ReplyDelete
  21. Thanks For Providing Us this Great Iformation .Get Our Some Quality Services Buy Adsense Accounts .
    Here is also avilable Buy Adsense Accounts .
    You Can Watch Adsense Earning Trick Here Youtube Channel Buy Adsense Accounts .

    ReplyDelete
  22. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. App Cloner

    ReplyDelete
  23. This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info: hoverwatch price

    ReplyDelete
  24. This comment has been removed by the author.

    ReplyDelete
  25. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work. play store

    ReplyDelete
  26. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks. smartwatch

    ReplyDelete
  27. Ios emulator for windows pc
    I think this is an informative post and it is very useful and knowledgeable. therefore,
    I would like to thank you for the efforts you have made in writing this article.

    ReplyDelete
  28. Really it's very good site..I want to share the site where you can enjoy freely watching the movies,dramas,songs,many other things so visit https://showboxappguide.com//

    ReplyDelete
  29. I like to recommend exclusively fine plus efficient information and facts, hence notice it: Android Tutorials

    ReplyDelete
  30. Your articles really impressed for me,because of all information so nice.sap tm training in bangalore

    ReplyDelete
  31. Linking is very useful thing.you have really helped lots of people who visit blog and provide them use full information.sap simple logistics training in bangalore

    ReplyDelete
  32. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving.sap wm training in bangalore

    ReplyDelete
  33. Really it was an awesome article,very interesting to read.You have provided an nice article,Thanks for sharing.sap ewm training in bangalore

    ReplyDelete
  34. This is really an awesome post, thanks for it. Keep adding more information to this.sap mm training in bangalore

    ReplyDelete
  35. thank you very much for share this wonderful article 토토사이트

    ReplyDelete
  36. Your topic is very nice and helpful to us … Thank you for the information you wrote.

    Upgrade your career Learn AWS Training from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Bangalore Training Academy Located in BTM Layout.

    ReplyDelete
  37. Really very happy to say, your post is very interesting to read. I never stop myself to say something about it. You’re doing a great job. Keep it up…

    Upgrade your career Learn SharePoint Developer Training in Bangalore from industry experts get Complete hands-on Training, Interview preparation, and Job Assistance at Softgen Infotech.

    ReplyDelete
  38. Great blog thanks for sharing Online marketing has changed how businesses run and succeed in today’s highly competitive world. With Adhuntt Media, your E-commerce store can reach for the stars and find the right customers. Our team of marketing analysts can analyze, strategize and implement the perfect path for you to take to beat your competition and come out as #1. Why talk about it when you can start right here at Adhuntt Media.
    digital marketing company in chennai

    ReplyDelete
  39. This comment has been removed by the author.

    ReplyDelete
  40. Nice blog thanks for sharing Try out different styles and bring your backyard back to life with the best garden service in Chennai - Karuna Nursery Gardens. Right from landscaping, terrace gardening and corporate services and renting plants, we do it all.
    plant nursery in chennai

    ReplyDelete
  41. Nice blog thanks for sharing Choosing the right place to buy your first plant isn’t that hard of a choice anymore. Presenting the best plant nursery in Chennai - Karuna Nursery Gardens is proud to showcase more than 3000+ plants ready to be chosen from.
    plant nursery in chennai

    ReplyDelete
  42. Excellent blog thanks for sharing It’s time you upgraded your Salon to meet global standards. Do that with the best wholesale cosmetic market in Chennai - The Pixies Beauty Shop.
    Cosmetics Shop in Chennai

    ReplyDelete
  43. This comment has been removed by the author.

    ReplyDelete
  44. I love your website your website is very good 파워볼사이트

    ReplyDelete
  45. thanks for this amazing
    article keep posting!
    try to visit our site once: http://hky7.com 토토 사이트

    ReplyDelete
  46. Top dance classes in Noida- indrayu

    Dance classes in noida with the world's best dancers. Teach your children with our great dance teacher,
    We provide dance classes for adults and kids,we are located in sector 50 noida

    Our Dance teacher s having experience in every kind of dance style
    You can also contact us for dance consultancy, we have won multiple medals and we also participate in different kind of dance programme, that’s why we are now top dance institute in noida 50, our success graph is going to above,
    We also do dance workshop and function, our student already working in bollywood,
    So if you are looking for
    Top dance classes in Noida, zumba classes in Noida
    also than you should contact Indrayu academy in noida


    Address: Kothari International School B-279, Sector 50, Noida 201301
    Phone: 0120 433 0844

    ReplyDelete
  47. CCTV & Surveillance Solutions Dubai

    We are best cctv installation services provider in Dubai
    We provide service at your doorstep. If you need any kind of camera of cctv security issue, we will help you with that, we are certifiedcctv installation dubai company , our engineer is certified by dubai administration,
    Our security expert can give you all details ,We also provide cctv if you need in bulk. We provide service in the office and for home also.

    ReplyDelete
  48. Check this very awesome site!
    https://kyb7.com 온라인 슬롯 머신 사이트

    ReplyDelete
  49. Nice post!
    try to visit our site once:
    http://hpx7.com 바카라사이트

    ReplyDelete
  50. Check this very awesome site!
    https://hky7.com 토토사이트

    ReplyDelete
  51. You give out information that is useful to many people
    Thank you very much for the good information
    I also share informative information.
    Would you like to visit my site?

    Online Slot Machine https://kyb7.com
    온라인 슬롯머신 사이트 )

    ReplyDelete
  52. You give out information that is useful to many people
    Thank you very much for the good information
    I also share informative information.
    Would you like to visit my site?

    https://hpx7.com 바카라사이트
    https://hpx7.com 바카라사이트
    https://hpx7.com 바카라사이트

    ReplyDelete
  53. You completed a number of fine points there. I did a search on the topic and found nearly all people will go along with with your blog. 먹튀검증

    ReplyDelete
  54. Hello, Neat post. There is a problem together with your website in web explorer, could test this¡K IE still is the marketplace leader and a large section of other folks will leave out your fantastic writing due to this problem.무료웹툰

    ReplyDelete
  55. You completed a number of fine points there. I did a search on the topic and found nearly all people will go along with with your blog. ccc admit card download

    ReplyDelete
  56. Thanks for sharing this exceptional and valuable information. It will help to improve our knowledge. Again thank you for sharing this marvelous data.
    Westminster Dog Show 2020 Live Stream
    Thank you very much.

    ReplyDelete

  57. Click here to cloud tech.
    .......................................................

    ReplyDelete
  58. It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.


    Selenium Training in Electronic City

    ReplyDelete
  59. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    Microsoft Azure Training in Electronic City

    ReplyDelete
  60. SEO (Search Engine Optimization) is the practice of optimizing content to be discovered through a search engine's organic search results. The benefits are obvious: free, passive traffic to your website, month after month 구글상위노출

    ReplyDelete
  61. We are struggling to do our daily chores & we should be thankful for these best bible study apps for helping us to nurture & protect our faith in Lords Best Bible Study apps

    ReplyDelete
  62. The iThermonitor app is a best trending thermometer app Android and iOS users as well. It intentionally allows users to read temperature through thermometer app

    ReplyDelete
  63. Awesome Post!!! I really enjoyed reading this article. It's really a nice experience to read your post. Thanks for sharing 바카라사이트

    ReplyDelete
  64. Nice post I have been searching for a useful post like this on salesforce course details, it is highly helpful for me and I have a great experience with this, Salesforce Training India

    ReplyDelete
  65. If you are looking for a job oriented practical based workday course syllabus curriculum at Workday training institutes in India then you are arrived at the right place called Workday training institutes in ameerpet because this institute is quite popular for this wonderful course not just in Workday training institutes in Jaipur and also proving job assistance for Workday training institutes in Pune.

    ReplyDelete
  66. Great post! I am actually getting ready to across this information, It’s very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.
    CRS Info Solutions Salesforce training for beginners   

    ReplyDelete
  67. I am so happy to found your blog post because it's really very informative. Please keep writing this kind of blogs and I regularly visit this blog. Have a look at my services.  
    This is really the best Top 20 Salesforce CRM Admin Development Interview Questions highly helpful. I have found these Scenario based Salesforce developers interview questions and answers very helpful to attempt job interviews. Wow, i got this scenario based Salesforce interview questions highly helpful.

    ReplyDelete
  68. Myself so glad to establish your blog entry since it's actually quite instructive. If it's not too much trouble continue composing this sort of web journals and I normally visit this blog. Examine my administrations.
    Go through these Salesforce Lightning Features course. Found this Salesforce CRM Using Apex And Visualforce Training worth joining. Enroll for SalesForce CRM Integration Training Program and practice well. 

    ReplyDelete
  69. This article is really helpful for me. I am regular visitor to this blog. Share such kind of article more in future. Personally i like this article a lot and you can have a look at my services also: I was seriously search for a Salesforce training institutes in ameerpet which offer job assistance and Salesforce training institutes in Hyderabad who are providing certification material. It's worth to join Salesforce training institutes in India because of their real time projects material and 24x7 support from customer desk. You can easily find the best Salesforce training institutes in kukatpally kphb which are also a part of Pega training institutes in hyderabad. This is amazing to join Data science training institutes in ameerpet who are quire popular with Selenium training institutes in ameerpet and trending coureses like Java training institutes in ameerpet and data science related programming coures python training institutes in ameerpet If you want HCM course then this workday training institutes in ameerpet is best for you to get job on workday.

    ReplyDelete
  70. Never too late to start learning at Salesforce Training in Australia even though you don't have any programming knowledge you can excell in Salesforce Training in London United Kingdom (UK) because it is all about your customers, so this time find the best Salesforce Training in Europe. This way we will learn Salesforce CRM.

    ReplyDelete