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

Monday, July 25, 2011

C++ 0x on Mac

Introduction
I was trying to convert my MSVC project into Qt Project so that it may compiled both on Windows and on Mac. But the problem is I used some of C++ 0x features in the project such as auto keyword, r-value reference and move constructor. Because my Qt on Windows uses MSVC 2010 compiler, there was no problem compiling it but the problem was on Mac which doesn't have C++ 0x feature yet.

What is special on Mac?
gcc 4.4 or later versions support C++ 0x standard but the version of gcc included in XCode 4.0 is still 4.2. Because I didn't have an excellent Mac specific knowledge, was thinking that it would be very easy to upgrade gcc into 4.4 or higher so that I may use C++ 0x standard on Mac. But it was not! The gcc compiler provided with XCode is not a normal gcc compiler but a special version which has been modified by Apple. Here I introduce how to use C++ 0x features with Qt Creator. May be I can try it also on XCode but I gave up because got a very dangerous feeling about switching XCode's default compiler.

Installation of gcc4.5
First, I installed gcc4.5 on my Mac by using of fink command 'fink install gcc45'. The command is very simple but the installation process took about 5 hours! Please note that if you are about to install a new version of gcc compiler via fink command on Friday afternoon and you got an date that night, you should give up one of beween date and gcc installation.

Qt Configuration
Now, launch your Qt Creator to modify the setting so that you may use gcc 4.5 compiler on your Qt Creator.
1. add gcc 4.5 into Tool Chains

Open preference window then choose Tool Chains on the tab on the left side of the preference window.
Click "add" button and choose "GCC" on the popup menu. Rename it into a new name such as "GCC4.5" to avoid confusing between other GCC compilers by double clicking the name.
Then set the compiler path , debugger, and ABI values. The gcc4.5 binary is usually located in /sw/bin/ and  its name is g++-4 or G++-4.5.
2. Create qmakespec for gcc4.x
There are differences between Apple's gcc compiler and gnu version of gcc compiler. Among these differences, in this time, -arch and -Xarch flag are the things to be cared. Qt's qmake predefined specification data is using -arch and Xarch flag to specify architecture values such as intel 32bit, intel 64bit, PPC 32bit, and PPC 64bit. We should make it work with our gcc compiler by switching these flags into -m32 or -m64.
Here is an example.
Go to your mkspecs directory of Qt. You can find out the location by checking Qt4 tab on Preferences.

Then make a copy of macx-g++ with a new name such as macx-gnu-g++. When you open qmake.conf file, you can see a line like below at the end of file.
include(../common/mac-g++.conf)
Most of environment values for qmakeconfig are stored in the file, ../common/mac-g++.conf. Let's change it into a new file name such as ../common/mac-gnu-g++.conf.
Go to common directory then copy mac-g++.conf file with a new name mac-gnu-g++.conf.

QMAKE_CFLAGS_X86_64     += -m64 -mmacosx-version-min=10.5
QMAKE_OBJECTIVE_CFLAGS_X86_64  += -m64 -mmacosx-version-min=10.5
....
QMAKE_CXXFLAGS_X86_64   += -m64  -mmacosx-version-min=10.5
QMAKE_CXXFLAGS_X86      += -m32

QMAKE_CC         = gcc-4
QMAKE_CXX        = g++-4

3. Modify Build Setting
Now, almost done! Select "Projects" tab on left side of the Qt Creator, then you will see "Build Settings". Go to the second section, "Build Steps" then put the value "-spec mac-gnu-g++" into "Additional Arguments".

Finished!
In my case, I can compile most of Qt projects by doing above things. But depending on the projects, especially depended libraries, there could be some more things to do.
Thank you.

Saturday, July 16, 2011

An Implementation of inter-thread communication between UI thread and Worker threads with Qt

Introduction
The basic concept of this implementation is explained on my previous post.

Architecture

Implementation

Event Object
class QtITCEvent:public QEvent
{
public:
    QtITCEvent(BaseEventHandler& handler, int val)
        :QEvent(IPC_THREAD_EVENT),
        value(val),
        handler(receiver)
    {

    }
    int getValue() // NS
    {
        return value;
    }

    CPortalNetThreadChanger& getHandler() // NS
    {
        return handler;
    }

private:
    int value;
    BaseEventHandler& handler;
};

class QtEventHandler:public QObject, BaseEventHandler
{
public:
    static BaseEventHandler& getInstance() // NS
    {
        static QtEventHandler instance;
        return &instance;
    }

protected:
    virtual bool event ( QEvent * e ) // NS
    {
        if (e->type() == IPC_THREAD_EVENT)
        {
            QtITCEvent* myEvent = dynamic_cast(e);
            if (!myEvent)
            {
                return false;
            }
            myEvent->getHandler().onHandleEvent(myEvent->getValue());
            return true;
        }
        return false;

    }
private:
    QtEventHandler()
    {

    }
    QtEventHandler(QtEventHandler&);
    void operator=(QtEventHandler&);


};

QtEventTrigger::QtEventTrigger()
{
    if (IPC_THREAD_EVENT == QEvent::None)
    {
        IPC_THREAD_EVENT = static_cast(QEvent::registerEventType());
    }
}

QtEventTrigger::~QtEventTrigger()
{

}

void QtEventTrigger::fire(int value) // NS
{
    QtITCEvent * event = new QtITCEvent(this, value);
    QCoreApplication::postEvent(QtEventHandler::getInstance(), event);
}

Sunday, July 10, 2011

Cross-platform C++ libraries

Introduction
There are some cross-platform C++ libraries and some of them contain UI Frameworks.
I introduce these libraries and my experiences of them here.

Cross-platform C++ libraries with UI Frameworks
There are two cross-platform C++ libraries which have UI Frameworks as I know. Both of these two libraries are implemented based on adapter pattern.

Qt by Nokia
Qt (pronounced officially as cute /ˈkjuːt/, though often incorrectly as Q.T. /ˈkjuːˈtiː/) is a cross-platform application framework that is widely used for developing application software with a graphical user interface (GUI) (in which cases Qt is classified as a widget toolkit), and also used for developing non-GUI programs such as command-line tools and consoles for servers. Qt is most notably used in Autodesk Maya, Google Earth, KDE, Adobe Photoshop Elements, OPIE, Skype, VLC media player, VirtualBox, and Mathematica, and by the European Space Agency, Siemens, Volvo, Walt Disney Animation Studios, Samsung, Philips, and Panasonic. - from Wikipedia

Qt SDK combines the Qt framework with tools designed to streamline the creation of applications for Symbian and Maemo, MeeGo (Nokia N9) in addition to desktop platforms, such as Microsoft Windows, Mac OS X, and Linux.

I want to mention two most impressed things while I've been using Qt: One is well-designed architecture for cross-platform UI framework, the other is Qt Creator, powerful cross-platform integrated development environment, including UI designer tools and on-device debugging. I really like Qt Creator and recommend it even though it crashes sometimes :). If you are sick of slow response of MSVS or XCode, I'd like to advice you to have a chance of using Qt Creator. It really responses very quick and is easy to add source code files to project and to modify project setting. Qt Creator use gcc compiler by default with Mac and Linux, and MinGW with Windows but you can change it into MSVC compiler or Intel compiler.

Qt is available under GPL v3, LGPL v2 and a commercial license. Learn more about licenses here.

wxWidgets
wxWidgets (formerly wxWindows) is a widget toolkit for creating graphical user interfaces (GUIs) for cross-platform applications. wxWidgets enables a program's GUI code to compile and run on several computer platforms with minimal or no code changes. It covers systems such as Microsoft Windows, Mac OS X (Carbon and Cocoa), iOS (Cocoa Touch), Linux/Unix (X11, Motif, and GTK+), OpenVMS, OS/2 and AmigaOS. A version for embedded systems is under development.  - from Wikipedia

wxWidgets provides MFC like classes, thus it is a good option for C++ developers who are familier to MFC or WTL. But there are few things to know attentively. UI classes in wxWidgets are usually using self destruction when their windows are destroyed. If you use these UIs without having enough understanding of their self destruction mechanisms, it will bring serious problems such as abnormal termination by double deletion of the object. wxWidgets is still under development and upgrading. It's getting better as it's version is upgraded, but I am still experiencing some problems with Mac OS X such as unimplemented APIs or odd behaviors.

Cross-platform C++ libraries without UI classes
There are several cross-platform C++ libraries without UI functions and these libraries are widely used to develop server side applications. Usually these libraries are liter than GUI libraries.


POCO
The POCO C++ Libraries are a collection of open source class libraries for developing network-centric, portable applications in C++. POCO stands for POrtable COmponents. The libraries cover functionality such as threads, thread synchronization, file system access, streams, shared libraries and class loading, sockets and network protocols (HTTP, FTP, SMTP, etc.), and include an HTTP server, as well as an XML parser with SAX2 and DOM interfaces and SQL database access. The modular and efficient design and implementation makes the POCO C++ Libraries well suited for embedded development. - from Wikipedia


I applied POCO to my latest project and am very satisfying with it. The design of classes shows good hierarchy and it is well understandable. I could use its functionality without spending much time to read the document and all of my codes have been working correctly without fixing or modification. I can say that POCO is a very stabilized library just like STL, and a nice solution for cross-platform development. I want to thank POCO developers for making this nice open source library.

BOOST
The Boost C++ Libraries are a collection of free libraries that extend the functionality of C++.