QT app compiled in under Windows performs calculation incorrectly
I have constructed my first piece of software using QT and C++, which is working perfectly when compiled on Ubuntu 11.04 and Mac OS X. When I compile on Windows, I get strange output in my GUI. I have tracked down the problem to an error calculating the paint coordinates. This is the offending calculation:
long x = ((pos-from) *width)/range ;
qDebug() << ***************;
qDebug() << "pos" <<"\t" << pos;
qDebug() << "from" <<"\t" << from;
qDebug() << "width" <<"\t" << width;
qDebug() << "range" <<"\t开发者_运维百科" << range;
qDebug() << "x" <<"\t" << x;
qDebug() << "***************";
And output from Ubuntu:
***************
pos 2500000
from 1
width 1005
range 4411537
x 569
***************
And the output from Windows:
***************
pos 2500000
from 1
width 1574
range 4411537
x -81
***************
Does anybody have an idea why I might be getting different values for x ?
Cheers.
If you are using gcc or similar (mingw) compilers, the long
type is 32 bits on 32 bits platforms and 64 bits on 64 bits platforms. Probably your non-windows "compiles" are on 64 bits platforms. VC++ always considers long
to be 32 bits. You can try using long long int
or __int64
(older VC++) (you only need one of pos, width or range variables to be of that type so that the others are upcasted).
long type in Windows is 32-bit.
You should use long long -type if you want 64-bits len or qint64 when you use Qt.
Your width
value is different in each test, so obviously the result will be different.
精彩评论