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

 

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