Configuring CMake for QT development

I know how much its confusing  when you need to configure some new technologies for the first time.Its just , you know how everything works and connects ,but no idea about those little tricky stuff.

In this blog post I am going to write about how to configure QT  with CMake. I have uploaded a sample CMake project which will helpful for much clearer understanding.

Download sample project from : http://dl.dropbox.com/u/17399055/cmake-qt-sample.zip

Key points :

  • QT comes with its own build tool called “qmake”.So, for Cmake to utilize that , you need to add the folder where “qmake” locates to your PATH environment variable.

Here are important snippets from the CakeList.txt file of  the sample project.

  • FIND_PACKAGE(Qt4 REQUIRED)
  • – This will find QT library folder,header files..etc using `qmake` .

  • INCLUDE(${QT_USE_FILE})
  • – This will include QT related header folders to the make file.

  • QT4_WRAP_CPP(myprj_moh_srcs ${myprj_moh_hdrs})
  • – This compiles QT related class headers with QT MOH compiler and generate standard C++ code.Technically any class which uses QTs signal slots (basically classes which inherits `QObjec`) should be passed to this.

  • QT4_ADD_RESOURCES(myprj_rccs_srcs ${myprj_rccs})
  • – This compiles `QT resource files` and wrap all resources into C++ header.This is useful if you want to embed your resources into the executable.

  • QT4_WRAP_UI(myprj_ui_hdrs ${myprj_uis})
  • – This compiles QT UI files into standard C++ code.QT UI files contain user designed interfaces using QT Creator or QT IDE plugins .(eg- QT Eclipse plugin)

How to covert QT QImage into OpenCV IplImage and vise-versa.

These days,I am working on developing an application for “High Voltage Electron Microscope”.Basically it takes images from “High Voltage Electron Microscope” and do tomographic reconstruction and 3D reconstruction…etc I use QT4 for user interface and and OpenCV for image processing functionalities and OpenGL as core frameworks.

I came up with big obstacle since it utilise many frameworks and have to handle conversion in between many image-data-structures.Eg : QImage in QT4 , IplImage in OpenCV and some custom image data structure for internal algorithms.

I came up with following conversion between QImage to IplImage.Hope it will helpful to someone whose spending hours on google.

IplImage* QImage2IplImage(QImage *qimg)
{

IplImage *imgHeader = cvCreateImageHeader( cvSize(qimg->width(), qimg->height()), IPL_DEPTH_8U, 4);
imgHeader->imageData = (char*) qimg->bits();

uchar* newdata = (uchar*) malloc(sizeof(uchar) * qimg->byteCount());
memcpy(newdata, qimg->bits(), qimg->byteCount());
imgHeader->imageData = (char*) newdata;
//cvClo
return imgHeader;
}

QImage*  IplImage2QImage(IplImage *iplImg)
{
int h = iplImg->height;
int w = iplImg->width;
int channels = iplImg->nChannels;
QImage *qimg = new QImage(w, h, QImage::Format_ARGB32);
char *data = iplImg->imageData;

for (int y = 0; y < h; y++, data += iplImg->widthStep)
{
for (int x = 0; x < w; x++)
{
char r, g, b, a = 0;
if (channels == 1)
{
r = data[x * channels];
g = data[x * channels];
b = data[x * channels];
}
else if (channels == 3 || channels == 4)
{
r = data[x * channels + 2];
g = data[x * channels + 1];
b = data[x * channels];
}

if (channels == 4)
{
a = data[x * channels + 3];
qimg->setPixel(x, y, qRgba(r, g, b, a));
}
else
{
qimg->setPixel(x, y, qRgb(r, g, b));
}
}
}
return qimg;

}