Andy Shaw

Qt Commercial Support Weekly #13 - Literals in qmake and loading resources in QTextEdit

2/9/2012 9:39 AM  | Posted by: Andy Shaw

 

Sometimes it may seem difficult to do things that should be easy, and sometimes things that appear to be easy turn out to be a lot harder than anticipated on paper.  This is one of the reasons why it is a good idea to turn to Qt Commercial Support as we know the tricks you can use to achieve certain things and the corners you can skirt around to get the end result you want.  Additionally, we are able to solve the bugs that are reported in Qt too and this can speed up the process of getting bugs resolved in Qt properly in the long term.

 

Note: This blog is best viewed with Mozilla Firefox. Internet Explorer is giving errors on the code snippet.


A lot of the time, especially with qmake, there are a number of undocumented ways that things can be resolved.  As I have said before in previous weeklies, qmake is a powerful tool when it comes to generating your makefiles and as a result there are a number of reserved characters, such as $, " and # that are interpreted in a particular way.  Luckily, there is already something in qmake that enables you to still have these characters literally displayed when possible.

Normally, you would escape the character to get the resulting character where you want it, however in qmake this does not work. What does work, instead, is to use the LITERAL_* variables to place the specified character in the specified place in the makefile.  For example, if you wanted to specified a # in the makefile you could do:



 QMAKE_POST_LINK += echo $${LITERAL_HASH}



which would expand to put:



 echo #



in the makefile for you.  If you want a dollar-sign, then you can replace HASH with DOLLAR and it will get the same result.  So, if you need to use  one of the reserved characters then it is worth trying LITERAL_ to see if you can get it to be placed in the makefile.  Failing that there is always support!


Another area of Qt which has been over the years a fairly popular and useful one is QTextEdit.  It needs no introduction as to what it actually provides. However, what may be news to some of you is how flexible it is when it comes to allowing you to insert images, a stylesheet or even custom resources from various sources.  To achieve this we need to look at QTextDocument which is the class that provides the actual document where the text editing takes place.  Again, this is fairly straightforward to use on the general level, but in this case we want to focus on the protected virtual function - loadResource().  What we can achieve with loadResource() is a means to load resources on the fly that have been referred to inside the HTML that is set on the QTextDocument which the QTextEdit is using.  For example if your HTML contains the following:



  <img src="mydata://red.png">



Then a normal web browser would not know how to interpret the mydata://red.png reference.  This would be the same when it comes to QTextDocument. However, what it will do in this case is call loadResource() to find out if the resource that it refers to is provided by the QTextDocument in some way.  So, what can be done here is that we can have the loadResource() function return an image that would represent the red.png image specified here.  How that image is created is entirely under the control of the developer. You could create an image at this point and simply paint it red, or blue, or draw a cat. The sky is the limit. You can also pull the item needed from a storage somewhere.  It will also work with any html you insert later on, including image references (for example) added via QTextCursor::insertImage().  So, it is fully flexible in enabling you to adapt the QTextDocument to load the resources from where you want to pull it from.



So until next time, happy coding :)

Comments:


hilaga martin | 5/16/2012 9:44 PM
this is a great thing to know " It will also work with any html you insert later on, including image references (for example) added via QTextCursor::insertImage(). So, it is fully flexible in enabling you to adapt the QTextDocument to load the resources from where you want to pull it from"..more power skip tracing

Add new comment:

User verification Image for user verification  
     

Tags

Archive

Authors

Pasi Matilainen

Pasi is a Software Specialist working at Digia, Qt Commercial R&D and he concentrates on Mac OS X development. Pasi holds an M.Sc. degree in Information Technology from the Tampere University of Technology, Finland.

Tarja Sundqvist

Tarja is a Senior Software Engineer in the Digia, Qt Commercial Support team. She has been working in Digia for over 10 years in various positions: software development, testing, error management. Now, Tarja is focusing on helping Qt Commercial customers with their daily Qt problems on Windows and Linux platforms. Tarja holds an M.Sc. degree in Information Processing Science from the University of Oulu, Finland.

Akseli Salovaara

Akseli is a Software Specialist at Digia, Qt Commercial R&D and is responsible for the Qt Commercial releases and deliveries. Akseli holds an B.Sc. degree in Information Technology from the University of Applied Sciences in Jyväskylä, Finland.

Samuli Piippo

Samuli is a Software Specialist at Digia, Qt Commercial R&D with a concentration on  embedded Linux and RTOS development. Samuli holds an M.Sc. degree in Information Processing Science from the University of Oulu, Finland.

Katherine Barrios

Katherine is the Marketing Manager at Digia, Qt Commercial. She is responsible for getting the word out about Qt Commercial to the Qt ecosystem and working together with our customers and the Qt community to further extend the love for Qt on desktop and embedded. She was previously employed at Nokia, Qt Development Frameworks as Program Marketing Manager and is based in Oslo, Norway.

Sami Makkonen

Sami is a Senior Product Manager working at Digia, Qt Commercial R&D and he is responsible for the product planning including new feature development and enhancements to existing functionality. Sami holds an M.Sc.(Econ.) degree in Computer Science.

Andy Shaw

Andy is the Head of Support at Digia, Qt Commercial and has been working with Qt and supporting customers using Qt for 11 years.  He thrives on solving customer problems and getting feedback from them.

Tuukka Turunen

Tuukka is the Director of R&D at Digia, Qt Commercial and is responsible for the planning, creation, verification and delivery of the Qt Commercial product. Tuukka holds a M.Sc.(Eng) and Licentiate of Technology degrees in Computer Science.

Qt Commercial Team