MFC to Qt migration part 1

Last Monday, after spending weekend planning the development for my upcoming software, I realize something important. One big challenge in developing the software is GUI, and MFC, being my main GUI framework since my first application is not really developer friendly, and totally a nightmare (just like any other Microsoft developer’s tools, really). And after one month playing with Qt, I realize how easy to develop a GUI with it.

For those who still confused about what I do exactly, this is a screenshot of my MFC based application. It’s an application that can read handwritten character using ICR (Intelligent Character Recognition) engine.

icr reader

ICR-based document reader

I decided to give Qt a go. And after spending one day, creating the base UI , I started to integrate my previous project which mostly, developed using Visual Studio 200 and some of it using linked to MFC runtime. At this stage, I don’t want to rewrite all my libraries in Qt. And I used several C++ libraries in my work such as : boost, poco, pantheios and opencv. I am not aiming for cross-platform now, so I use Qt environment with Microsoft Visual C++ 2008 compiler and I can use my already compiled libraries in the new Qt-based application.

Here is some issue that I encounter during my attempt to integrate Qt with my VC++ libraries and how I fixed it.

  1. sigslot ’emit’ errors. in my projects, I used sigslot a lot. sigslot is a C++ signal/slot library, written to follow Qt signal/slot style. Using sigslot make code cleaner and modular, which is what you want in your code. Since ’emit’ is Qt keyword, all function named ’emit’ in sigslot.h must be replaced. In my case, I replace all ’emit’ words in sigslot.h into ‘semit’.
  2. implicit linking libraries path. boost, poco and pantheios use implicit linking. You don’t have to specify explicitly what lib file you want to use in your project, you just need to include the header in your code and specify the library’s lib folder in linking phase, then it will link automatically to libraries that use used. You have to use keyword QMAKE_LIBDIR in your Qt .pro file to add the path. For example : QMAKE_LIBDIR += $(BOOST_LIB). Note that BOOST_LIB is defined in environment path.
  3. If one of your library dynamically linked to MFC, you need to add MFC definition into qmake system. you can do this by adding DEFINES += _AFXDLL into your .pro file. Additionally, you might need to add DEFINES -= UNICODE, in case you have libraries that not build with unicode character (as in my case, pantheios).
  4. debug and release build. I spent three hours trying to figure out why release build always crash at startup. After googling, modify .pro and rebuild, I finally realized the release build use debug libs. This is how I did it wrong

This is in my .pro

debug {
LIBS += -L$(OPENCV)\lib -lcv210d -lcxcore210d -lhighgui210d -lml210d
}

release {
LIBS += -L$(OPENCV)\lib -lcv210 -lcxcore210 -lhighgui210 -lml210
}

I notice, that if I comment out the release libs part, I still compiled release build successfully, meaning that it uses debug libraries and resulted in crash. After googling, I found the solution. I change my .pro file :

CONFIG += debug_and_release

CONFIG(debug, debug|release) {
LIBS += -L$(OPENCV)\lib -lcv210d -lcxcore210d -lhighgui210d -lml210d
}

CONFIG(release, debug|release) {
LIBS += -L$(OPENCV)\lib -lcv210 -lcxcore210 -lhighgui210 -lml210
}

So to summarize, this is part of my .pro file

DEFINES -= UNICODE
DEFINES += _AFXDLL

CONFIG += debug_and_release

INCLUDEPATH += $(BOOST_INCLUDE)
INCLUDEPATH += $(OPENCV)\include

QMAKE_LIBDIR += $(BOOST_LIB)

CONFIG(debug, debug|release) {
    LIBS += -L$(OPENCV)\lib -lcv210d -lcxcore210d -lhighgui210d -lml210d
}

CONFIG(release, debug|release) {
    LIBS += -L$(OPENCV)\lib -lcv210 -lcxcore210 -lhighgui210 -lml210
}

And now my Qt GUI ready to use all my previous libraries. As you see below, the Qt based app is still far from MFC shown above in term on visual, but I can guarantee, the code is prettier. That’s what important, right ?

Qt based UI

Qt based UI

Some terms :
GUI : Graphical User Interface, and for those who don’t really understand this, it’s everything that you see in your PC or mac is GUI, except the command line.
MFC : Microsoft Foundation Classes, one of main developer framework from Microsoft, before .NET and now, WPF.
Qt : A cross-platform application and UI framework. It has two license, LGPL and commercial.  Developed by trolltech, now owned by Nokia, then Digia for the commercial licensing.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: