I'm receiving an unexpected behavior in my current project.
I use the DICOM library dcmtk
to read information from some dicom files, and Qt
to show the images.
During the information extraction, I have to convert fields of the format "<64bit float>\<64 bit float>" (Dicom Tag PixelSpacing). I split into 2 strings at "\" and convert the strings into a double. So far, everything works fine.
Well, almost: whenever I create a QApplication
object before I convert the strings to doubles, it gives me integers instead of doubles.
The code looks like this:
// Faulty situation
Database db;
QApplication app(&argc, argv);
db.fill_from_source(source); // here i get ints instead of doubles
// Rearrange code and recompile:
Database db;
db.fill_from_source(source); // now it gets me doubles.
QApplication app(&argc, argv);
// The fill function looks like this (simplified)
void Database::fill_from_source(const Source& source){
string s = source.get_pixel_spacing_string();
vector<string> s2 = split(s, "\\");
// get the double, that should not be integers!
double a = stod(s2[0]);
double b = stod(s2[1]);
}
It confuses me even more, that it does work stepping through the code using QtCreator and GDB. However, when I run the executable, I get integers again.
So I tracked the issue down to the stod
operation: I get the right strings out of the DICOM file, but after stod
the numbers after the dot are just truncated. Same behavior with stdlib
's strtod
Does the QApplication
allocation does something with the std::stod
function? Since everything happens while runtime, I do not understand how.
Replacing stod
with QString::toDouble
resolves the issue...
I'am using gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3), GNU ld (GNU Binutils for Ubuntu) 2.24
.
Other code dependencies include Eigen3
, Boost.Python
. The code is built using a CMake project with QtCreator as IDE.
Has anyone an idea where this problem comes from? Is this a Qt bug?
std::stod
behaviour depends on the currently installed C locale.According to cppreference:
As pointed by @peppe in the comments, during
QApplication
's constructionsetlocale(LC_ALL, "");
is called on Unix, thus alteringstd::stod
.You can store the locale and set it back as follows:
EDIT:
After rereading documentation for QCoreApplication, there is a paragraph about locale settings in detailed description:
However @J.Riesmeier provided an interessant answer as one of the DCMTK developers.