Main Page · Class Overview · Hierarchy · All Classes · Special Pages
Using QCustomPlot with special Qt define flags

Safe string casts with QT_NO_CAST_FROM_ASCII and QT_NO_CAST_TO_ASCII

QCustomPlot can be compiled with the special Qt flags QT_NO_CAST_FROM_ASCII and QT_NO_CAST_TO_ASCII 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.

Avoiding Qt specific language extensions with QT_NO_KEYWORDS

QCustomPlot is a library based on and written in Qt. This means it uses Qt keywords such as foreach, signal, slot and emit for improved legibility.

However, some projects wish to disable these Qt specific language extensions by using the define flag QT_NO_KEYWORDS. 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:

Search patternReplace with
(^|[^\a_])emit\s\1Q_EMIT note the trailing space
^( *)signals:\1Q_SIGNALS:
(^|[^\a_])foreach( *)\(\1Q_FOREACH\2(

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.

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. qcustomplot.cpp qcustomplot.h) from the command line and performs the replacement on them.

1 #!/usr/bin/env python
2 
3 # This script is used to make the amalgamated sources qcustomplot.h/.cpp compatible
4 # to compiles with flag QT_NO_KEYWORDS set. It applies the following regular expression replacements
5 #
6 # (^|[^\a_])emit\s -> \1Q_EMIT
7 # ^( *)signals: -> \1Q_SIGNALS:
8 # (^|[^\a_])foreach( *)\( -> \1Q_FOREACH\2(
9 #
10 # to the files whose filenames are passed on the command line. The replacement can in principle be
11 # applied to the sources with any other tool or editor that knows regular expressions.
12 #
13 # To test: Copy freshly amalgamated qcustomplot.h/.cpp files into this directory, call this script on
14 # them, and then run qmake; make
15 
16 import os, sys, re
17 
18 baseDir = sys.path[0];
19 os.chdir(baseDir) # change current working dir to script dir
20 
21 def performKeywordReplacement(filename):
22  print "making '"+filename+"' no-keywords-compatible..."
23  patterns = []
24  patterns.append((re.compile("(^|[^\\a_])emit\\s"), "\\1Q_EMIT "))
25  patterns.append((re.compile("^( *)signals:"), "\\1Q_SIGNALS:"))
26  patterns.append((re.compile("(^|[^\\a_])foreach( *)\\("), "\\1Q_FOREACH\\2("))
27  inFile = open(filename)
28  outFilename = filename + ".tmp"
29  outFile = open(outFilename, "w")
30  for line in inFile:
31  for patt in patterns:
32  line = re.sub(patt[0], patt[1], line)
33  outFile.write(line)
34  outFile.close()
35  inFile.close()
36  os.remove(filename)
37  os.rename(outFilename, filename)
38 
39 for filename in sys.argv[1:]:
40  if not os.path.isfile(filename):
41  print "file '"+filename+"' not found"
42  sys.exit(-1)
43  performKeywordReplacement(filename);
44