[Development] Converting types in Qt

Ziller Eike Eike.Ziller at digia.com
Wed Jul 16 17:01:53 CEST 2014


On Jul 16, 2014, at 1:30 PM, Jędrzej Nowacki <jedrzej.nowacki at digia.com> wrote:

> On Wednesday 16 of July 2014 06:37:25 Ziller Eike wrote:
>> I don’t think we have a single place in Qt Creator where we want automatic
>> conversions when using QVariant. A search for QVariant(Map) returns 5400
>> hits. In the map case, we usually expect the one retrieving the value for a
>> key to know the exact type of what was thrown in (that’s usually the same
>> class, or related classes), and then we use item models and QSettings which
>> we use in the same way.
> 
> From performance point of view it is good to avoid any conversions. I would 
> say even more, if you know all types in your application there is no point in 
> using QVariant. Sometimes it is not possible, and sometimes one just don't 
> want be bothered. 
> 
> I made an extremely unfair experiment.  I switched off all conversions in Qt 
> and I recompiled QtCreator. To be honest I expected that it would crash at 
> startup, but no (impressive!). I was able to use menu and open dialogs, 
> nothing more. From the stderr I could deduct that a QVariant conversion was 
> used in reading xml, qws files and in animations.

True, we use QVariant::toString for an easy way to write the value of the key-value pairs of basic types in qtversions.xml, profiles.xml, .pro.user, and the sessions (.qws)
Even there we keep the type information though, and use that to explicitly convert the variants in the map back to the right type when reading the file, so type conversion should never be outside the reader/writer in that case. What the conversion saves us, is a few case statements in the reader/writer.

  <data>
  <variable>QtVersion.0</variable>
  <valuemap type="QVariantMap">
   <value type="int" key="Id">10</value>
   <value type="QString" key="Name">Qt 5.3</value>
   <value type="QString" key="QMakePath">/Users/Shared/qt/qt/5.3/64/qtbase/bin/qmake</value>
   <value type="QString" key="QtVersion.Type">Qt4ProjectManager.QtVersion.Desktop</value>
   <value type="bool" key="isAutodetected">false</value>
  </valuemap>
 </data>

No idea what happens in animations, but I’d suppose that any conversions there would be accidentally.

-- 
Eike Ziller, Senior Software Engineer - Digia, Qt
Digia Germany GmbH, Rudower Chaussee 13, D-12489 Berlin
Geschäftsführer: Mika Pälsi, Juha Varelius, Anja Wasenius
Sitz der Gesellschaft: Berlin. Registergericht: Amtsgericht Charlottenburg, HRB 144331 B 



More information about the Development mailing list