Qt World Summit 2023: Berlin awaits! Join us now
最新版Qt 6.3已正式发布。 了解更多。
最新バージョンQt6.5がご利用いただけます。 詳細はこちら

Qt Commercial Support Weekly #8: A Couple of QCoreApplication/QApplication Tips

This will be the last support weekly post of 2011, but there is no need to be sad as we will be back again in the new year as I am taking some (well deserved?) time off next week.


This week we will cover a couple of pieces related to QCoreApplication/QApplication which will hopefully be useful to some of you out there.  


First of all, what seemed to be a recurring related problem last week was problems connected to plugins.  As Qt is so diverse it is possible to use it in some small applications purely for doing some simple operation such as reading from the command line, converting images from one format to another or a network task that does not require a gui.  One thing that is easy to forget when it comes to this is although you may not need to call QCoreApplication::exec() or QApplication::exec() then it is still relevant to actually create a QCoreApplication/QApplication object for your application to work.


The reason for this is that the application object actually enables plugins to be used in your application, the obvious ones are the imageformat plugins and the sql plugins, but what can be forgotten is that some textcodecs are also implemented as plugins.  Therefore if you are trying to convert from a locale encoding (such as Japanese) then you need to have the textcodec plugins as well.  So you need to create an application object to ensure that these plugins are loaded.  You don't need to call exec() on it, as it is sufficient to just have the object existing.  This will mean that it will be able to convert to and from the different locales easily, load SQL databases and some image formats without any problem.


Moving on to a related topic which is subclassing QCoreApplication and QApplication, this is done a lot but what I have noticed a fair bit in the past is a subtle problem which gets missed sometimes and can cause memory corruption.  The problem is with the constructor itself. What I have seen goes something like this:


MyApplication::MyApplication(int argc, char **argv) : QApplication(argc, argv)


At first glance, this looks fine. It will build and the application will run and you may not actually hit a problem.  However, there is a subtle thing missing and that is the missing reference for the argc variable.  QApplication actually takes an int reference because it will modify the original int variable taken from the main() call.  So it should look like:


MyApplication::MyApplication(int &argc, char **argv) : QApplication(argc, argv)


This resolves the memory corruption caused by it.  So, this is certainly something to watch out for when subclassing either QCoreApplication or QApplication.


As you probably have noticed now, Qt Commercial 4.8.0 is now released and you can find this in your download area in the customer portal. I hope that you are all able to use it and find that it is working to your needs.  As a reminder, if you do find any problems with it, then please submit them via the customer portal.  The support team will be available through the holiday period with the exception of the 26th December (which is the only public holiday that does not fall on a weekend this year). So, we will be able to handle your queries.


From all of us in the Qt Commercial support team I wish you all happy holidays and I hope that 2012 brings you more and more productive results from using Qt Commercial!

Blog Topics: