123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
- <meta name="author" content="Emanuel Eichhammer" />
- <meta name="copyright" content="(C) 2013-2015 Emanuel Eichhammer" />
- <title>Using QCustomPlot with special Qt define flags</title>
- <link href="qt.css" rel="stylesheet" type="text/css"/>
- </head>
- <body>
- <div id="top">
- <a class="headerLink" href="index.html">Main Page</a> ·
- <a class="headerLink" href="classoverview.html">Class Overview</a> ·
- <a class="headerLink" href="hierarchy.html">Hierarchy</a> ·
- <a class="headerLink" href="annotated.html">All Classes</a> ·
- <a class="headerLink" href="pages.html">Special Pages</a>
- <!-- Generated by Doxygen 1.8.6 -->
- </div><!-- top -->
- <div class="header">
- <div class="headertitle">
- <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>
- </div><!--header-->
- <div class="contents">
- <div class="textblock"><h1><a class="anchor" id="specialqtflags-noascii"></a>
- Safe string casts with QT_NO_CAST_FROM_ASCII and QT_NO_CAST_TO_ASCII</h1>
- <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>
- <h1><a class="anchor" id="specialqtflags-nokeywords"></a>
- Avoiding Qt specific language extensions with QT_NO_KEYWORDS</h1>
- <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>
- <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>
- <table class="doxtable">
- <tr>
- <td style="text-align:center;font-weight:bold">Search pattern</td><td style="text-align:center;font-weight:bold">Replace with </td></tr>
- <tr>
- <td><code>(^|[^\a_])emit\s</code></td><td><code>\1Q_EMIT </code>—<span style="font-size:0.9em">note the trailing space</span> </td></tr>
- <tr>
- <td><code>^( *)signals:</code></td><td><code>\1Q_SIGNALS:</code> </td></tr>
- <tr>
- <td><code>(^|[^\a_])foreach( *)\(</code></td><td><code>\1Q_FOREACH\2(</code> </td></tr>
- </table>
- <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>
- <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>
- <div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno"> 1</span> <span class="comment">#!/usr/bin/env python</span></div>
- <div class="line"><a name="l00002"></a><span class="lineno"> 2</span> </div>
- <div class="line"><a name="l00003"></a><span class="lineno"> 3</span> <span class="comment"># This script is used to make the amalgamated sources qcustomplot.h/.cpp compatible</span></div>
- <div class="line"><a name="l00004"></a><span class="lineno"> 4</span> <span class="comment"># to compiles with flag QT_NO_KEYWORDS set. It applies the following regular expression replacements</span></div>
- <div class="line"><a name="l00005"></a><span class="lineno"> 5</span> <span class="comment">#</span></div>
- <div class="line"><a name="l00006"></a><span class="lineno"> 6</span> <span class="comment"># (^|[^\a_])emit\s -> \1Q_EMIT </span></div>
- <div class="line"><a name="l00007"></a><span class="lineno"> 7</span> <span class="comment"># ^( *)signals: -> \1Q_SIGNALS:</span></div>
- <div class="line"><a name="l00008"></a><span class="lineno"> 8</span> <span class="comment"># (^|[^\a_])foreach( *)\( -> \1Q_FOREACH\2(</span></div>
- <div class="line"><a name="l00009"></a><span class="lineno"> 9</span> <span class="comment">#</span></div>
- <div class="line"><a name="l00010"></a><span class="lineno"> 10</span> <span class="comment"># to the files whose filenames are passed on the command line. The replacement can in principle be</span></div>
- <div class="line"><a name="l00011"></a><span class="lineno"> 11</span> <span class="comment"># applied to the sources with any other tool or editor that knows regular expressions.</span></div>
- <div class="line"><a name="l00012"></a><span class="lineno"> 12</span> <span class="comment">#</span></div>
- <div class="line"><a name="l00013"></a><span class="lineno"> 13</span> <span class="comment"># To test: Copy freshly amalgamated qcustomplot.h/.cpp files into this directory, call this script on</span></div>
- <div class="line"><a name="l00014"></a><span class="lineno"> 14</span> <span class="comment"># them, and then run qmake; make</span></div>
- <div class="line"><a name="l00015"></a><span class="lineno"> 15</span> </div>
- <div class="line"><a name="l00016"></a><span class="lineno"> 16</span> <span class="keyword">import</span> os, sys, re</div>
- <div class="line"><a name="l00017"></a><span class="lineno"> 17</span> </div>
- <div class="line"><a name="l00018"></a><span class="lineno"> 18</span> baseDir = sys.path[0];</div>
- <div class="line"><a name="l00019"></a><span class="lineno"> 19</span> os.chdir(baseDir) <span class="comment"># change current working dir to script dir</span></div>
- <div class="line"><a name="l00020"></a><span class="lineno"> 20</span>  </div>
- <div class="line"><a name="l00021"></a><span class="lineno"> 21</span> <span class="keyword">def </span>performKeywordReplacement(filename):</div>
- <div class="line"><a name="l00022"></a><span class="lineno"> 22</span>  <span class="keywordflow">print</span> <span class="stringliteral">"making '"</span>+filename+<span class="stringliteral">"' no-keywords-compatible..."</span></div>
- <div class="line"><a name="l00023"></a><span class="lineno"> 23</span>  patterns = []</div>
- <div class="line"><a name="l00024"></a><span class="lineno"> 24</span>  patterns.append((re.compile(<span class="stringliteral">"(^|[^\\a_])emit\\s"</span>), <span class="stringliteral">"\\1Q_EMIT "</span>))</div>
- <div class="line"><a name="l00025"></a><span class="lineno"> 25</span>  patterns.append((re.compile(<span class="stringliteral">"^( *)signals:"</span>), <span class="stringliteral">"\\1Q_SIGNALS:"</span>))</div>
- <div class="line"><a name="l00026"></a><span class="lineno"> 26</span>  patterns.append((re.compile(<span class="stringliteral">"(^|[^\\a_])foreach( *)\\("</span>), <span class="stringliteral">"\\1Q_FOREACH\\2("</span>))</div>
- <div class="line"><a name="l00027"></a><span class="lineno"> 27</span>  inFile = open(filename)</div>
- <div class="line"><a name="l00028"></a><span class="lineno"> 28</span>  outFilename = filename + <span class="stringliteral">".tmp"</span></div>
- <div class="line"><a name="l00029"></a><span class="lineno"> 29</span>  outFile = open(outFilename, <span class="stringliteral">"w"</span>)</div>
- <div class="line"><a name="l00030"></a><span class="lineno"> 30</span>  <span class="keywordflow">for</span> line <span class="keywordflow">in</span> inFile:</div>
- <div class="line"><a name="l00031"></a><span class="lineno"> 31</span>  <span class="keywordflow">for</span> patt <span class="keywordflow">in</span> patterns:</div>
- <div class="line"><a name="l00032"></a><span class="lineno"> 32</span>  line = re.sub(patt[0], patt[1], line)</div>
- <div class="line"><a name="l00033"></a><span class="lineno"> 33</span>  outFile.write(line)</div>
- <div class="line"><a name="l00034"></a><span class="lineno"> 34</span>  outFile.close()</div>
- <div class="line"><a name="l00035"></a><span class="lineno"> 35</span>  inFile.close()</div>
- <div class="line"><a name="l00036"></a><span class="lineno"> 36</span>  os.remove(filename)</div>
- <div class="line"><a name="l00037"></a><span class="lineno"> 37</span>  os.rename(outFilename, filename)</div>
- <div class="line"><a name="l00038"></a><span class="lineno"> 38</span>  </div>
- <div class="line"><a name="l00039"></a><span class="lineno"> 39</span> <span class="keywordflow">for</span> filename <span class="keywordflow">in</span> sys.argv[1:]:</div>
- <div class="line"><a name="l00040"></a><span class="lineno"> 40</span>  <span class="keywordflow">if</span> <span class="keywordflow">not</span> os.path.isfile(filename):</div>
- <div class="line"><a name="l00041"></a><span class="lineno"> 41</span>  <span class="keywordflow">print</span> <span class="stringliteral">"file '"</span>+filename+<span class="stringliteral">"' not found"</span></div>
- <div class="line"><a name="l00042"></a><span class="lineno"> 42</span>  sys.exit(-1)</div>
- <div class="line"><a name="l00043"></a><span class="lineno"> 43</span>  performKeywordReplacement(filename);</div>
- <div class="line"><a name="l00044"></a><span class="lineno"> 44</span> </div>
- </div><!-- fragment --> </div></div><!-- contents -->
- </body>
- </html>
|