<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://www.shadoware.org/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Shadoware.Org - Mot-clé - qmake</title>
  <link>http://www.shadoware.org/</link>
  <atom:link href="http://www.shadoware.org/index.php/feed/tag/qmake/rss2" rel="self" type="application/rss+xml"/>
  <description>Moi, mes idées, mes logiciels ...</description>
  <language>fr</language>
  <pubDate>Mon, 06 Feb 2012 15:38:11 +0100</pubDate>
  <copyright>Ulrich VANDENHEKKE</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>[C++/Qt] CMake et Qt</title>
    <link>http://www.shadoware.org/post/%5BQt%5D-CMake-et-Qt</link>
    <guid isPermaLink="false">urn:md5:da05e097d1a727105b6e5727b3bb6c22</guid>
    <pubDate>Sun, 26 Jun 2011 14:41:00 +0200</pubDate>
    <dc:creator>Ulrich Van Den Hekke</dc:creator>
        <category>C++</category>
        <category>cmake</category><category>planet</category><category>qmake</category><category>qt</category>    
    <description>&lt;p&gt;Suite à un &lt;a href=&quot;http://www.shadoware.org/post/2008/01/21/42-qabstractitemmodel-qt&quot;&gt;billet&lt;/a&gt; datant de 2008, je reviens vers vous pour ajouter quelques précisions sur la compilation de programme &lt;a href=&quot;http://qt.nokia.com/&quot;&gt;Qt&lt;/a&gt; avec &lt;a href=&quot;http://www.cmake.org/&quot;&gt;CMake&lt;/a&gt;. En effet, pour mon programme &lt;a href=&quot;http://xinx.shadoware.org?piwik_campaign=Shadoware&amp;amp;piwik_keyword=Billet CMake et Qt&quot;&gt;XINX&lt;/a&gt;, j'ai modifié la chaîne de compilation actuelle utilisant &lt;em&gt;QMake&lt;/em&gt; par une chaîne de compilation &lt;em&gt;CMake&lt;/em&gt;.&lt;/p&gt;


&lt;p&gt;&lt;em&gt;CMake&lt;/em&gt; est un puissant générateur de &lt;code&gt;Makefile&lt;/code&gt;, il permet de remplacer les anciens (mais pas complètement révolus) &lt;a href=&quot;http://www.gnu.org/software/autoconf/&quot;&gt;autotools&lt;/a&gt;. &lt;em&gt;CMake&lt;/em&gt; ne remplace donc pas le programme &lt;code&gt;make&lt;/code&gt; mais vient se placer en amont.&lt;/p&gt;


&lt;p&gt;&lt;em&gt;CMake&lt;/em&gt; permet de compiler un programme à  différents endroits du dossier des sources, ce qui permet de garder le répertoire des sources &lt;em&gt;propre&lt;/em&gt;.&lt;/p&gt;


&lt;p&gt;Nous allons considérer dans la suite le dossier &lt;code&gt;projet&lt;/code&gt; suivant&amp;nbsp;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;projet&lt;/code&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;source&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;build&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;    &lt;h3&gt;Compilation&lt;/h3&gt;


&lt;p&gt;Nous passerons sous silence la compilation d'un programme non &lt;em&gt;Qt&lt;/em&gt; qui peut être retrouvé dans la &lt;a href=&quot;http://www.cmake.org/cmake/help/cmake_tutorial.html&quot;&gt;documentation&lt;/a&gt; et nous nous limiterons aux explications liées aux programmes écrits en ''Qt' (qui peuvent également être retrouvés dans d'autres tutoriels sur Internet).&lt;/p&gt;


&lt;h4&gt;Package à utiliser&lt;/h4&gt;


&lt;p&gt;Pour utiliser &lt;em&gt;Qt4&lt;/em&gt; avec &lt;em&gt;CMake&lt;/em&gt;, il faut inclure le package &lt;code&gt;Qt4&lt;/code&gt;&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:project&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;project&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;lenomdemonprojet&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt; 
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:cmake_minimum_required&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;cmake_minimum_required&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;VERSION&lt;/span&gt; 2.8.0&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:find_package&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;find_package&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;Qt4 REQUIRED&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h4&gt;Définir les modules Qt à utiliser&amp;nbsp;:&lt;/h4&gt;


&lt;p&gt;Il est ensuite possible d'activer ou de désactiver les différents modules de &lt;em&gt;Qt&lt;/em&gt; à utiliser suivant le programme que vous écrivez&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_DONT_USE_QTCORE &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_DONT_USE_QTGUI &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QT3SUPPORT &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTASSISTANT &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QAXCONTAINER &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QAXSERVER &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTDESIGNER &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTMOTIF &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTMAIN &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTNETWORK &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTNSPLUGIN &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTOPENGL &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTSQL &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTXML &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTXMLPATTERNS &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTWEBKIT &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTSVG &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTTEST &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTUITOOLS &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTDBUS &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTSCRIPT &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTASSISTANTCLIENT &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTHELP &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_PHONON &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Par exemple pour faire un programme utilisant le module Xml, XmlPatterns, et Webkit il faut ajouter&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTXML &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTXMLPATTERNS &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;QT_USE_QTWEBKIT &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;TRUE&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h4&gt;Quelques déclarations supplémentaires&lt;/h4&gt;


&lt;p&gt;Après avoir compilé les différentes parties du programme avec le générateur de &lt;code&gt;Makefile&lt;/code&gt; de &lt;em&gt;Qt&lt;/em&gt; (QMake), j'ai relevé les différentes définitions à ajouter lors de la compilation. De plus une fois les modules définis, il faut inclure également le module &lt;em&gt;UseQt4&lt;/em&gt;.&lt;/p&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_definitions&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_definitions&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;-DUNICODE&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# Les Q_ASSERT, Q_ASSERT_X, ... sont désactivés en mode Release&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:if&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;if&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;CMAKE_BUILD_TYPE &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;STREQUAL&lt;/span&gt; &lt;span style=&quot;color: #912f11;&quot;&gt;&amp;quot;Release&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
	&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_definitions&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_definitions&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;-DQT_NO_DEBUG&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:endif&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;endif&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;()&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# Include utilisé par Qt (du style include (UseQt4))&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:include&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;include&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #b08000;&quot;&gt;${QT_USE_FILE}&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #666666; font-style: italic;&quot;&gt;# Ajoute les définitions propres à Qt (suivant les modules ajoutés)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_definitions&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_definitions&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #b08000;&quot;&gt;${QT_DEFINITIONS}&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h4&gt;Macros pour générer les fichiers &lt;code&gt;moc&lt;/code&gt;.&lt;/h4&gt;


&lt;h5&gt;Macro par défaut&lt;/h5&gt;


&lt;p&gt;La facilité de programmation en &lt;em&gt;Qt&lt;/em&gt; est due à l'ajout par le framework de la notion de méta-objet. Cette notion se fait à l'aide d'un générateur propre à ce framework&amp;nbsp;: &lt;code&gt;moc&lt;/code&gt;&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/%5BQt%5D-CMake-et-Qt#pnote-636-1&quot; id=&quot;rev-pnote-636-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;


&lt;p&gt;Pour générer les méta-objets, il faut définir sur les objets descendants de &lt;code&gt;QObject&lt;/code&gt;, une macro &lt;code&gt;Q_OBJECT&lt;/code&gt;. Cette macro ajoute, côté interface, la déclaration de méthodes supplémentaires pour la gestion des méta-informations, et coté implémentation, ajoute l'implémentation de ces méta-informations ainsi que la déclaration des signaux et de slots de l'objet&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/%5BQt%5D-CMake-et-Qt#pnote-636-2&quot; id=&quot;rev-pnote-636-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;


&lt;p&gt;Pour générer les fichiers d'implémentation générés par le compilateur &lt;code&gt;moc&lt;/code&gt;, il est possible d'utiliser la macro suivante pour une liste de fichier sources (extension .cpp, ou .cxx, ...)&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;qt4_wrap_cpp&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;outfiles inputfile ... OPTIONS ...&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Cette macro générera un fichier de type &lt;code&gt;moc_nomfichier.cxx&lt;/code&gt; qu'il faudra inclure dans le ficher CPP (car il n'est pas inclus automatiquement dans la liste des fichiers à compiler.&lt;/p&gt;
&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #339900;&quot;&gt;#include &amp;quot;moc_nomfichier.cxx&amp;quot;&lt;/span&gt;&lt;/pre&gt;


&lt;h5&gt;Une autre macro&lt;/h5&gt;


&lt;p&gt;Autre possibilité, faire comme avec les fichiers &lt;code&gt;.pro&lt;/code&gt; de &lt;em&gt;QMake&lt;/em&gt; et générer un fichier moc pour chaque fichier &lt;code&gt;.h&lt;/code&gt; possédant une macro &lt;code&gt;Q_OBJECT&lt;/code&gt;. Pour cela j'ai écrit la petite macro suivante&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:macro&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;macro&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;xinx_automoc outfiles&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
  qt4_get_moc_flags&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;moc_flags&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
  qt4_extract_options&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;moc_files moc_options &lt;span style=&quot;color: #b08000;&quot;&gt;${ARGN}&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&amp;nbsp;
  &lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:foreach&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;foreach&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;it &lt;span style=&quot;color: #b08000;&quot;&gt;${moc_files}&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
    &lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:get_filename_component&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;get_filename_component&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;it &lt;span style=&quot;color: #b08000;&quot;&gt;${it}&lt;/span&gt; ABSOLUTE&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&amp;nbsp;
    &lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:if&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;if&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;EXISTS&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${it}&lt;/span&gt; &lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
		&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:file&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;file&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;READ &lt;span style=&quot;color: #b08000;&quot;&gt;${it}&lt;/span&gt; _contents&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&amp;nbsp;
		&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:string&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;string&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;REGEX&lt;/span&gt; &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;MATCHALL&lt;/span&gt; &lt;span style=&quot;color: #912f11;&quot;&gt;&amp;quot;Q_OBJECT&amp;quot;&lt;/span&gt; _match &lt;span style=&quot;color: #912f11;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #b08000;&quot;&gt;${_contents}&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
		&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:if&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;if&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;_match&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
			qt4_make_output_file&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #b08000;&quot;&gt;${it}&lt;/span&gt; moc_ cxx outfile&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
			qt4_create_moc_command&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #b08000;&quot;&gt;${it}&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${outfile}&lt;/span&gt; &lt;span style=&quot;color: #912f11;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #b08000;&quot;&gt;${moc_flags}&lt;/span&gt;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #912f11;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #b08000;&quot;&gt;${moc_options}&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
			macro_add_file_dependencies&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #b08000;&quot;&gt;${it}&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${outfile}&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
			&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #b08000;&quot;&gt;${outfiles}&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${${outfiles}&lt;/span&gt;} &lt;span style=&quot;color: #b08000;&quot;&gt;${outfile}&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
		&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:endif&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;endif&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;_match&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
    &lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:endif&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;endif&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt; &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;EXISTS&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${it}&lt;/span&gt; &lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
  &lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:endforeach&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;endforeach&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;it&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:endmacro&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;endmacro&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;xinx_automoc&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Elle s'utilise ainsi&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;xinx_automoc&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;moc_headers &lt;span style=&quot;color: #b08000;&quot;&gt;${headers}&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h4&gt;Macro pour générer les fichiers en tête d'interface graphique.&lt;/h4&gt;


&lt;p&gt;Les interfaces graphiques sont développées à l'aide de &lt;em&gt;Qt-Designer&lt;/em&gt;. Ces fichiers sont au format XML.&lt;/p&gt;


&lt;p&gt;La macro CMake suivante permet de transformer ces fichiers XML en fichier d'entête C.&lt;/p&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;qt4_wrap_ui&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;outfiles inputfile ...&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h4&gt;Macro pour compiler les fichiers de resources&lt;/h4&gt;


&lt;p&gt;La macro CMake suivante permet de compiler le fichier de ressource (extension *.qrc) en fichier &lt;code&gt;cpp&lt;/code&gt; qui sera ensuite compilé avec le reste du programme&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;qt4_add_resources&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;outfiles inputfile ... OPTIONS ...&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h4&gt;Macro pour compiler les fichiers de traduction&lt;/h4&gt;


&lt;p&gt;La macro CMake suivante est utilisée pour compiler un fichier de traduction &lt;code&gt;ts&lt;/code&gt; en fichier de traduction compilé &lt;code&gt;qm&lt;/code&gt;. Ce dernier est ensuite à inclure dans le programme (soit par un fichier de ressource, soit dans un dossier à coté).&lt;/p&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;qt4_add_translation&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;qm_files ts_files ...&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h4&gt;Compilation d'un plugin&lt;/h4&gt;


&lt;p&gt;En plus des options décrites ci-dessus, pour compiler un plugin, il faut ajouter quelques définitions supplémentaires.&lt;/p&gt;


&lt;p&gt;Il faut bien faire attention à ce que le plugin soit compilé en mode &lt;code&gt;Release&lt;/code&gt; si le programme l'est aussi (Ce qui n'est pas forcément pratique pour faire du debug).&lt;/p&gt;


&lt;h5&gt;Plugin dynamique&lt;/h5&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_definitions&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_definitions&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;-DQT_PLUGIN&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_definitions&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_definitions&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;-DQT_SHARED&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_library&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_library&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;webplugin &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;SHARED&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${webplugin_SRCS}&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${webplugin_MOC_SRCS}&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h5&gt;Plugin Static&lt;/h5&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_definitions&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_definitions&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;-DQT_PLUGIN&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_definitions&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_definitions&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;-DQT_STATICPLUGIN&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:if&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;if&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;CMAKE_SIZEOF_VOID_P &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;MATCHES&lt;/span&gt; 8&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
	&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_definitions&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_definitions&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;-fPIC&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:endif&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;endif&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;()&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_library&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_library&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;webplugin &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;STATIC&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${webplugin_SRCS}&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${webplugin_MOC_SRCS}&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h4&gt;Ajout d'un fichier RC pour windows&lt;/h4&gt;


&lt;p&gt;Les fichiers RC Windows contiennent des informations, comme par exemple, la version et le nom du programme.&lt;/p&gt;


&lt;p&gt;Ces informations peuvent être utilisées par les programmes d'installation sous Windows ou aussi par Windows lui-même.&lt;/p&gt;


&lt;p&gt;Afin de pouvoir ajouter un fichier RC file au programme, on peut ajouter ceci&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:if&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;if&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;MINGW&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
	&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;CMAKE_RC_COMPILER_INIT windres&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
	&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:enable_language&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;enable_language&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;RC&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
	&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:set&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;set&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;CMAKE_RC_COMPILE_OBJECT &lt;span style=&quot;color: #912f11;&quot;&gt;&amp;quot;&amp;lt;CMAKE_RC_COMPILER&amp;gt; &amp;lt;FLAGS&amp;gt; &amp;lt;DEFINES&amp;gt; -O coff -o &amp;lt;OBJECT&amp;gt; &amp;lt;SOURCE&amp;gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:endif&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;endif&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;MINGW&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Ensuite le fichier RC se compile comme tout autre fichier (&lt;code&gt;C++&lt;/code&gt;, ...)&lt;/p&gt;



&lt;h4&gt;Compilation d'une librairie static&lt;/h4&gt;

&lt;pre class=&quot;cmake cmake&quot; style=&quot;font-family:inherit&quot;&gt;&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_definitions&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_definitions&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;-DQT_SHARED&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;
&lt;a href=&quot;http://www.cmake.org/cmake/help/cmake2.6docs.html#command:add_library&quot;&gt;&lt;span style=&quot;color: #1f3f81; font-style: bold;&quot;&gt;add_library&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;(&lt;/span&gt;xinxplugins &lt;span style=&quot;color: #077807; font-sytle: italic;&quot;&gt;STATIC&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${xinxplugins_SRCS}&lt;/span&gt; &lt;span style=&quot;color: #b08000;&quot;&gt;${xinxplugins_MOC_SRCS}&lt;/span&gt;&lt;span style=&quot;color: #197d8b;&quot;&gt;)&lt;/span&gt;&lt;/pre&gt;


&lt;h3&gt;Utiliser le programme CMake&lt;/h3&gt;


&lt;p&gt;Une fois le programme configuré, il est possible de compiler ce dernier à l'aide des commandes suivantes&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;bash bash&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;mkdir&lt;/span&gt; build
&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;cd&lt;/span&gt; build
&amp;nbsp;
cmake ..&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;sources
&amp;nbsp;
&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;make&lt;/span&gt;
&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;make&lt;/span&gt; &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;install&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Sous Windows, il est possible de compiler en utilisant &lt;a href=&quot;http://www.mingw.org/&quot;&gt;MinGW&lt;/a&gt; pour compiler ou d'utiliser le compilateur de &lt;code&gt;Visual Studio&lt;/code&gt;.&lt;/p&gt;


&lt;p&gt;Il est également possible de définir un emplacement pour l'installation différent du dossier par défaut&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;bash bash&quot; style=&quot;font-family:inherit&quot;&gt;cmake -DCMAKE_INSTALL_PREFIX=&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;home&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;login&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;/pre&gt;
&lt;div class=&quot;footnotes&quot;&gt;&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/%5BQt%5D-CMake-et-Qt#rev-pnote-636-1&quot; id=&quot;pnote-636-1&quot;&gt;1&lt;/a&gt;] C'est ce qui fait tout l'avantage de &lt;em&gt;Qt&lt;/em&gt; mais est aussi son inconvénient car il ajoute une couche supplémentaire. C'est ensuite une question de goût.&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/%5BQt%5D-CMake-et-Qt#rev-pnote-636-2&quot; id=&quot;pnote-636-2&quot;&gt;2&lt;/a&gt;] facilitant la gestion des évènements dans &lt;em&gt;Qt&lt;/em&gt;&lt;/p&gt;&lt;/div&gt;
</description>
    
    
    
          <comments>http://www.shadoware.org/post/%5BQt%5D-CMake-et-Qt#comment-form</comments>
      <wfw:comment>http://www.shadoware.org/post/%5BQt%5D-CMake-et-Qt#comment-form</wfw:comment>
      <wfw:commentRss>http://www.shadoware.org/feed/atom/comments/636</wfw:commentRss>
      </item>
    
  <item>
    <title>[C++/Qt] Performance de l'utilisation de QSharedPointer</title>
    <link>http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer</link>
    <guid isPermaLink="false">urn:md5:f477dcc0189d180d38c2a61076493790</guid>
    <pubDate>Tue, 25 Jan 2011 12:00:00 +0100</pubDate>
    <dc:creator>Ulrich Van Den Hekke</dc:creator>
        <category>Performances</category>
        <category>kde</category><category>nokia</category><category>performance</category><category>planet</category><category>qmake</category><category>qt</category><category>trolltech</category>    
    <description>&lt;h3&gt;Présentation&lt;/h3&gt;


&lt;p&gt;&lt;em&gt;Qt&lt;/em&gt; est un framework orienté objet écrit en C++ et permettant de faire des interfaces graphiques. Ce framework est utilisé par le projet &lt;em&gt;KDE&lt;/em&gt; depuis ses débuts pour en faire un environnement de bureau très complet.&lt;/p&gt;


&lt;p&gt;&lt;em&gt;Qt&lt;/em&gt; fournit un ensemble de pointeur &lt;em&gt;intelligent&lt;/em&gt;&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-1&quot; id=&quot;rev-pnote-628-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt; permettant de gérer plus facilement la mémoire. Le but est alors de ne plus avoir à supprimer des objets. La suppression se fera soit par un pointeur &lt;em&gt;intelligent&lt;/em&gt; soit par le système de hiérarchie d'objet existant en &lt;em&gt;Qt&lt;/em&gt; (l'objet père qui supprime l'ensemble des objets fils qui lui sont rattachés).&lt;/p&gt;


&lt;p&gt;&lt;em&gt;Qt&lt;/em&gt; propose l'ensemble des pointeurs &lt;em&gt;intelligents&lt;/em&gt; suivants:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://doc.qt.nokia.com/4.6/qshareddatapointer.html&quot; hreflang=&quot;en&quot; title=&quot;Documentation Qt de QSharedDataPointer&quot;&gt;QSharedDataPointer&lt;/a&gt; / &lt;a href=&quot;http://doc.qt.nokia.com/4.6/qshareddata.html&quot; hreflang=&quot;en&quot; title=&quot;Documentation Qt de QSharedData&quot;&gt;QSharedData&lt;/a&gt;&amp;nbsp;: ces deux classes utilisées ensemble permettent d'écrire un objet avec partage implicite. Cela signifie que l'objet fonctionnera comme la classe &lt;em&gt;QString&lt;/em&gt;. Tant que l'objet est copié, passé en paramètre, .... l'objet n'est pas dupliqué (tous les objets pointes vers le même espace mémoire). Au moment où l'objet est modifié, l'objet est dupliqué. C'est ce qu'on appelle le COW&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-2&quot; id=&quot;rev-pnote-628-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://doc.qt.nokia.com/4.6/qexplicitlyshareddatapointer.html&quot; hreflang=&quot;en&quot; title=&quot;Documentation Qt de QExplictlySharedDataPointer&quot;&gt;QExplictlySharedDataPointer&lt;/a&gt; / &lt;a href=&quot;http://doc.qt.nokia.com/4.6/qshareddata.html&quot; hreflang=&quot;en&quot; title=&quot;Documentation Qt de QSharedData&quot;&gt;QSharedData&lt;/a&gt;&amp;nbsp;: QExplicitlySharedDataPointer est une variante de QSharedDataPointer. Ce pointeur &lt;em&gt;intelligent&lt;/em&gt;, comme son nom l'indique, est détaché uniquement lorsque la méthode detach() est appelée explicitement. Cette classe permet de faire des objets qui fonctionnent comme des pointeurs mais qui sont utilisés sans la notion de pointeur (le *). La suppression des données partagées se fait donc quand tous les objets ne sont plus utilisés.&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://doc.qt.nokia.com/4.6/qscopedpointer.html&quot; hreflang=&quot;en&quot; title=&quot;Documentation Qt de QScopedPointer&quot;&gt;QScopedPointer&lt;/a&gt;&amp;nbsp;: Ce pointeur est le plus simple. Il permet de déclarer un pointeur sur le tas et s'occupe de la destruction de l'objet, lorsque le programme sort de la portée du bloc. Cela permet de ne plus se soucier de la libération du pointeur dans les cas d'erreur (exception, retour avant la fin de la fonction car le fichier n'a pu être ouvert, ...).&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://doc.qt.nokia.com/4.6/qsharedpointer.html&quot; hreflang=&quot;en&quot; title=&quot;Documentation Qt de QSharedPointer&quot;&gt;QSharedPointer&lt;/a&gt;&amp;nbsp;: Le pointeur dont on parlera dans la suite de ce billet. Il permet de partager non plus des données (comme le fait QSharedData) mais de partager un pointeur&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-3&quot; id=&quot;rev-pnote-628-3&quot;&gt;3&lt;/a&gt;]&lt;/sup&gt;. Nous allons voir dans la suite du billet, comment simplement utiliser ce pointeur, et les performances de ce pointeur par rapport à un pointeur standard.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Sommaire&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#utilisation_qsharedpointer&quot;&gt;Utilisation de QSharedPointer&lt;/a&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#a_quoi_sert_il&quot;&gt;A quoi sert-il ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#comment_l_utiliser&quot;&gt;Comment l'utiliser ?&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#comment_utiliser_this&quot;&gt;Comment utiliser this&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#comment_eviter_les_references_circulaires&quot;&gt;Comment éviter les références circulaires&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#utilisation_dans_les_applications_multi_thread&quot;&gt;Utilisation dans les applications multi-thread.&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#utilisation_d_un_pool&quot;&gt;Utilisation d'un pool&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#benchmark&quot;&gt;Benchmark&lt;/a&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#code_source&quot;&gt;Code source&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#le_jeu_de_test&quot;&gt;Le jeu de test&lt;/a&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#test_de_l_allocation&quot;&gt;Test de l'allocation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#test_de_modification_d_une_donnee&quot;&gt;Test de Modification d'une donnée&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#test_d_affectation&quot;&gt;Test d'affectation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#test_de_destruction&quot;&gt;Test de destruction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#resultat_du_test&quot;&gt;Résultat du test&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;/li&gt;
&lt;/ol&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;footnotes&quot;&gt;&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-1&quot; id=&quot;pnote-628-1&quot;&gt;1&lt;/a&gt;] en anglais : smart-pointer&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-2&quot; id=&quot;pnote-628-2&quot;&gt;2&lt;/a&gt;] COW = Copy On Write&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-3&quot; id=&quot;pnote-628-3&quot;&gt;3&lt;/a&gt;] Ce pointeur est l'équivalent du pointeur &lt;em&gt;intelligent&lt;/em&gt; &lt;a href=&quot;http://www.boost.org/doc/libs/1_45_0/libs/smart_ptr/shared_ptr.htm&quot; hreflang=&quot;en&quot;&gt;boost::shared_ptr&lt;/a&gt; du projet &lt;a href=&quot;http://www.boost.org/&quot; hreflang=&quot;en&quot;&gt;Boost&lt;/a&gt;. Boost est une librairie qui ajoute beaucoup de facilité pour les programmes en C++, comme par exemple les smart-pointer dont certains seront inclus dans C++0x, ou de la boucle &lt;code&gt;std::for_each&lt;/code&gt;.&lt;/p&gt;&lt;/div&gt;
    &lt;p&gt;&lt;a name=&quot;utilisation_qsharedpointer&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Utilisation de QSharedPointer&lt;/h3&gt;


&lt;p&gt;&lt;a name=&quot;a_quoi_sert_il&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;A quoi sert-il&amp;nbsp;?&lt;/h4&gt;


&lt;p&gt;L'objet &lt;code&gt;QSharedPointer&lt;/code&gt; fait partie des pointeurs &lt;em&gt;intelligents&lt;/em&gt;. Ces pointeurs permettent de gérer automatiquement la libération de la mémoire (plus besoin de faire &lt;code&gt;delete ptr;&lt;/code&gt; quand le pointeur n'est plus utilisé) tout en restant utilisable comme un pointeur normal.&lt;/p&gt;


&lt;p&gt;&lt;code&gt;QSharedPointer&lt;/code&gt; fonctionne par comptage de référence. Après la déclaration, à chaque affectation, on augmente le compteur de référence, lorsqu'on quitte la portée du bloc, on décrémente le compteur de référence. &lt;code&gt;QSharedPointer&lt;/code&gt; détruit donc automatiquement le pointeur quand il n'existe plus aucune référence vers ce pointeur. &lt;code&gt;QSharedPointer&lt;/code&gt; vient donc comme une encapsulation de notre pointeur.&lt;/p&gt;


&lt;p&gt;&lt;img src=&quot;http://www.shadoware.org/public/billets/qsharedpointer/QSharedPointer1.png&quot; alt=&quot;QSharedPointer1.png&quot; style=&quot;display:block; margin:0 auto;&quot; title=&quot;QSharedPointer1.png, janv. 2011&quot; /&gt;&lt;/p&gt;


&lt;p&gt;&lt;a name=&quot;comment_l_utiliser&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Comment l'utiliser&amp;nbsp;?&lt;/h4&gt;


&lt;p&gt;La déclaration d'un pointeur en C, se fait en écrivant &lt;code&gt;MyObject*&lt;/code&gt;. La syntaxe en utilisant un QSharedPointer se fait en écrivant &lt;code&gt;QSharedPointer&amp;lt;MyObject&amp;gt;&lt;/code&gt;. Par la suite dans le programme, l'utilisation du pointeur &lt;code&gt;QSharedObject&lt;/code&gt; se fera de la même manière qu'un pointeur C. (Avec l'opérateur &lt;code&gt;-&amp;gt;&lt;/code&gt; pour appeler un membre, une méthode, ...) . Appelons dans la suite pointeur C, les pointeurs standards et &lt;code&gt;QSharedPointer&lt;/code&gt;, le pointeur intelligent.&lt;/p&gt;


&lt;p&gt;Afin d'éviter d'avoir un pointeur normal pouvant être supprimé à tout moment dans l'application, lors de l'utilisation de &lt;code&gt;QSharedPointer&lt;/code&gt;, il ne faut utiliser le pointeur C résultant du &lt;code&gt;new&lt;/code&gt; que pour la création du &lt;code&gt;QSharedPointer&lt;/code&gt;. On peut donc directement créer le &lt;code&gt;QSharedPointer&lt;/code&gt; en utilisant le constructeur &lt;code&gt;QSharedPointer ( T * ptr )&lt;/code&gt; qui prend en paramètre le pointeur C. C'est entre ces parenthèses que nous allons créer le pointeur C&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-1&quot; id=&quot;rev-pnote-628-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span style=&quot;color: #666666;&quot;&gt;// Création du pointeur intelligent à partir d'un pointeur normal.&lt;/span&gt;
  QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; MyObject&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #666666;&quot;&gt;// Utilisation du pointeur intelligent comme un pointeur normal.&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    ptr&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;setMembre&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;maValeur&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #666666;&quot;&gt;// Appel d'une méthode utilisant ce pointeur&lt;/span&gt;
  maMethode&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Lorsque l'on quitte le bloc, si le comptage de référence tombe à 0, on supprime le pointeur. A l'intérieur de &lt;code&gt;maMethode()&lt;/code&gt; le nombre de référence sera passé à 2. Si la méthode utilise le pointeur mais ne l'assigne nul part, le nombre de référence devrait être retombé à 1 et donc ici sera décrémenté à 0.&lt;/p&gt;


&lt;p&gt;Si par contre, &lt;code&gt;maMethode()&lt;/code&gt; fait des opérations d'assignation de &lt;code&gt;ptr&lt;/code&gt; et conserve une copie, le comptage ne tombera pas à 0 tant que l'objet restera utilisé (assigné) ailleurs.&lt;/p&gt;


&lt;p&gt;Regardons un exemple de &lt;code&gt;maMethode()&lt;/code&gt;&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; maMethode&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
  ptr&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;setMembre2&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;maValeur&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Au début du bloc, ici le comptage de référence est à 2 et sera décrémenté à la sortie de la méthode. On peut modifier les membres de &lt;code&gt;ptr&lt;/code&gt;, et dans ce cas pas de changement du comptage de référence.&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;this&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;monPtr &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; ptr&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Au contraire, on peut également l'assigner à un autre objet. Dans ce cas le comptage de référence de cet objet passera à 3. A la sortie de la méthode il sera décrémenté et passera alors à 2. L'objet ne sera pas supprimé tant qu'on ne fera pas un &lt;code&gt;this-&amp;gt;monPtr.clear()&lt;/code&gt; ou que &lt;code&gt;this&lt;/code&gt; ne sera pas détruit.&lt;/p&gt;


&lt;p&gt;Si on veut garder une référence d'un pointeur mais qu'on ne souhaite pas que celle-ci incrémente le nombre de référence du &lt;code&gt;QSharedPointer&lt;/code&gt;, il est possible de créer un pointeur &lt;em&gt;faible&lt;/em&gt;. Ce pointeur passe par l'objet &lt;code&gt;QWeakPointer&lt;/code&gt;. Pour obtenir ce type de pointeur, il suffit de faire&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;QWeakPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptrW &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; ptr&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;toWeakRef &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;&lt;code&gt;ptrW&lt;/code&gt; n'incrémente donc pas le comptage de référence, cela signifie donc que le pointeur peut être détruit même si un objet &lt;code&gt;QWeakPointer&lt;/code&gt; existe. Il sera alors possible de faire un &lt;code&gt;ptrW.isNull()&lt;/code&gt; pour savoir si le pointeur est toujours valide. Si l'utilisateur a également besoin d'avoir accès à un membre de l'objet, il pourra le transformer en &lt;code&gt;QSharedPointer&lt;/code&gt; avant de l'utiliser sauf si le pointeur est &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr2 &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; ptrW&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;toStrongRef &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt; 
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
   ptr&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;maMethodePtr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Il faut tester que &lt;code&gt;ptr2&lt;/code&gt; est encore valide, car tant que la transformation du pointeur &lt;em&gt;faible&lt;/em&gt; vers le &lt;code&gt;QSharedPointer&lt;/code&gt; n'a pas encore été fait, il est possible que le nombre de référence vers l'objet soit tombé à 0 et qu'il ait été supprimé.&lt;/p&gt;


&lt;p&gt;&lt;a name=&quot;comment_utiliser_this&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Comment utiliser &lt;code&gt;this&lt;/code&gt;&lt;/h4&gt;


&lt;p&gt;Un des points peu pratique de l'utilisation de &lt;code&gt;QSharedPointer&lt;/code&gt; est que le comptage de référence ne fonctionne pas si plusieurs &lt;code&gt;QSharedPointer&lt;/code&gt; pointent vers le même objet mais ont tous été créés à partir du pointeur C. Prenons par exemple, le cas suivant&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;MyObject &lt;span style=&quot;color: #000040;&quot;&gt;*&lt;/span&gt; ptr &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; MyObject&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr1 &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; QSharedPointer&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr2 &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; QSharedPointer&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Le problème d'écrire ces lignes ainsi, et que pour  &lt;code&gt;ptr1&lt;/code&gt; comme pour &lt;code&gt;ptr2&lt;/code&gt;, l'objet n'est référencé qu'une fois. Ainsi le premier qui tombera à 0 détruira l'objet, alors que l'autre pourrait encore l'utiliser. Il faut donc écrire les choses comme suite&amp;nbsp;:&lt;/p&gt;


&lt;p&gt;&lt;img src=&quot;http://www.shadoware.org/public/billets/qsharedpointer/QSharedPointer2.png&quot; alt=&quot;QSharedPointer2.png&quot; style=&quot;display:block; margin:0 auto;&quot; title=&quot;QSharedPointer2.png, janv. 2011&quot; /&gt;&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr1 &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; QSharedPointer&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; MyObject&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr2 &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; ptr1&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Ainsi &lt;code&gt;ptr1&lt;/code&gt; et &lt;code&gt;ptr2&lt;/code&gt; ont bien chacun connaissance de l'existence de l'autre. Cela contraint donc à remplacer toutes les déclarations du type &lt;code&gt;MyObject*&lt;/code&gt; par &lt;code&gt;QSharedPointer&amp;lt;MyObject&amp;gt;&lt;/code&gt;. Ceci est donc à faire dans les paramètres des méthodes, dans les membres, dans la déclaration des variables locales, ... . On ne peut donc plus utiliser le pointeur C &lt;code&gt;MyObject*&lt;/code&gt; directement, mais seulement au travers de &lt;code&gt;QSharedPointer&lt;/code&gt; ou de &lt;code&gt;QWeakPointer&lt;/code&gt;.&lt;/p&gt;


&lt;p&gt;Cela commence à poser problème lors de l'utilisation de &lt;code&gt;this&lt;/code&gt; dans un objet. Imaginons une méthode d'un objet mettant à jour des membres fils avec en paramètre le père. Nous aurions alors tendance à écrire ceci&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; Object2&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;setParent&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; parent&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
...
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
....
&amp;nbsp;
&lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; MyObject&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;setMember&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;Object2 &lt;span style=&quot;color: #000040;&quot;&gt;*&lt;/span&gt; obj&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
  _membre &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; obj&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    obj&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;setParent&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Ceci ne marchera pas car on créerait un nouvel objet &lt;code&gt;QSharedPointer&lt;/code&gt; commençant son comptage de référence à 1, alors que nous en avons déjà au moins un autre pointant vers notre instance&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-2&quot; id=&quot;rev-pnote-628-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt;. &lt;code&gt;MyObject&lt;/code&gt; pourrait alors être détruit alors qu'il est encore utilisé par &lt;code&gt;Object2&lt;/code&gt;.&lt;/p&gt;


&lt;p&gt;Pour éviter cela, il faut alors passer par un pointeur &lt;em&gt;intelligent&lt;/em&gt; &lt;code&gt;this&lt;/code&gt;. Pour cela nous allons utiliser deux choses&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Un membre nommé &lt;code&gt;_this&lt;/code&gt; de type pointeur &lt;em&gt;intelligent&lt;/em&gt; &lt;code&gt;QWeakPointer&lt;/code&gt;, contenant une référence à l'objet lui même. (Nous n'utilisons pas un QSharedPointer, pour éviter une référence circulaire, voir le paragraphe suivant).&lt;/li&gt;
&lt;li&gt;Une méthode statique utilisée pour la création (nous n'allons plus utiliser le constructeur, car à ce moment, il n'existe pas encore de QSharedPointer pointant vers notre objet).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Voici un exemple de comment écrire le constructeur maison&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; MyObject
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;:&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; create&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;QString parametre&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; MyObject&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;parametre&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
    ptr&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;_this &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; ptr.&lt;span style=&quot;color: #007788;&quot;&gt;toWeakRef&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; ptr&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;:&lt;/span&gt;
  MyObject&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;QString parametre&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    ...
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  QWeakPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; _this&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Le constructeur devient alors privé (ou protégé si on a besoin de la notion d'héritage) afin d'obliger l'utilisateur de la classe à utiliser notre méthode de création. Dans notre nouvelle méthode de création &lt;code&gt;create&lt;/code&gt;, qui est une méthode statique, nous allons créer le pointeur et initialiser le &lt;code&gt;QWeakPointer&lt;/code&gt; de notre objet avec le pointeur &lt;em&gt;intelligent&lt;/em&gt; que nous venons de créer. Nous retournons un &lt;code&gt;QSharedPointer&lt;/code&gt;. La méthode &lt;code&gt;create&lt;/code&gt; devient alors notre nouveau constructeur, mais créant des instances d'objets de type &lt;code&gt;QSharedPointer&amp;lt;MyObject&amp;gt;&lt;/code&gt; et non plus des instances d'objet &lt;code&gt;MyObject*&lt;/code&gt;.&lt;/p&gt;


&lt;p&gt;Notre méthode &lt;code&gt;setMember()&lt;/code&gt; peut alors être ré-écrite&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; MyObject&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;setMember&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;Object2 &lt;span style=&quot;color: #000040;&quot;&gt;*&lt;/span&gt; obj&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
  _membre &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; obj&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    obj&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;setParent&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;_this.&lt;span style=&quot;color: #007788;&quot;&gt;toStrongRef&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;&lt;a name=&quot;comment_eviter_les_references_circulaires&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Comment éviter les références circulaires&lt;/h4&gt;


&lt;p&gt;Le principe d'une référence circulaire est qu'un objet A référence l'objet B et l'objet B référence l'objet A.&lt;/p&gt;


&lt;p&gt;Voici par exemple, un cas de référence circulaire&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; A
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;:&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; create&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;A&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; A&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; ptr&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
  ~A&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;:&lt;/span&gt;
  A&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    b &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; B&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
    b&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;setA&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;_this&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
  QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;B&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; b&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  QWeakPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;A&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; _this&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; B
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;:&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;B&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; create&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt; 
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;B&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; B&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; ptr&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; setA&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;A&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; b&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;A&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; getA&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;:&lt;/span&gt;
  QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;A&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;  a&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Cela peut-être aussi le cas, si par exemple une instance d'objet C référence des instances d'objets fils C, qui possèdent eux-même un pointeur vers l'objet C père.&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; C
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;:&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;C&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; create&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;C&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; C&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
    _this &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; ptr.&lt;span style=&quot;color: #007788;&quot;&gt;toWeakPtr&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; ptr&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  ~C&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; addChild&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;C&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; c&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    _childs.&lt;span style=&quot;color: #007788;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;c&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
    c&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;setParent&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;_this&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; setParent&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;C&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; c&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;C&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; getParent&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;:&lt;/span&gt;
  C&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  QWeakPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;C&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; _this&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;C&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; _parent&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  QList&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt; QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;C&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; _childs&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Dans ces cas là, on a&amp;nbsp;:
- L'objet A possède la référence vers l'objet B
- L'objet B possède une référence vers l'objet A.
- Même principe avec l'objet C&lt;/p&gt;


&lt;p&gt;&lt;img src=&quot;http://www.shadoware.org/public/billets/qsharedpointer/QSharedPointer3.png&quot; alt=&quot;QSharedPointer3.png&quot; style=&quot;display:block; margin:0 auto;&quot; title=&quot;QSharedPointer3.png, janv. 2011&quot; /&gt;&lt;/p&gt;


&lt;p&gt;Dans ce cas, il restera alors toujours une référence vers A, et une vers B, même si plus aucune variable ne référence ces objets. Cette référence circulaire fait que l'objet ne sera jamais détruit même si on n'a plus besoin de l'objet.&lt;/p&gt;


&lt;p&gt;Si on décide que l'objet A sera l'objet maitre (donc que sa destruction engendrera la destruction de l'objet B), on peut alors écrire les choses ainsi pour l'objet B&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;class&lt;/span&gt; B
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;:&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;B&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; create&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt; 
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
    QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;B&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; B&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; ptr&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; setA&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;QWeakPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;A&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; b&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  QWeakPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;A&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; getA&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #0000ff;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;:&lt;/span&gt;
  QWeakPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;A&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;  a&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Dans ce cas, avec l'utilisation d'un &lt;code&gt;QWeakPointer&lt;/code&gt;, lorsque qu'il n'existera plus de référence vers l'objet A, le pointeur &lt;em&gt;faible&lt;/em&gt; &lt;code&gt;a&lt;/code&gt; sera mis à jour comme ne contenant plus de référence&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-3&quot; id=&quot;rev-pnote-628-3&quot;&gt;3&lt;/a&gt;]&lt;/sup&gt;. L'instance de l'objet A sera réellement détruite. Il n'y aura alors plus aucune référence vers l'objet B qui sera alors également détruit&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-4&quot; id=&quot;rev-pnote-628-4&quot;&gt;4&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;


&lt;p&gt;&lt;a name=&quot;utilisation_dans_les_applications_multi_thread&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Utilisation dans les applications multi-thread.&lt;/h4&gt;


&lt;p&gt;L'utilisation de &lt;code&gt;QSharedPointer&lt;/code&gt; simplifie l'écriture des applications multi-thread (les objets &lt;code&gt;QSharedPointer&lt;/code&gt; et &lt;code&gt;QWeakPointer&lt;/code&gt; sont thread-safe).&lt;/p&gt;


&lt;p&gt;Dans ces applications il n'y a alors plus besoin de se soucier si l'objet est en cours d'utilisation ailleurs dans l'application avant de le supprimer&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-5&quot; id=&quot;rev-pnote-628-5&quot;&gt;5&lt;/a&gt;]&lt;/sup&gt;. Lorsqu'un pointeur ne devient plus utilisé dans un thread donné, il ne sera détruit que s'il n'y a pas d'autres références dans d'autres threads de l'application&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-6&quot; id=&quot;rev-pnote-628-6&quot;&gt;6&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;


&lt;p&gt;Avec l'utilisation de &lt;code&gt;QWeakPointer&lt;/code&gt;, un thread pourra tester l'existence du pointeur avant d'effectuer une opération et pourra aviser le cas échéant sans faire planter toute l'application&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-7&quot; id=&quot;rev-pnote-628-7&quot;&gt;7&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;


&lt;p&gt;&lt;a name=&quot;utilisation_d_un_pool&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Utilisation d'un pool&lt;/h4&gt;


&lt;p&gt;Si la création et la destruction d'un objet est coûteux, il est envisageable de diminuer le coût de destruction et de création d'un thread en utilisant un Pool d'objet. Dans ce cas l'objet &lt;a href=&quot;http://doc.qt.nokia.com/4.6/qqueue.html&quot; hreflang=&quot;en&quot; title=&quot;Documentation de QQueue&quot;&gt;QQueue&lt;/a&gt; pourra être utilisé pour représenter notre Pool.&lt;/p&gt;


&lt;p&gt;Lors de la demande de création, en utilisant notre méthode &lt;code&gt;create&lt;/code&gt; ci-dessus, on prend alors une valeur du pool (si disponible) et on la retourne sous forme d'un &lt;code&gt;QSharedPointer&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; MyObject&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
  MyObject &lt;span style=&quot;color: #000040;&quot;&gt;*&lt;/span&gt; c_ptr&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;_queue.&lt;span style=&quot;color: #007788;&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
	c_ptr &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; _queue.&lt;span style=&quot;color: #007788;&quot;&gt;dequeue&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;else&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt; 
	c_ptr &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; MyObject&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
  QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;MyObject&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;c_ptr, ReturnToPool&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  ptr&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;_this &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; ptr.&lt;span style=&quot;color: #007788;&quot;&gt;toWeakRef&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; ptr&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Dans l'exemple ci-dessus&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-8&quot; id=&quot;rev-pnote-628-8&quot;&gt;8&lt;/a&gt;]&lt;/sup&gt;, on demande à la queue, qui doit être une variable globale ou statique, un élément, et si ce n'est pas possible, on crée un nouvel objet de type &lt;code&gt;MyObject&lt;/code&gt; (dont on suppose la création coûteuse).&lt;/p&gt;


&lt;p&gt;Lors de la création du &lt;code&gt;QSharedPointer&lt;/code&gt; on utilise alors le constructeur &lt;code&gt;QSharedPointer ( T * ptr, Deleter deleter )&lt;/code&gt; sur lequel on définit une méthode &lt;code&gt;Deleter&lt;/code&gt; nommée &lt;code&gt;ReturnToPool&lt;/code&gt; dont le but est de remettre les objets en pool.&lt;/p&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; ReturnToPool&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;MyObject &lt;span style=&quot;color: #000040;&quot;&gt;*&lt;/span&gt;obj&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;_queue.&lt;span style=&quot;color: #007788;&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt; MAX_SIZE_QUEUE&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
	_queue.&lt;span style=&quot;color: #007788;&quot;&gt;enqueue&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span style=&quot;color: #0000ff;&quot;&gt;else&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
	&lt;span style=&quot;color: #0000dd;&quot;&gt;delete&lt;/span&gt; obj&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
  &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Dans ce cas de retour au pool, si le pool est rempli, on détruit l'objet (pour éviter de consommer trop de mémoire), sinon on l'ajoute au pool. Dans ce cas, le pool est agrandi au fur et à mesure des besoins, jusqu'à une taille limite.&lt;/p&gt;


&lt;p&gt;Bien sûr il faut que la performance de l'utilisation d'un pool soit plus intéressante que celle de la création de l'objet et de son initialisation.&lt;/p&gt;


&lt;p&gt;&lt;strong&gt;&lt;ins&gt;Attention&lt;/ins&gt;&amp;nbsp;: Ce point ne fonctionne, par contre, pas si l'objet (&lt;code&gt;MyObject&lt;/code&gt;) est un descendant de &lt;code&gt;QObject&lt;/code&gt;. En effet &lt;code&gt;QObject&lt;/code&gt; garde une référence du &lt;code&gt;QSharedPointer&lt;/code&gt; en mémoire et lors de la réutilisation du &lt;code&gt;QObject&lt;/code&gt; une erreur indique que l'objet n'a pas été détruit et est déjà utilisé par un &lt;code&gt;QSharedPointer&lt;/code&gt;. On n'a pas le problème avec &lt;code&gt;std::tr1::shared_ptr&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;


&lt;p&gt;&lt;a name=&quot;benchmark&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;Benchmark&lt;/h3&gt;


&lt;p&gt;Le but du benchmark est de se faire une idée sur les performances d'une application utilisant des &lt;code&gt;QSharedPointer&lt;/code&gt; à la place des pointeurs normaux. Attention, ce bench ne prend pas en compte le besoin potentiel de Mutex, de comptage de référence manuel, ... dans les applications multi-thread qui pourrait être nécessaire pour ne pas supprimer le pointeur si besoin.&lt;/p&gt;


&lt;p&gt;Dans ce test nous allons tester également (en comparaison), le pointeur &lt;em&gt;intelligent&lt;/em&gt; du C++0x&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#pnote-628-9&quot; id=&quot;rev-pnote-628-9&quot;&gt;9&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;


&lt;p&gt;Nous allons donc tester les opérations courantes de création, destruction, modification, affectation.&lt;/p&gt;


&lt;p&gt;&lt;a name=&quot;code_source&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Code source&lt;/h4&gt;


&lt;p&gt;Le code source est disponible, attaché au billet. Dans la suite du billet, seuls les morceaux intéressants du benchmark seront décris. Le benchmark utilise QTest. Nous avons créé un objet bidon &lt;code&gt;ObjetTest&lt;/code&gt; qui dans le constructeur allouera un pointeur et remplira une liste, et le destructeur supprime ce pointeur (et forcément la liste).&lt;/p&gt;


&lt;p&gt;Pour que chaque test soit indépendant, le jeu de test sera initialisé avant le début de chaque QBENCHMARK et détruit à la fin du bloc. Nous aurons quatre méthodes&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Allocation&lt;/li&gt;
&lt;li&gt;Modification&lt;/li&gt;
&lt;li&gt;Affectation&lt;/li&gt;
&lt;li&gt;Nettoyage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pour chaque test nous allons faire le test avec&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;un pointeur C standard&lt;/li&gt;
&lt;li&gt;le pointeur &lt;code&gt;QSharedPointer&lt;/code&gt; de &lt;em&gt;Qt&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;le pointeur &lt;code&gt;std::tr1::shared_ptr&lt;/code&gt; de &lt;em&gt;C++0x&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pour le test d'allocation et le test de nettoyage, nous allons également utiliser l'optimisation possible, vu ci-dessus, d'un Pool d'objet. Nous allons faire le test avec&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;le pointeur &lt;code&gt;QSharedPointer&lt;/code&gt; de &lt;em&gt;Qt&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;le pointeur &lt;code&gt;std::tr1::shared_ptr&lt;/code&gt; de &lt;em&gt;C++0x&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a name=&quot;le_jeu_de_test&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;Le jeu de test&lt;/h4&gt;


&lt;p&gt;&lt;a name=&quot;test_de_l_allocation&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;Test de l'allocation&lt;/h5&gt;


&lt;p&gt;La création du pointeur en utilisant &lt;code&gt;QSharedPointer&lt;/code&gt; instancie le pointeur ainsi que le &lt;code&gt;QSharedPointer&lt;/code&gt;. Le temps d'exécution est donc potentiellement deux fois plus long (voir le benchmark à la fin de ce billet).&lt;/p&gt;


&lt;p&gt;Pour la création du pool, nous allons utiliser une méthode qui créera le pointeur s'il n'est pas dans le pool, et sinon prendra le pointeur du pool. Dans notre cas de test, il y aura toujours une valeur dans le pool, que l'on aura rempli au préalable.&lt;/p&gt;


&lt;p&gt;La méthode &lt;code&gt;createFromPool()&lt;/code&gt; et &lt;code&gt;createFromBoostPool()&lt;/code&gt; est sensiblement identique&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;ObjetTest&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; createFromPool&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
        ObjetTest &lt;span style=&quot;color: #000040;&quot;&gt;*&lt;/span&gt; c_ptr&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #0000ff;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;_queue.&lt;span style=&quot;color: #007788;&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
        &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
          c_ptr &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; _queue.&lt;span style=&quot;color: #007788;&quot;&gt;dequeue&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
        &lt;span style=&quot;color: #0000ff;&quot;&gt;else&lt;/span&gt;
        &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
          c_ptr &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; ObjetTest&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
        QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;ObjetTest&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;c_ptr, returnToPool&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #0000ff;&quot;&gt;return&lt;/span&gt; ptr&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;table&gt;
&lt;tr&gt;&lt;th&gt;C Pointer&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;ObjetTest&lt;span style=&quot;color: #000040;&quot;&gt;*&lt;/span&gt; ptr &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; ObjetTest&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;Qt Smart Pointer&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;ObjetTest&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; ObjetTest&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;Qt Smart Pointer as Pool&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;ObjetTest&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; createFromPool &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;C++0x Smart Pointer&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;std&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;tr1&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;shared_ptr&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;ObjetTest&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;new&lt;/span&gt; ObjetTest&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;C++0x Smart Pointer as Pool&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;std&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;tr1&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;shared_ptr&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;ObjetTest&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; ptr &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; createFromBoostPool &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;



&lt;p&gt;&lt;a name=&quot;test_de_modification_d_une_donnee&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;Test de Modification d'une donnée&lt;/h5&gt;


&lt;p&gt;Pour la modification d'une donnée, on génère un nombre aléatoire que l'on va stocker (toujours le même pour chaque test, cela n'a pas d'importance). La génération du nombre aléatoire se fait en dehors du bloc, pour éviter de polluer le test avec le calcul d'un nombre aléatoire. Ici il n'y a pas création d'affectation du pointeur, juste une affectation d'une valeur dans le contenu du pointeur. La syntaxe pour le C, et pour le pointeur intelligent est identique.&lt;/p&gt;

&lt;table&gt;
&lt;tr&gt;&lt;th&gt;C Pointer / Qt Smart Pointer / C++0x Smart Pointer&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;obj&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;value &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; random_number&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;



&lt;p&gt;&lt;a name=&quot;test_d_affectation&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;Test d'affectation&lt;/h5&gt;


&lt;p&gt;Pour l'affectation nous allons créer une nouvelle variable qui pointera sur le même pointeur, et sur lequel on fera une modification. La création d'un pointeur peut arriver par exemple lors du passage du pointeur à une fonction, ou lors de la déclaration d'une variable devant contenir la même valeur. Cette déclaration supplémentaire a peu d'impact pour un pointeur C mais pour un pointeur &lt;em&gt;intelligent&lt;/em&gt; oblige la création d'un objet, et l'incrément d'un nombre d'instance (qu'on décrémente ici dans la même boucle).&lt;/p&gt;

&lt;table&gt;
&lt;tr&gt;&lt;th&gt;C Pointer&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;ObjetTest &lt;span style=&quot;color: #000040;&quot;&gt;*&lt;/span&gt; obj2 &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; obj&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
obj2&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;value &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; random_number&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;Qt Smart Pointer&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;QSharedPointer&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;ObjetTest&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; obj2 &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; obj&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
obj2&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;value &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; random_number&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;C++0x Smart Pointer&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;std&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;tr1&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #007788;&quot;&gt;shared_ptr&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;lt;&lt;/span&gt;ObjetTest&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt; obj2 &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; obj&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
obj2&lt;span style=&quot;color: #000040;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #000080;&quot;&gt;&amp;gt;&lt;/span&gt;value &lt;span style=&quot;color: #000080;&quot;&gt;=&lt;/span&gt; random_number&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;



&lt;p&gt;&lt;a name=&quot;test_de_destruction&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;Test de destruction&lt;/h5&gt;


&lt;p&gt;Pour ce test, nous allons initialiser une liste de pointeur, et pour le benchmark, nous allons supprimer un à un chaque élément de la liste.
La destruction du pointeur en C se fait par un &lt;code&gt;delete&lt;/code&gt;. Pour le pointeur ''intelligent', il n'y a pas de destruction explicite. Nous allons juste supprimer le pointeur de la liste, le pointeur sera alors automatiquement détruit car il n'y aura plus de référence vers ce pointeur.&lt;/p&gt;


&lt;p&gt;Pour le cas de test utilisant la notion du Pool, on aura créé le pointeur avec le delete &lt;code&gt;returnToPool()&lt;/code&gt;&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;void&lt;/span&gt; returnToPool&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;ObjetTest &lt;span style=&quot;color: #000040;&quot;&gt;*&lt;/span&gt;obj&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#123;&lt;/span&gt;
        _queue.&lt;span style=&quot;color: #007788;&quot;&gt;enqueue&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;obj&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;Cette méthode ne fait pas de réelle destruction, mais juste un ajout de l'objet au pool.&lt;/p&gt;

&lt;table&gt;
&lt;tr&gt;&lt;th&gt;C Pointer&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;delete&lt;/span&gt; c_ptr_list.&lt;span style=&quot;color: #007788;&quot;&gt;at&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000dd;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;
c_ptr_list.&lt;span style=&quot;color: #007788;&quot;&gt;removeFirst&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;Qt Smart Pointer / Qt Smart Pointer as Pool / C++0x Smart Pointer / C++0x Smart Pointer as Pool &lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;

&lt;pre class=&quot;cpp cpp&quot; style=&quot;font-family:inherit&quot;&gt;smart_ptr_list.&lt;span style=&quot;color: #007788;&quot;&gt;removeFirst&lt;/span&gt; &lt;span style=&quot;color: #008000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #008000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #008080;&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;



&lt;p&gt;&lt;a name=&quot;resultat_du_test&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;Résultat du test&lt;/h5&gt;


&lt;p&gt;Le test a été fait en utilisant la version 4.6.3 de Qt. Test effectué pour 5 000 000 itérations.&lt;/p&gt;

&lt;table&gt;
&lt;tr&gt;&lt;th/&gt;&lt;th&gt;Pointeur C&lt;/th&gt;&lt;th&gt;Pointeur Qt&lt;/th&gt;&lt;th&gt;Pointeur C++0x&lt;/th&gt;&lt;th&gt;Pool en utilisant QSharedPointer&lt;/th&gt;&lt;th&gt;Pool en utilisant std::tr1::shared_ptr&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;Allocation&lt;/th&gt;&lt;td&gt;0.0004275 msec&lt;/td&gt;&lt;td&gt;0.0007692 msec&lt;/td&gt;&lt;td&gt;0.0006604 msec&lt;/td&gt;&lt;td&gt;0.0002590 msec&lt;/td&gt;&lt;td&gt;0.0002286 msec&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;Modification&lt;/th&gt;&lt;td&gt;0.000010 msec&lt;/td&gt;&lt;td&gt;0.000012 msec&lt;/td&gt;&lt;td&gt;0.000012 msec&lt;/td&gt;&lt;td colspan=&quot;2&quot; rowspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;Affectation&lt;/th&gt;&lt;td&gt;0.000010 msec&lt;/td&gt;&lt;td&gt;0.0000386 msec&lt;/td&gt;&lt;td&gt;0.0000230 msec&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;th&gt;Destruction&lt;/th&gt;&lt;td&gt;0.000190 msec&lt;/td&gt;&lt;td&gt;0.0003161 msec&lt;/td&gt;&lt;td&gt;0.0003359 msec&lt;/td&gt;&lt;td&gt;0.0004003 msec&lt;/td&gt;&lt;td&gt;0.0003601 msec&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;



&lt;p&gt;Conclusion que l'on peut en tirer, le pointeur C est ce qu'il y a de plus rapide à partir du moment où on fait de l'allocation de l'affectation ou de la destruction. Par contre il n'apporte pas la souplesse qu'apporte les pointeurs &lt;em&gt;intelligents&lt;/em&gt; entre autre pour les applications multi-threadé.&lt;/p&gt;


&lt;p&gt;On remarque que le pointeur C++0x est plus rapide pour la création, mais apparemment plus lent en destruction. Il est également possible avec le pool de gagner en performance (surtout en création). Par contre le coût de destruction de l'objet n'est pas encore assez fort pour y gagner en utilisant le pool.&lt;/p&gt;


&lt;p&gt;Ensuite il est important de se faire son propre jugement selon ses besoins. Si besoin le source est attaché, vous pouvez faire vos propres tests.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-1&quot; id=&quot;pnote-628-1&quot;&gt;1&lt;/a&gt;] Si à un moment donné il faut utiliser le pointeur C pour une raison quelconque, on peut utiliser &lt;code&gt;ptr.data()&lt;/code&gt; mais il faut s'assurer que le pointeur ne sera pas détruit en déclarant un &lt;code&gt;QSharedPointer&lt;/code&gt; dans le même bloc utilisant le pointeur C. Le &lt;code&gt;QSharedPointer&lt;/code&gt; ne devra être détruit qu'après utilisation du pointeur C. Ceci peut être fait dans certain cas pour des raisons de performance.&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-2&quot; id=&quot;pnote-628-2&quot;&gt;2&lt;/a&gt;] Sinon nous ne serions plus là pour lancer la méthode&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-3&quot; id=&quot;pnote-628-3&quot;&gt;3&lt;/a&gt;] Le &lt;code&gt;QWeakPointeur&lt;/code&gt; ne gardant pas d'instance d'objet, car il n'incrémente pas le compteur de référence&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-4&quot; id=&quot;pnote-628-4&quot;&gt;4&lt;/a&gt;] s'il n'existe pas de référence vers l'objet B ailleurs dans l'application&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-5&quot; id=&quot;pnote-628-5&quot;&gt;5&lt;/a&gt;] Attention quand même, &lt;code&gt;QSharedPointer&lt;/code&gt; protège le pointeur mais pas le contenu&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-6&quot; id=&quot;pnote-628-6&quot;&gt;6&lt;/a&gt;] d'autres threads pouvant inclure le thread principal&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-7&quot; id=&quot;pnote-628-7&quot;&gt;7&lt;/a&gt;] Si l'objet est supprimé, &lt;code&gt;QWeakPointer&lt;/code&gt;, sera alors remis à &lt;code&gt;null&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-8&quot; id=&quot;pnote-628-8&quot;&gt;8&lt;/a&gt;] Dans le code en question, il faudrait ajouter la notion de mutex autour de la gestion de la queue, en cas de création parallèle.&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#rev-pnote-628-9&quot; id=&quot;pnote-628-9&quot;&gt;9&lt;/a&gt;] Le pointeur &lt;em&gt;intelligent&lt;/em&gt; &lt;code&gt;shared_ptr&lt;/code&gt; de C++0x à pour origine le pointeur Boost&lt;/p&gt;&lt;/div&gt;
</description>
    
          <enclosure url="http://www.shadoware.org/public/billets/qsharedpointer/smart_benchmark.7z"
      length="1805" type="text/plain" />
    
    
          <comments>http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#comment-form</comments>
      <wfw:comment>http://www.shadoware.org/post/2011/01/17/Performance-de-l-utilisation-de-QSharedPointer#comment-form</wfw:comment>
      <wfw:commentRss>http://www.shadoware.org/feed/atom/comments/628</wfw:commentRss>
      </item>
    
  <item>
    <title>Qt 4.5 est sortie</title>
    <link>http://www.shadoware.org/post/2009/03/03/Qt-4.5-est-sortie</link>
    <guid isPermaLink="false">urn:md5:64ae6f28b3981e32fa5d8e9568c8220c</guid>
    <pubDate>Tue, 03 Mar 2009 16:42:00 +0100</pubDate>
    <dc:creator>Ulrich Van Den Hekke</dc:creator>
        <category>Logiciels</category>
        <category>kde</category><category>nokia</category><category>planet</category><category>qmake</category><category>qt</category>    
    <description>    &lt;p&gt;Voilà quelques heures que la dernière version de &lt;em&gt;Qt&lt;/em&gt; est sortie. Cette version, numéroté 4.5, est essentiellement orienté performance.&lt;/p&gt;


&lt;p&gt;&lt;img src=&quot;http://www.shadoware.org/public/billets/qt/logo_nokia_qt.png&quot; alt=&quot;Logo Nokia/Qt&quot; style=&quot;display:block; margin:0 auto;&quot; title=&quot;Logo Nokia/Qt, fév. 2009&quot; /&gt;&lt;/p&gt;


&lt;p&gt;Elle contient entre autre &lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2009/03/03/Qt-4.5-est-sortie#pnote-617-1&quot; id=&quot;rev-pnote-617-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Une mise à jour de &lt;em&gt;QtWebKit&lt;/em&gt; vers un version de &lt;em&gt;WebKit&lt;/em&gt; plus récente (avec un nouveau moteur JavaScript plus rapide, la possibilité d'ajouter des greffons comme &lt;em&gt;Flash&lt;/em&gt;...). Il est également possible d'utiliser les balises d'HTML 5.&lt;/li&gt;
&lt;li&gt;Amélioration des performances sur le moteur de rendu ainsi que sur le rendu du texte. Une librairie permettant de faire des tests de performance a été incluse&amp;nbsp;: &lt;em&gt;QtBenchLib&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Le support de &lt;em&gt;Mac OS X Cocoa&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;La possibilité de faire des transformations XSLT&lt;/li&gt;
&lt;li&gt;Un débuggeur &lt;em&gt;QtScript&lt;/em&gt;&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2009/03/03/Qt-4.5-est-sortie#pnote-617-2&quot; id=&quot;rev-pnote-617-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img src=&quot;http://www.shadoware.org/public/billets/qt/qtscript-debugger-small.png&quot; alt=&quot;Qt Script Debuger&quot; style=&quot;display:block; margin:0 auto;&quot; title=&quot;Qt Script Debugger, mar. 2009&quot; /&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Le support du format &lt;em&gt;OpenDocument&lt;/em&gt; (ODF version 1.0, ne supportant que le texte et les images)&lt;/li&gt;
&lt;li&gt;Amélioration du support des proxys&lt;/li&gt;
&lt;li&gt;Amélioration de &lt;em&gt;QtDesigner&lt;/em&gt;, &lt;em&gt;QtLinguist&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;Une meilleur intégration au thème Gtk&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cette nouvelle version apporte également une grande nouveauté puisqu'elle est &lt;em&gt;LGPL&lt;/em&gt;. Il est donc désormais possible de faire une application propriétaire à l'aide de cette librairie sans prendre une licence chez &lt;em&gt;Qt Software&lt;/em&gt;. Par contre si une entreprise souhaite un support pour la librairie, il faudra tout de même acheter une licence.&lt;/p&gt;


&lt;p&gt;&lt;img src=&quot;http://www.shadoware.org/public/billets/qt/.ScreenShot166_s.jpg&quot; alt=&quot;Téléchargement de Qt 4.5&quot; style=&quot;float:right; margin: 0 0 1em 1em;&quot; title=&quot;Téléchargement de Qt 4.5, mar. 2009&quot; /&gt;&lt;/p&gt;


&lt;p&gt;La sortie de &lt;em&gt;Qt&lt;/em&gt; 4.5 est accompagné de &lt;em&gt;Qt Creator&lt;/em&gt; 1.0. &lt;em&gt;Qt Creator&lt;/em&gt; est l'outil dernier né de chez &lt;em&gt;Qt Software&lt;/em&gt; (branche de &lt;em&gt;Nokia&lt;/em&gt;) et est un IDE de développement de programme &lt;em&gt;Qt&lt;/em&gt;. Un paquet est proposé contenant &lt;em&gt;Qt&lt;/em&gt;, &lt;em&gt;Qt Creator&lt;/em&gt;, ainsi que &lt;em&gt;MinGW&lt;/em&gt; et permettant d'avoir une installation tout en un d'un poste de développement.&lt;/p&gt;


&lt;p&gt;Vous pouvez donc aller télécharger la dernière version de &lt;em&gt;Qt&lt;/em&gt;, ainsi que de &lt;em&gt;Qt Creator&lt;/em&gt; (attention les serveurs sont surchargés) à cette &lt;a href=&quot;http://www.qtsoftware.com/downloads&quot; hreflang=&quot;fr&quot;&gt;adresse&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Je remercie toute l'équipe de nouvellement &lt;em&gt;Qt Software&lt;/em&gt; de cette nouvelle version.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2009/03/03/Qt-4.5-est-sortie#rev-pnote-617-1&quot; id=&quot;pnote-617-1&quot;&gt;1&lt;/a&gt;] pour plus de détail voir &lt;a href=&quot;http://doc.trolltech.com/4.5/qt4-5-intro.html&quot; hreflang=&quot;fr&quot;&gt;http://doc.trolltech.com/4.5/qt4-5-intro.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2009/03/03/Qt-4.5-est-sortie#rev-pnote-617-2&quot; id=&quot;pnote-617-2&quot;&gt;2&lt;/a&gt;] L'image du debugger viens du site &lt;em&gt;Qt Software&lt;/em&gt;&lt;/p&gt;&lt;/div&gt;
</description>
    
          <enclosure url="http://www.shadoware.org/public/billets/qt/changes-4.5"
      length="59463" type="text/plain" />
    
    
          <comments>http://www.shadoware.org/post/2009/03/03/Qt-4.5-est-sortie#comment-form</comments>
      <wfw:comment>http://www.shadoware.org/post/2009/03/03/Qt-4.5-est-sortie#comment-form</wfw:comment>
      <wfw:commentRss>http://www.shadoware.org/feed/atom/comments/617</wfw:commentRss>
      </item>
    
  <item>
    <title>Paquet Debian et Qt</title>
    <link>http://www.shadoware.org/post/2008/09/08/45-paquet-debian-et-qt</link>
    <guid isPermaLink="false">urn:md5:98bc4b24cff491d05aba301c4e3acd97</guid>
    <pubDate>Mon, 08 Sep 2008 20:45:00 +0000</pubDate>
    <dc:creator>Ulrich Van Den Hekke</dc:creator>
        <category>Distribution</category>
        <category>debian</category><category>dépots</category><category>paquets</category><category>planet</category><category>qmake</category><category>qt</category><category>trolltech</category>    
    <description>&lt;p&gt;A titre personnel je fabrique quelques programmes en Qt. Comme j'utilise un système &lt;em&gt;Gnu/Debian&lt;/em&gt;, j'ai cherché à fabriquer des paquets pour mon système (plus pour le plaisir qu'autre chose, car la plus grande partie de mes utilisateurs sont sous &lt;em&gt;MS/Windows&lt;/em&gt;).&lt;/p&gt;


&lt;p&gt;Ce billet explique la création de paquet &lt;em&gt;Gnu/Debian&lt;/em&gt; pour des applications Qt 4 utilisant QMake&lt;/p&gt;    &lt;h3&gt;Installation des paquets&lt;/h3&gt;


&lt;p&gt;Pour la création des paquets pour des programmes utilisant &lt;code&gt;qmake&lt;/code&gt;, il faut &lt;code&gt;cdbs, dh_make, dpkg-buildpackage&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;bash&quot;&gt;aptitude &lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;install&lt;/span&gt; cdbs dh-&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;make&lt;/span&gt; fakeroot devscripts&lt;/pre&gt;


&lt;h3&gt;Création du paquet&lt;/h3&gt;


&lt;h4&gt;Préparation&lt;/h4&gt;


&lt;p&gt;Vous pouvez donc choisir un de vos programmes que vous voulez empaqueter. Pour cela vous allez commencer par nettoyer l'arborescence de votre projet pour retirer les fichiers de sauvegarde ainsi que les fichiers de construction (&lt;code&gt;*~ *.o&lt;/code&gt;, ...). Ce petit nettoyage va permettre d'avoir un paquet source &lt;em&gt;propre&lt;/em&gt;.&lt;/p&gt;


&lt;p&gt;Il faut que vous placiez les sources dans un dossier du nom de &lt;code&gt;&amp;lt;programme&amp;gt;-&amp;lt;version&amp;gt;&lt;/code&gt;.&lt;/p&gt;


&lt;p&gt;Voici un exemple de fichier projet&amp;nbsp;:&lt;/p&gt;

&lt;pre&gt;
TARGET = qgenconfig
TEMPLATE = app

CONFIG += exceptions warn_on qt
QT += xml

HEADERS += src/loadconfigimpl.h \
   src/setupfile.h \
   src/configfile.h \
   src/p_configfile.h
SOURCES += src/loadconfigimpl.cpp \
   src/setupfile.cpp \
   src/main.cpp \
   src/configfile.cpp
FORMS = ui/loadconfig.ui

DESTDIR = bin
MOC_DIR = build
OBJECTS_DIR = build
UI_DIR = build
&lt;/pre&gt;


&lt;p&gt;Pour générer le &lt;code&gt;Makefile&lt;/code&gt;, vous pouvez faire comme d'habitude&amp;nbsp;:&lt;/p&gt;
&lt;pre class=&quot;bash&quot;&gt;qmake-qt4 qgenconfig.pro&lt;/pre&gt;


&lt;h4&gt;Création des scripts&lt;/h4&gt;


&lt;p&gt;Pour créer les paquets, il faut un ensemble de script dans le dossier &lt;code&gt;debian&lt;/code&gt;. Pour se faciliter la vie, il est possible de créer les scripts avec &lt;code&gt;dh_make&lt;/code&gt;.&lt;/p&gt;

&lt;pre class=&quot;bash&quot;&gt;&lt;span style=&quot;color: #007800;&quot;&gt;DEBFULLNAME=&lt;/span&gt;&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;Ulrich Van Den Hekke&amp;quot;&lt;/span&gt; dh_make -e ulrich.vdh&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;@&lt;/span&gt;shadoware.org -n -s -c gpl&lt;/pre&gt;


&lt;p&gt;Voici la description de la liste des paramètres&amp;nbsp;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;DEBFULLNAME&lt;/code&gt; indique le nom du mainteneur.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-e&lt;/code&gt; Permet de préciser l'email (suivis de l'email).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-n&lt;/code&gt; Produit un paquet natif pour &lt;em&gt;Gnu/Debian&lt;/em&gt; (quand l'auteur fait ses paquets lui-même).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-s&lt;/code&gt; Indique que le paquet ne va contenir qu'un simple exécutable (si vous voulez faire plusieurs paquets, il suffit de ne pas mettre cette option).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-c&lt;/code&gt; Indique la licence d'utilisation (ici &lt;code&gt;gpl&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Vous pouvez ensuite supprimer les fichiers exemples (mais pas nécessairement si vous voulez les étudier).&lt;/p&gt;

&lt;pre class=&quot;bash&quot;&gt;&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;rm&lt;/span&gt; debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/*&lt;/span&gt;.ex  debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/*&lt;/span&gt;.EX  debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;docs  debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;dirs&lt;/span&gt;&lt;/pre&gt;


&lt;h5&gt;Paquet simple&lt;/h5&gt;


&lt;p&gt;Nous allons commencer par créer un paquet simple. Cela signifie qu'il n'y aura qu'un seul exécutable dans le paquet (pas de librairie, ...).&lt;/p&gt;


&lt;p&gt;Dans le fichier &lt;code&gt;control&lt;/code&gt;, il faut que vous renseignez les informations du paquet que vous allez créer.&lt;/p&gt;

&lt;pre&gt;
Source: qgenconfig
Section: devel
Priority: extra
Maintainer: Ulrich Van Den Hekke &amp;lt;ulrich.vdh@shadoware.org&amp;gt;
Build-Depends: debhelper (&amp;gt;= 7)
Standards-Version: 3.7.3
Homepage: http://projects.shadoware.org/qgenconfig

Package: qgenconfig
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}
Description: Create a config class in Qt after loading a XML file.
 Load an XML description file of a settings object and create a class
 that can be used to load and save information in a QSettings class. To
 load information structure are used.
&lt;/pre&gt;


&lt;p&gt;Dans le paquet ci-dessus, on créait un paquet binaire et un paquet source. Les variables de dépendance (dans &lt;code&gt;Depends&lt;/code&gt;) sont remplacées automatiquement à la génération du paquet. La description est celle écrite dans &lt;code&gt;aptitude&lt;/code&gt;. La première ligne est la description courte, les suivantes (précédées d'un espace) sont la description longue.&lt;/p&gt;


&lt;p&gt;Ensuite on va remplacer le fichier &lt;code&gt;debian/rules&lt;/code&gt; en utilisant &lt;code&gt;cdbs&lt;/code&gt;&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;bash&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/make -f&lt;/span&gt;
include &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;share&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;cdbs&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;rules&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debhelper.mk
include &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;share&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;cdbs&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;class&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;qmake.mk
&lt;span style=&quot;color: #007800;&quot;&gt;QMAKE=&lt;/span&gt;qmake-qt4
&amp;nbsp;
&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;install&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;qgenconfig::
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;mkdir&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;DEB_DESTDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;mkdir&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;DEB_DESTDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;bin&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cp&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;bin&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;qgenconfig $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;DEB_DESTDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;bin&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;/pre&gt;


&lt;p&gt;En premier lieu on définit les variables pour la compilation, puis ensuite on définit les opérations d'installations dans la cible &lt;code&gt;install/qgenconfig&lt;/code&gt;&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2008/09/08/45-paquet-debian-et-qt#pnote-36-1&quot; id=&quot;rev-pnote-36-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.
Dans les opérations, vous allez copier votre exécutable dans le dossier de génération de debian (dossier &lt;code&gt;debian/&amp;lt;cible&amp;gt;&lt;/code&gt;).&lt;/p&gt;


&lt;h5&gt;Paquet multiple&lt;/h5&gt;


&lt;p&gt;Créer plusieurs paquets à partir d'une même source avec &lt;code&gt;cdbs&lt;/code&gt; est assez simple. Il suffit de rajouter des cibles, correspondantes au fichier &lt;code&gt;control&lt;/code&gt;, dans le fichier &lt;code&gt;rules&lt;/code&gt; .&lt;/p&gt;


&lt;p&gt;Dans le fichier de &lt;code&gt;control&lt;/code&gt; on va avoir plusieurs paquets binaires pour un seul paquet source.&lt;/p&gt;

&lt;pre&gt;
Source: xinx
Section: devel
Priority: extra
Maintainer: Ulrich Van Den Hekke &amp;lt;xinx@shadoware.org&amp;gt;
Build-Depends: debhelper (&amp;gt;= 7)
Standards-Version: 3.7.3
Homepage: http://xinx.shadoware.org/

Package: xinx
Section: devel
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}, xinx-scripts (&amp;gt;= 0.7.2.0)
Recommends: xinx-plugins-services, xinx-plugins-cvs, xinx-plugins-svn
Suggests: xinx-doc
Description: XSL/JS/HTML editor for Generix
 XINX is an editor of XSL stylesheet, JavaScript, Cascading Style Sheet.
 This editor is used with egx.

Package: xinx-doc
Section: doc
Architecture: all
Depends: xinx (&amp;gt;= 0.7.2.0)
Description: Documentation for xinx
 Technical Documentation of XINX
&lt;/pre&gt;


&lt;p&gt;Nous allons définir dans le fichier &lt;code&gt;rules&lt;/code&gt; les différentes cibles que nous avons dans le fichier &lt;code&gt;control&lt;/code&gt;&lt;sup&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2008/09/08/45-paquet-debian-et-qt#pnote-36-2&quot; id=&quot;rev-pnote-36-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;

&lt;pre class=&quot;bash&quot;&gt;&lt;span style=&quot;color: #808080; font-style: italic;&quot;&gt;#!/usr/bin/make -f&lt;/span&gt;
include &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;share&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;cdbs&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;rules&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debhelper.mk
include &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;share&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;cdbs&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #000000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;class&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;qmake.mk
&lt;span style=&quot;color: #007800;&quot;&gt;QMAKE=&lt;/span&gt;qmake-qt4 project.pro
&amp;nbsp;
&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;install&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx::
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;mkdir&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;mkdir&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;bin&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cp&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;bin&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cp&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinxprojectwizard&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinxprojectwizard $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;bin&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
&amp;nbsp;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;mkdir&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;lib&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cp&lt;/span&gt; -a $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;libxinx&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;libsharedxinx&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;*&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;lib&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cp&lt;/span&gt; -a $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;components&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;libxinxcmp&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;*&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;lib&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
&amp;nbsp;
&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;install&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx-doc::
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;mkdir&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx-doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;mkdir&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx-doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;share&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;mkdir&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx-doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;share&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;mkdir&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx-doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;share&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx-doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;cp&lt;/span&gt; -a $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/*&lt;/span&gt; $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx-doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;share&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx-doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;
	&lt;span style=&quot;color: #c20cb9; font-weight: bold;&quot;&gt;rm&lt;/span&gt; -rf $&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#40;&lt;/span&gt;CURDIR&lt;span style=&quot;color: #7a0874; font-weight: bold;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;debian&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx-doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;usr&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;share&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;xinx-doc&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;html&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;.svn&lt;/pre&gt;


&lt;p&gt;Ici on génère deux paquets, le paquet binaire et la documentation.&lt;/p&gt;


&lt;h4&gt;Génération du paquet&lt;/h4&gt;


&lt;h5&gt;Mise à jour des informations du paquets&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Pour modifier le changelog sur la version courante&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;dch -a&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;Pour créer une nouvelle version&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;dch -i&lt;/pre&gt;


&lt;h5&gt;Création du paquet&lt;/h5&gt;


&lt;pre&gt;dpkg-buildpackage -rfakeroot&lt;/pre&gt;


&lt;p&gt;L'application va signer le paquet s'il trouve la clé assignée à l'utilisateur.&lt;/p&gt;


&lt;h3&gt;Création d'un dépôt&lt;/h3&gt;


&lt;p&gt;Après avoir créé des paquets, il peut-être intéressant de créer un dépôt pour installer ces paquets. voici la marche à suivre pour créer le dépôt.&lt;/p&gt;


&lt;p&gt;Sur votre serveur internet (nous ne parlerons pas ici de la mise à disposition sur Internet), vous allez devoir créer un dossier contenant votre référentiel (exemple&amp;nbsp;: &lt;code&gt;/www/sites/apt&lt;/code&gt;).&lt;/p&gt;


&lt;h4&gt;Création de l'arborescence&lt;/h4&gt;


&lt;p&gt;Vous allez devoir créer une arborescence comme suite&amp;nbsp;:&lt;/p&gt;


&lt;pre&gt;dists
   +- main 
       |- binary-i386
       |- binary-amd64
       +- source&lt;/pre&gt;


&lt;p&gt;Parmi les fichiers générés vous allez devoir mettre le fichier &lt;code&gt;.deb&lt;/code&gt; dans le dossier binaire, et les fichiers &lt;code&gt;.changes&lt;/code&gt;, &lt;code&gt;.dsc&lt;/code&gt;, et &lt;code&gt;.tar.gz&lt;/code&gt; dans le dossier &lt;code&gt;source&lt;/code&gt;.&lt;/p&gt;


&lt;h4&gt;Création d'un fichier apt-ftparchive.conf&lt;/h4&gt;


&lt;p&gt;Nous allons commencer par créer un fichier &lt;code&gt;apt-ftparchive.conf&lt;/code&gt;  dans lequel nous allons définir les fichiers à créer.&lt;/p&gt;

&lt;pre&gt;
Dir {
	ArchiveDir &amp;quot;.&amp;quot;;
	CacheDir &amp;quot;.&amp;quot;;
};

Tree &amp;quot;dists/unstable&amp;quot; {
	Sections &amp;quot;main&amp;quot;;
	Architectures &amp;quot;amd64 source&amp;quot;;
};

BinDirectory &amp;quot;dists/unstable/main/binary-amd64&amp;quot; {
	Packages &amp;quot;dists/unstable/main/binary-amd64/Packages&amp;quot;;
	Contents &amp;quot;dists/unstable/Contents-amd64&amp;quot;;
	SrcPackages &amp;quot;dists/unstable/main/source/Sources&amp;quot;;
};
&lt;/pre&gt;


&lt;h4&gt;Création du fichier apt-unstable-release.conf&lt;/h4&gt;

&lt;pre&gt;
APT::FTPArchive::Release::Origin	&amp;quot;shadoware.org&amp;quot;;
APT::FTPArchive::Release::Label		&amp;quot;shadoware.org&amp;quot;;
APT::FTPArchive::Release::Suite		&amp;quot;unstable&amp;quot;;
APT::FTPArchive::Release::Codename	&amp;quot;unstable&amp;quot;;
APT::FTPArchive::Release::Architectures	&amp;quot;amd64 source&amp;quot;;
APT::FTPArchive::Release::Components	&amp;quot;main&amp;quot;;
APT::FTPArchive::Release::Description	&amp;quot;Paquets du site Shadoware.Org&amp;quot;;
&lt;/pre&gt;


&lt;h4&gt;Création du fichier update-archive.sh&lt;/h4&gt;


&lt;p&gt;Ce fichier est celui que l'on doit exécuter pour la génération du contenu de l'arborescence&amp;nbsp;:&lt;/p&gt;

&lt;pre class=&quot;bash&quot;&gt;apt-ftparchive generate apt-ftparchive.conf
apt-ftparchive -c apt-unstable-release.conf release dists&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;unstable &lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;&amp;gt;&lt;/span&gt; dists&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;unstable&lt;span style=&quot;color: #000000; font-weight: bold;&quot;&gt;/&lt;/span&gt;Release&lt;/pre&gt;
&lt;div class=&quot;footnotes&quot;&gt;&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2008/09/08/45-paquet-debian-et-qt#rev-pnote-36-1&quot; id=&quot;pnote-36-1&quot;&gt;1&lt;/a&gt;] D'autres cibles sont disponibles (cf la doc de &lt;code&gt;cdbs&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://www.shadoware.org/post/2008/09/08/45-paquet-debian-et-qt#rev-pnote-36-2&quot; id=&quot;pnote-36-2&quot;&gt;2&lt;/a&gt;] A cause d'un bogue, ou d'une mauvaise utilisation, la variable &lt;code&gt;$(DEB_DESTDIR)&lt;/code&gt; ne me ramenait pas la bonne valeur, je l'ai donc remplacée par &lt;code&gt;$(CURDIR)/debian&lt;/code&gt;.&lt;/p&gt;&lt;/div&gt;
</description>
    
    
    
          <comments>http://www.shadoware.org/post/2008/09/08/45-paquet-debian-et-qt#comment-form</comments>
      <wfw:comment>http://www.shadoware.org/post/2008/09/08/45-paquet-debian-et-qt#comment-form</wfw:comment>
      <wfw:commentRss>http://www.shadoware.org/feed/atom/comments/36</wfw:commentRss>
      </item>
    
</channel>
</rss>
