Saturday, November 22, 2008

QT3 - BOUML - CMake: Hello World!

I am going to demonstrate how to combine the two tools, BOUML and CMake, to create the simplest possible QT3-based classic "Hello World" application.

First, in BOUML create a project under a name hello_world in a directory hello_world/model. Then select c++ as a working language. In the browser pane right-click to access the Edit  generation settings menu, where in the "Directory" tab we will enter hello_world/gensrc as a c++ root directory, where all the generated source code will be saved.



Now we will create the Main.cpp file. Right-click again on the project name in the browser pane to create a new Class Diagram within a new Class View. Open the diagram and put a new class in it, simply called Main.
hello_world
+-main (class view)
+-main (class diagram)
+-Main (class)

In the original QT3 example two headers are needed, for QApplication and QPushButton classes. Normally, we would use the External Types settings from the Generation Settings/C++ [5], but it won't work here. Instead, we will create another class diagram called QT3 with two classes: qapplication and qpushbutton (leave the capitalization as it is - these will be used as a template for header file names), which we will use to show the external dependency between Main and the required QT3 classes. We will achieve that by checking the external checkbox in their Class Dialog/C++ tab.
hello_world
+-main (class view)
+-main (class diagram)
+-Main (class)
+-QT3 (class view)
+-QT3 (class diagram)
+-qapplication (class)
+-qpushbutton (class)

Once we've done that just drag the QT3 classes from the browser pane to the Main class diagram. Then draw the dependency relation from Main to both.

It's time to add the main function; right-click on the Main class and select Add operation to add it. In the UML tab select int as the return value type. Then add two standard argc and argv arguments in the parameters area. To have main defined outside the Main class we will configure it as friend by checking relevant box in the C++ tab. To add the body of main select the C++ tab again and hit the Edit body button. In the open window paste the following:
    QApplication a(argc, argv);

QPushButton hello("Hello world!", 0);
hello.resize(100, 30);

a.setMainWidget(&hello);
hello.show();
return a.exec();

We are almost there - so we need deploy the classes: right-click on the Main class view and select Tools/Deploy classes, and then Generate/C++. Two files will be created, namely Main.cpp and Main.h

Voila!

Now let's put all the stuff into the CMake build framework. Create a hello_world/build folder and create a CMakeLists.txt file there. Edit it and fill with the following:
CMAKE_MINIMUM_REQUIRED( VERSION 2.4 )
IF( COMMAND cmake_policy )
cmake_policy( SET CMP0003 NEW )
ENDIF( COMMAND cmake_policy )

FIND_PACKAGE( Qt3 REQUIRED )

ADD_DEFINITIONS(
${QT_DEFINITIONS}
-DQT_THREAD_SUPPORT
)

SET( SRC_DIR "${PROJECT_SOURCE_DIR}/../gensrc" )
SET( BIN_DIR "${PROJECT_SOURCE_DIR}/../build" )

ADD_SUBDIRECTORY( ${SRC_DIR} ${BIN_DIR} )

INCLUDE_DIRECTORIES(
${SRC_DIR}
${QT_INCLUDE_DIR}
)

ADD_EXECUTABLE( hello
${SRC_DIR}/Main.cpp
)

TARGET_LINK_LIBRARIES(
hello
${QT_LIBRARIES}
)

Then execute
cmake . && make

and enjoy the newly created hello executable.

No comments:

Post a Comment