Showing posts with label Qt. Show all posts
Showing posts with label Qt. Show all posts

Friday, May 2, 2014

Decimal for QT C++

We all know not to use primitive data type double for any arithmetic calculation involves financial or sensitive data which  rounding errors is not acceptable. In that case, Java has java.math.BigDecimal, C# can use System.Decimal, but there is no Decimal built in support for C++.
However there are a few third party libraries to the rescue.
  1. GNU Multiple Precision Arithmetic Library.
  2. qdecimal
  3. Intel's floating point math library
We are going to focus on qdecimal, which specifically target QT C++ at this post.
Because I use Mingw-32 QT version, the library I download does not compile properly, so instead I include the source code into my project.


#include <QCoreApplication>
#include <QtConcurrent/QtConcurrent>
#include "qdecimal/QDecDouble.hh"
#include "qdecimal/QDecNumber.hh"


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QDecDouble decDouble = 10.00;
    QDecDouble result = decDouble / 90;
    QTextStream(stdout) << result.toString();
    return a.exec();
}


that's it, hop it helps.

Sunday, April 27, 2014

QT Static Library

It is convenient to have a static library linked to your QT application, not only shrinks down the total deliverable size, but also makes the deploy easier. QT official forum recently published a post on how to compile the library, and here is my experience on executing it under Windows 7.

Things you need.

  1. QT Source Code
  2. 7-zip
  3. QT-mingw-32 version, download it here
  4. Direct X SDK
  5. Windows Power Shell, if you don't already have it.
  6. Power Shell Build Script

Steps to build the library

  1. Install QT-Mingw first, assume it is installed at c:\tools\QT
  2. Install 7 zip
  3. Install Direct X SDK, 
  4. Install Power Shell.
  5. Place qt-everywhere-opensource-src-5.2.x.x.7z under c:\tools
  6. Place the power shell script under c:\tools
  7. Edit the build script as follows.

[CmdletBinding()]
param(
    $QtSrcUrl = "C:/tools/qt-everywhere-opensource-src-5.2.1.7z",
    $QtStaticDir = "C:\tools\Qt\Static",
    $QtVersion = "",
    $MingwDir = "C:\tools\Qt\Qt5.2.1\Tools\mingw48_32",
    [switch]$NoPause = $false
)

Lastly, run power shell script, by opening up a power shell window, and type c:\tools\windows-build-qt-static.ps1

The compilation will take a while (about two hours for my Intel I5 machine), and stay tune for part two for static library linking.

Hope it helps.

Thursday, April 24, 2014

Qt::Concurrent, Multi-Thread under QT programming

Concurrency was always pain in the ass for my short programming career, however under QT C++, there is a easy trick to complete this. It never been this easy, hence I want to share it.
QTConcurrent is what you need, and here is a short example.




#include <QCoreApplication>
#include <QtConcurrent/QtConcurrent>

int plusFunction(int number);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int number = 0;
    QFuture<int> future = QtConcurrent::run(plusFunction, number);
    QTextStream(stdout) << future.result() << endl;
    return a.exec();
}


int plusFunction(int number) {
    return number + 1;
}