specialqtflags.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
  5. <meta name="author" content="Emanuel Eichhammer" />
  6. <meta name="copyright" content="(C) 2013-2015 Emanuel Eichhammer" />
  7. <title>Using QCustomPlot with special Qt define flags</title>
  8. <link href="qt.css" rel="stylesheet" type="text/css"/>
  9. </head>
  10. <body>
  11. <div id="top">
  12. <a class="headerLink" href="index.html">Main Page</a> &middot;
  13. <a class="headerLink" href="classoverview.html">Class Overview</a> &middot;
  14. <a class="headerLink" href="hierarchy.html">Hierarchy</a> &middot;
  15. <a class="headerLink" href="annotated.html">All Classes</a> &middot;
  16. <a class="headerLink" href="pages.html">Special Pages</a>
  17. <!-- Generated by Doxygen 1.8.6 -->
  18. </div><!-- top -->
  19. <div class="header">
  20. <div class="headertitle">
  21. <div class="title">Using <a class="el" href="classQCustomPlot.html" title="The central class of the library. This is the QWidget which displays the plot and interacts with the ...">QCustomPlot</a> with special Qt define flags </div> </div>
  22. </div><!--header-->
  23. <div class="contents">
  24. <div class="textblock"><h1><a class="anchor" id="specialqtflags-noascii"></a>
  25. Safe string casts with QT_NO_CAST_FROM_ASCII and QT_NO_CAST_TO_ASCII</h1>
  26. <p><a class="el" href="classQCustomPlot.html" title="The central class of the library. This is the QWidget which displays the plot and interacts with the ...">QCustomPlot</a> can be compiled with the special Qt flags <code>QT_NO_CAST_FROM_ASCII</code> and <code>QT_NO_CAST_TO_ASCII</code> out of the box. All strings used in the library are wrapped in QLatin1String to avoid casts from string/char literals. So if your project requires the added cast safety from these Qt flags, you can use QCustomPlot without any changes necessary.</p>
  27. <h1><a class="anchor" id="specialqtflags-nokeywords"></a>
  28. Avoiding Qt specific language extensions with QT_NO_KEYWORDS</h1>
  29. <p>QCustomPlot is a library based on and <em>written in</em> Qt. This means it uses Qt keywords such as <code>foreach</code>, <code>signal</code>, <code>slot</code> and <code>emit</code> for improved legibility.</p>
  30. <p>However, some projects wish to disable these Qt specific language extensions by using the define flag <code>QT_NO_KEYWORDS</code>. To compile QCustomPlot with that flag set, it is necessary to replace all occurances of Qt keywords. This is easily done with QCustomPlot code, by using the following regular expression replacements:</p>
  31. <table class="doxtable">
  32. <tr>
  33. <td style="text-align:center;font-weight:bold">Search pattern</td><td style="text-align:center;font-weight:bold">Replace with </td></tr>
  34. <tr>
  35. <td><code>(^|[^\a_])emit\s</code></td><td><code>\1Q_EMIT&#160;</code>&mdash;<span style="font-size:0.9em">note the trailing space</span> </td></tr>
  36. <tr>
  37. <td><code>^( *)signals:</code></td><td><code>\1Q_SIGNALS:</code> </td></tr>
  38. <tr>
  39. <td><code>(^|[^\a_])foreach( *)\(</code></td><td><code>\1Q_FOREACH\2(</code> </td></tr>
  40. </table>
  41. <p>QCustomPlot code is written with these replacements in mind. They always work and catch all occurances of used Qt keywords. Their functioning is also automatically tested upon every release.</p>
  42. <p>The regular expressions can be applied in any IDE/Editor that supports them, including QtCreator itself. Below is a python script that is used to test the functioning of the regular expressions. It takes the names of the files to process (e.g. <em>qcustomplot.cpp qcustomplot.h</em>) from the command line and performs the replacement on them.</p>
  43. <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span>&#160;<span class="comment">#!/usr/bin/env python</span></div>
  44. <div class="line"><a name="l00002"></a><span class="lineno"> 2</span>&#160;</div>
  45. <div class="line"><a name="l00003"></a><span class="lineno"> 3</span>&#160;<span class="comment"># This script is used to make the amalgamated sources qcustomplot.h/.cpp compatible</span></div>
  46. <div class="line"><a name="l00004"></a><span class="lineno"> 4</span>&#160;<span class="comment"># to compiles with flag QT_NO_KEYWORDS set. It applies the following regular expression replacements</span></div>
  47. <div class="line"><a name="l00005"></a><span class="lineno"> 5</span>&#160;<span class="comment">#</span></div>
  48. <div class="line"><a name="l00006"></a><span class="lineno"> 6</span>&#160;<span class="comment"># (^|[^\a_])emit\s -&gt; \1Q_EMIT </span></div>
  49. <div class="line"><a name="l00007"></a><span class="lineno"> 7</span>&#160;<span class="comment"># ^( *)signals: -&gt; \1Q_SIGNALS:</span></div>
  50. <div class="line"><a name="l00008"></a><span class="lineno"> 8</span>&#160;<span class="comment"># (^|[^\a_])foreach( *)\( -&gt; \1Q_FOREACH\2(</span></div>
  51. <div class="line"><a name="l00009"></a><span class="lineno"> 9</span>&#160;<span class="comment">#</span></div>
  52. <div class="line"><a name="l00010"></a><span class="lineno"> 10</span>&#160;<span class="comment"># to the files whose filenames are passed on the command line. The replacement can in principle be</span></div>
  53. <div class="line"><a name="l00011"></a><span class="lineno"> 11</span>&#160;<span class="comment"># applied to the sources with any other tool or editor that knows regular expressions.</span></div>
  54. <div class="line"><a name="l00012"></a><span class="lineno"> 12</span>&#160;<span class="comment">#</span></div>
  55. <div class="line"><a name="l00013"></a><span class="lineno"> 13</span>&#160;<span class="comment"># To test: Copy freshly amalgamated qcustomplot.h/.cpp files into this directory, call this script on</span></div>
  56. <div class="line"><a name="l00014"></a><span class="lineno"> 14</span>&#160;<span class="comment"># them, and then run qmake; make</span></div>
  57. <div class="line"><a name="l00015"></a><span class="lineno"> 15</span>&#160;</div>
  58. <div class="line"><a name="l00016"></a><span class="lineno"> 16</span>&#160;<span class="keyword">import</span> os, sys, re</div>
  59. <div class="line"><a name="l00017"></a><span class="lineno"> 17</span>&#160;</div>
  60. <div class="line"><a name="l00018"></a><span class="lineno"> 18</span>&#160;baseDir = sys.path[0];</div>
  61. <div class="line"><a name="l00019"></a><span class="lineno"> 19</span>&#160;os.chdir(baseDir) <span class="comment"># change current working dir to script dir</span></div>
  62. <div class="line"><a name="l00020"></a><span class="lineno"> 20</span>&#160; </div>
  63. <div class="line"><a name="l00021"></a><span class="lineno"> 21</span>&#160;<span class="keyword">def </span>performKeywordReplacement(filename):</div>
  64. <div class="line"><a name="l00022"></a><span class="lineno"> 22</span>&#160; <span class="keywordflow">print</span> <span class="stringliteral">&quot;making &#39;&quot;</span>+filename+<span class="stringliteral">&quot;&#39; no-keywords-compatible...&quot;</span></div>
  65. <div class="line"><a name="l00023"></a><span class="lineno"> 23</span>&#160; patterns = []</div>
  66. <div class="line"><a name="l00024"></a><span class="lineno"> 24</span>&#160; patterns.append((re.compile(<span class="stringliteral">&quot;(^|[^\\a_])emit\\s&quot;</span>), <span class="stringliteral">&quot;\\1Q_EMIT &quot;</span>))</div>
  67. <div class="line"><a name="l00025"></a><span class="lineno"> 25</span>&#160; patterns.append((re.compile(<span class="stringliteral">&quot;^( *)signals:&quot;</span>), <span class="stringliteral">&quot;\\1Q_SIGNALS:&quot;</span>))</div>
  68. <div class="line"><a name="l00026"></a><span class="lineno"> 26</span>&#160; patterns.append((re.compile(<span class="stringliteral">&quot;(^|[^\\a_])foreach( *)\\(&quot;</span>), <span class="stringliteral">&quot;\\1Q_FOREACH\\2(&quot;</span>))</div>
  69. <div class="line"><a name="l00027"></a><span class="lineno"> 27</span>&#160; inFile = open(filename)</div>
  70. <div class="line"><a name="l00028"></a><span class="lineno"> 28</span>&#160; outFilename = filename + <span class="stringliteral">&quot;.tmp&quot;</span></div>
  71. <div class="line"><a name="l00029"></a><span class="lineno"> 29</span>&#160; outFile = open(outFilename, <span class="stringliteral">&quot;w&quot;</span>)</div>
  72. <div class="line"><a name="l00030"></a><span class="lineno"> 30</span>&#160; <span class="keywordflow">for</span> line <span class="keywordflow">in</span> inFile:</div>
  73. <div class="line"><a name="l00031"></a><span class="lineno"> 31</span>&#160; <span class="keywordflow">for</span> patt <span class="keywordflow">in</span> patterns:</div>
  74. <div class="line"><a name="l00032"></a><span class="lineno"> 32</span>&#160; line = re.sub(patt[0], patt[1], line)</div>
  75. <div class="line"><a name="l00033"></a><span class="lineno"> 33</span>&#160; outFile.write(line)</div>
  76. <div class="line"><a name="l00034"></a><span class="lineno"> 34</span>&#160; outFile.close()</div>
  77. <div class="line"><a name="l00035"></a><span class="lineno"> 35</span>&#160; inFile.close()</div>
  78. <div class="line"><a name="l00036"></a><span class="lineno"> 36</span>&#160; os.remove(filename)</div>
  79. <div class="line"><a name="l00037"></a><span class="lineno"> 37</span>&#160; os.rename(outFilename, filename)</div>
  80. <div class="line"><a name="l00038"></a><span class="lineno"> 38</span>&#160; </div>
  81. <div class="line"><a name="l00039"></a><span class="lineno"> 39</span>&#160;<span class="keywordflow">for</span> filename <span class="keywordflow">in</span> sys.argv[1:]:</div>
  82. <div class="line"><a name="l00040"></a><span class="lineno"> 40</span>&#160; <span class="keywordflow">if</span> <span class="keywordflow">not</span> os.path.isfile(filename):</div>
  83. <div class="line"><a name="l00041"></a><span class="lineno"> 41</span>&#160; <span class="keywordflow">print</span> <span class="stringliteral">&quot;file &#39;&quot;</span>+filename+<span class="stringliteral">&quot;&#39; not found&quot;</span></div>
  84. <div class="line"><a name="l00042"></a><span class="lineno"> 42</span>&#160; sys.exit(-1)</div>
  85. <div class="line"><a name="l00043"></a><span class="lineno"> 43</span>&#160; performKeywordReplacement(filename);</div>
  86. <div class="line"><a name="l00044"></a><span class="lineno"> 44</span>&#160;</div>
  87. </div><!-- fragment --> </div></div><!-- contents -->
  88. </body>
  89. </html>