Wednesday 7 March 2012

QTHelloWorld in Momentics IDE for MS Windows users


Want to start developing/porting Qt apps to Blackberry Playbook but don't want to dive into Qt libraries compilation? If yes then this article is for you, it lets you jump directly to developing Qt app skipping tedious process of compiling Qt libraries first.
We'll be speaking here only about creating Qt application to be deployed to Blackberry Playbook Simulator (Qt library below is compiled for x86 architecture).

1. Prepare your MS Windows system
1.1. Download Native SDK for Tablet OS 2.0 from here https://bdsc.webapps.blackberry.com/native/download/ and install - very straitforward process.
1.2. Create a folder C:\qt-x86-lib (you can create a folder wherever you want, this path will be used in this article)
1.3. Download Qt library from http://qt-qnx.tm-k.com/qt-x86-lib.tgz and save it into C:\qt-x86-lib\ folder created in 1.2. This is a set of Qt libraries precompiled for x86 platform and suitable only for Simulator. This is not something that is being built nightly but it's fresh above and proved to work. Probably with time if there is enough interest in the project I'll setup something more serious to follow current Qt development repository.
1.4. Unpack qt-x86-lib.tgz archive using any archivator that understands .tgz format (my favorite one is 7zip). You should get C:\qt-x86-lib\stage\... and lots of files/folders underneath.

That's basically it, now you can start develop Qt app.

2. Create QTHelloWorld project in Momentics IDE
Start Momentics IDE.
Click File->New->BlackBerry Tablet OS C/C++ Project and in Project Name: specify QTHelloWorld, hit Next and Basic Setting will open.
Here:
- check Language: C++
- Build Style: Managed build
- Project Type: Application-Empty Application
Click Next and Build Configurations will open, we want Simulator-Debug here. Click Finish.
At this point we have an empty project with bar-descriptor opened for you in IDE. You can put whatever you want at General Page:


Click Assets tab and then make sure to select Simulator-Debug at Build Configurations panel. Then click Add Files... then File System... and select your libQTCore.so.4.8.0 that you should have in qt-x86-lib\stage\nto\x86\usr\lib\qt4\lib\. Do the same for libQtGui.so.4.8.0, libQtOpenGL.so.4.8.0. Now add one more file: C:\qt-x86-lib\stage\nto\x86\usr\lib\qt4\plugins\platforms\libblackberry.so. Now we have to correct one line in bar-descriptor.xml. Click on Source tab and locate the line
<asset path="C:/qt-x86-lib/stage/nto/x86/usr/lib/qt4/plugins/platforms/libblackberry.so" type="Qnx/Elf">lib/libblackberry.so</asset>
Change it to:
<asset path="C:/qt-x86-lib/stage/nto/x86/usr/lib/qt4/plugins/platforms/libblackberry.so" type="Qnx/Elf">lib/platforms/libblackberry.so</asset>
Click File->Save all...

Now you should have picture similar to this:


Now let's add source file main.cpp to the project. Right click on src folder in Project Explorer and choos New->Source file, in Source File: field put main.cpp and hit Finish.

Main.cpp will be opened for you, copy paste following code:
#include <QApplication>
#include <QPushButton>
#include <QWidget>

int main(int argc, char *argv[])
{
    qputenv("QT_QPA_FONTDIR", "/usr/fonts/font_repository/monotype");
    QCoreApplication::addLibraryPath("app/native/lib");

    QApplication app(argc, argv);

    QWidget window;
    window.resize(1024, 600);

    QPushButton quit("Quit", &window);
    quit.setGeometry(422, 100, 180, 40);
    QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));

    window.show();
    return app.exec();
}
Click File->Save all...


Now it is time to change some compiler/linker settings. Right click on QTHelloWorld in Project Explorer and choose Properties. Make sure you set up right things in Configuration:, Include Directories (-l) and Defines (-D) as shown on the picture:


Now set up your libraries this way:


Click Ok.
Let's try to build - click Project->Build Project. In Console you should eventually see "Build complete for project QTHelloWorld". Congratulations! it's already something... 
Now hit Run->Debug Configurations... You need to specify -platform blackberry at Arguments tab. And at Environment tab check that you have LD_LIBRARY_PATH=app/native/lib (sometimes it is missing, don't know why). Click 'Debug' and after the project is packaged and deployed to your Simulator you will be switched to Debug Perspective and the program execution will stop at main().



Now you know what to do -) Enjoy!

PS: many thanks to http://hecgeek.blogspot.com/2011/10/qt-for-blackberry-playbook.html who gave me the very first hint how to start.

Wednesday 15 February 2012

QTHelloWorld in Momentics IDE for Blackberry Playbook

1. Build QT library for Playbook Simulator
For simplicity let's assume you are using /qt-x86-lib folder (x86 because you are building for Simulator)
$ cd /qt-x86-lib
$ git clone https://BGmot@github.com/BGmot/Qt.git
...git will work here for you...
$ cd Qt

My Momentics IDE is installed in /Developer/SDKs/bbndk-2.0/, adjust the next line according to your setup.
$ source /Developer/SDKs/bbndk-2.0/bbndk-env.sh
$ ./configure-qsk x86
...lot's of output...
$ make
... here you can go drink some coffee/beer/whatever you prefer/watch favorite movie...
$ make install
...lot's of output...

At this point you should have all QT libraries compiled for x86 architecture. Let's check it.
$ ls -l /qt-x86-lib/Qt/stage/nto/x86/usr/lib/qt4/lib
drwxr-xr-x  84 eyurchen  domusers      2856 Feb  9 14:28 fonts
-rw-r--r--   1 eyurchen  domusers       922 Feb 10 11:13 libQtCore.la
-rw-r--r--   1 eyurchen  domusers       872 Feb 10 11:13 libQtCore.prl
lrwxr-xr-x   1 eyurchen  domusers        18 Feb 10 11:13 libQtCore.so -> libQtCore.so.4.8.0
lrwxr-xr-x   1 eyurchen  domusers        18 Feb 10 11:13 libQtCore.so.4 -> libQtCore.so.4.8.0
lrwxr-xr-x   1 eyurchen  domusers        18 Feb 10 11:13 libQtCore.so.4.8 -> libQtCore.so.4.8.0
-rwxr-xr-x   1 eyurchen  domusers   3193036 Feb 10 11:13 libQtCore.so.4.8.0
...
You should have similar output.


2. Create QTHelloWorld project in Momentics IDE
Download/Install/Start Momentics 2.0
Click File->New->BlackBerry Tablet OS C/C++ Project and in Project Name: specify QTHelloWorld, hit Next and Basic Setting will open.
Here:
- check Language: C++
- Build Style: Managed build
- Project Type: Application-Empty Application
Click Next and Build Configurations will open, we want Simulator-Debug here. Click Finish.
At this point we have an empty project with bar-descriptor opened for you in IDE. You can put whatever you want at General Page:


Click Assets tab and then make sure to select Simulator-Debug at Build Configurations panel. Then click Add Files... then File System... and select your libQTCore.so library that you built in /qt-x86-lib/Qt/stage/nto/x86/usr/lib/qt4/lib. Do the same for libQtGui.so, libQtOpenGL.so. Now add one more file: /qt-x86-lib/Qt/stage/nto/x86/usr/lib/qt4/plugins/platforms/libblackberry.so. Now you should have picture similar to this:

Now we have to correct one line in bar-descriptor.xml. Click on Source tab and locate the line
<asset path="/qt-x86-lib/Qt/stage/nto/x86/usr/lib/qt4/plugins/platforms/libblackberry.so" type="Qnx/Elf">lib/libblackberry.so</asset>

Change it to:
<asset path="/qt-x86-lib/Qt/stage/nto/x86/usr/lib/qt4/plugins/platforms/libblackberry.so" type="Qnx/Elf">lib/platforms/libblackberry.so</asset>
Click File->Save all...

Now let's add source file main.cpp to the project. Right click on src folder in Project Explorer and choos New->Source file, in Source File: field put main.cpp and hit Finish.

Main.cpp will be opened for you, copy paste following code:


#include <QApplication>
#include <QPushButton>
#include <QWidget>

int main(int argc, char *argv[])
{
    QCoreApplication::addLibraryPath("app/native/lib");
    QApplication app(argc, argv);

    QWidget window;
    window.resize(1024, 600);

    QPushButton quit("Quit", &window);
    quit.setGeometry(422, 100, 180, 40);
    QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));

    window.show();
    return app.exec();
}
Click File->Save all...

Now it is time to change some compiler/linker settings. Right click on QTHelloWorld in Project Explorer and choose Properties. Make sure you set up right things in Configuration:, Include Directories (-l) and Defines (-D) as shown on the picture:

Now set up your libraries this way:

Click Ok.
Let's try to build - click Project->Build Project. In Console you should eventually see "Build complete for project QTHelloWorld". Congratulations! it's already something... Now hit Run->Debug and after the project is packaged and deployed to your Simulator you will be switched to Debug Perspective and the program execution will stop at main().

Now you know what to do -) Enjoy!

PS: many thanks to http://hecgeek.blogspot.com/2011/10/qt-for-blackberry-playbook.html who gave me the very first hint how to start.