понеделник, 12 юли 2010 г.

Hello World with GUI application for FrienndlyARM Tutorial


Here are the steps of creating a simple "Hello world" GUI application for FriendlyARM board.   You should have development tools ARM-Linux GCC 4.3.2 and ARM-Qtopia 2.2.0 already installed.  If you don't have them yet you can download them from here: http://www.friendlyarm.net/downloads
My PC is working under Ubuntu 8.04. and the path of Qtopia installation is /usr/local.  I know that my environment is not quite correctly set.That's why I do some steps as root and source some environment variables from file setQpeEnv. But it works.

Here are the steps:
1. With Qt Designer create the main form.

Remark:
Qt Designer comes with the "ARM-Qtopia 2.2.0". It's executable is:
/usr/local/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/bin/designer
The screenshot above is from this tool.
Do not try newer versions of Qt Creator. They are not compatible.

From File-New select "Dialog" template.  Change the "name" property to frmMainWindow. This will be the name of the form class.  Save it as MainWindow.ui. This is an XML file. You can view and edit it's contents with text editor.  For example if you want to add tabs to TabWidget this is the only option.

2. Create a script file "build" for building the application with following content:
#!/bin/bash
source /usr/local/arm-qtopia/qtopia-2.2.0-FriendlyARM/setQpeEnv 
qmake -spec /usr/local/arm-qtopia/qtopia-2.2.0-FriendlyARM/qtopia/mkspecs/qws/linux-arm-g++ -o Makefile *.pro 
make clean
make
3.Create a script file "formuic" for compiling the .ui form file with the following content
echo MAIN FORM
echo HEADER
/usr/local/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/bin/uic -o MainWindow.h MainWindow.ui
echo CLASS
/usr/local/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/bin/uic -i MainWindow.h -o  MainWindow.cpp MainWindow.ui 

Open a terminal window and go to the project folder. Run the script:
    #./formuic
The uic tool (user interface compiler) takes the .ui file. 
When MainWindow.ui is compiled two source files are created - a header and cpp. They content the form class "frmMainWindow".
 
4. Create two files main.cpp , MainWindowImp.h and MainWindowImp.cpp with the following contents:

main.cpp
#include <qtopia/qpeapplication.h>
#include "MainWindowImp.h"

QTOPIA_ADD_APPLICATION("stivlib",frmMainWindowImplementation)
QTOPIA_MAIN
QTOPIA_ADD_APPLICATION macro takes two parameters. The first is "stivlib"  - the name of the project.  The second frmMainWindowImplementation is the name of the class from MainWindowImp.h

MainWindowImp.h
In this file a new class frmMainWindowImplementation is defined which inherits the form class frmMainWindow.  This is done because each time you make changes to you form with Qt Designer and the .ui file is compiled any changes made in MainWindow.h and MainWindow.cpp will be lost. All the functional implementations like new variables definitions and new methods should be done in this new class. 

/****************************************************************************
** Created: Sun Jul 11 18:05:02 2010
**      by:  Svilen Stavrev
** Implementation Class for the main form
****************************************************************************/
#ifndef FRMMAINWINDOWIMP_H
#define FRMMAINWINDOWIMP_H

#include "MainWindow.h"

class frmMainWindowImplementation : public frmMainWindow

    Q_OBJECT

public:
    frmMainWindowImplementation( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 ) 
    : frmMainWindow(parent,name,modal,fl)
    {
        //Constructor
    }
};
#endif // FRMMAINWINDOW_H
 
MainWindowImp.cpp
/************************************************
** Created: Sun Jul 11 18:05:02 2010
**      by:  Svilen Stavrev
** Implementation Class for the main form
************************************************/
#include "MainWindowImp.h"

5. Generate the .pro (project) file using the qmake tool:
    $su
    #source  /usr/local/arm-qtopia/qtopia-2.2.0-FriendlyARM/setQpeEnv
    #qmake -project

A project file stivlib.pro has been created.
Open it with the text editor and add these lines:

TARGET=stivlib         <-- This is the exact name of the target executable file. No extension .bin will be added
CONFIG += qtopiaapp
CONFIG -= buildQuicklaunch
5. Build the application
    #./build
Voila! The binary file stivlib is now in the project folder. 
On my next post I'll show how to upload it to the FriendlyARM board and how to create a desktop launcher.

25 коментара:

  1. Hello,
    My PC is working under Ubuntu 10.10 and my path of Qtopia installation is /opt/FriendlyARM/mini2440 . So I change the third line in the file 'formuic' to >> /opt/FriendlyARM/mini2440/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/bin/uic -o MainWindow.h MainWindow.ui .
    But when run the script I only see:
    MAIN FORM
    HEADER

    No prompt, it seems that the terminal hangs. Wat can be the problem?

    Wim

    ОтговорИзтриване
  2. Sorry for late reply jeba.
    "MAIN FORM" and "HEADER" are just echo.
    Try to call uic from command line to check that you have the proper installation. Enter this:
    /opt/FriendlyARM/mini2440/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/bin/uic

    And if everything is ok you should get somethink like this:

    Qt user interface compiler
    Usage: /opt/FriendlyARM/mini2440/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/bin/uic [options] [mode]

    Generate declaration:
    .........
    .........

    I usually do it as root. If you don't get this responce check the path to uic program again.

    If everithing is ok with uic check your .ui file. I can send you a ui file that I'm sure is fine.

    ОтговорИзтриване
  3. Hi!

    I have followed your tutorial and all works fine till I get to build the application. The following error occurs:

    arm-linux-g++ -shared -o libstivlib.so .obj/release-shared/main.o .obj/release-shared/MainWindowImp.o .obj/release-shared/MainWindow.o .obj/release-shared/moc_MainWindow.o -luuid -lqtopia2 -lqtopia -lqpe -L/qtopia-2.2.0-FriendlyARM/qtopia/lib -L/qtopia-2.2.0-FriendlyARM/qt2/lib -lqte
    /usr/local/arm/4.3.2/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.2/../../../../arm-none-linux-gnueabi/bin/ld: cannot find -lqtopia2

    Could you please give me a hint how to solve this issue?

    ОтговорИзтриване
  4. I would like to add something.

    I have changed stivlib with hello. I have checked the Makefile and I can see that the final target is libhello.so. I think it should be hello the final target.

    Thanks,
    Sasha.

    ОтговорИзтриване
  5. OK thanks stavrev the first part is working now. The problem was that i had used QT4 designer instead of the designer in /opt/FriendlyARM/mini2440/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/bin folder. Now i can create the MainWindow.h and .cpp file and also the .pro file. I have stored the files in ~/Desktop/test, and so a file test.pro is created by the "#qmake -project" command. When run the build script i got the following errors:

    main.cpp:1:10: error: #include expects "FILENAME" or
    main.cpp:2:10: error: #include expects "FILENAME" or
    main.cpp:4: error: expected constructor, destructor, or type conversion before '(' token
    make: *** [.obj/release-shared/main.o] Error 1

    ОтговорИзтриване
  6. Hi, jeba_wdej.

    Replace the first two lines with:
    #include "qtopia/qpeapplication.h"
    #include "MainWindowImp.h"

    Sasha.

    ОтговорИзтриване
  7. Hi Sasha,
    The compiler tries to build library instead of application.
    Seems like you missed to change the target name in QTOPIA_ADD_APPLICATION macro in main.cpp file. It should content this:


    #include <qtopia/qpeapplication.h>
    #include "MainWindowImp.h"
    QTOPIA_ADD_APPLICATION("hello",frmMainWindowImplementation)
    QTOPIA_MAIN


    If compiler can't find main() entry point it creates library.
    I think other possible reason might be if you run
    #qmake -project
    before you have main.cpp file created. This file should be present in the hello.pro in the SOURCE line.
    The file hello.pro:


    CONFIG += qtopiaapp
    CONFIG -= buildQuicklaunch
    TEMPLATE = app
    INCLUDEPATH += .
    TARGET = hello
    # Input
    HEADERS += MainWindowImp.h
    INTERFACES += MainWindow.ui
    SOURCES += main.cpp MainWindowImp.cpp


    Check these things, delete the Makefile and run build again. Hope this will help.

    Svilen


    P.S.
    On the step 4 I wrote by mistake:
    4. Create two files main.cpp , MainWindowImp.h and MainWindowImp.cpp with the following contents:

    The files are not two but three.
    Sorry

    ОтговорИзтриване
  8. Thanks Sasha I'm one step further now.But now I got another couple of errors:
    main.cpp:2:27: error: MainWindowImp.h: No such file or directory
    main.cpp: In function 'QWidget* create_frmMainWindowImplementation(QWidget*, const char*, uint)':
    main.cpp:4: error: expected type-specifier before 'frmMainWindowImplementation'
    main.cpp:4: error: cannot convert 'int*' to 'QWidget*' in return
    main.cpp:4: error: expected ';' before 'frmMainWindowImplementation'
    main.cpp:4: error: 'frmMainWindowImplementation' was not declared in this scope
    make: *** [.obj/release-shared/main.o] Error 1

    The content of my genetarted projectfile test.pro is:
    ######################################################################
    # Automatically generated by qmake (1.06c-Qtopia) Sun Nov 28 22:27:55 2010
    ######################################################################

    TEMPLATE = app
    INCLUDEPATH += .

    # Input
    INTERFACES += MainWindow.ui
    SOURCES += main.cpp MainWindowImp.cpp

    TARGET=test
    CONFIG += qtopiaapp
    CONFIG -= buildQuickLaunch

    ОтговорИзтриване
  9. Hi jeba,
    Did you miss MainWindowImp.h file? Also you shoud have heder files described in the project file. Add this line to test.pro

    HEADERS += MainWindowImp.h

    Here is what .pro file should content:
    CONFIG += qtopiaapp
    CONFIG -= buildQuicklaunch
    TEMPLATE = app
    INCLUDEPATH += .
    TARGET = hello
    # Input
    HEADERS += MainWindowImp.h
    INTERFACES += MainWindow.ui
    SOURCES += main.cpp MainWindowImp.cpp

    ОтговорИзтриване
  10. Hi Svilen,

    YES it's working now, thanks. A stivlib.so file will be produced by the build script. Is this the "executable" file? Where can i find more information about gui programming with Qtopia?

    ОтговорИзтриване
  11. The executable file is stivlib. Files with .so are library files.

    There are many books and tutorials on Qt programming. You can install Qt on PC with Windows or Linux and start learning it. Programming for FriendlyARM is not much different.

    ОтговорИзтриване
  12. I can´t find an executable file. The only file produced is the libstivlib.so file. How can i execute this file on my FriendlyARM? I have searched on my PC for files named stivlib but I can´t find this.
    What I have seen with QT on my PC under ubuntu is that it just is easy to make GUI applications as it is in visual studio for windows. But for QTopia there is only a designer for the form design. What i miss is a complete tool, or a clear description of how to make complex GUI applications for my FriendlyARM.

    ОтговорИзтриване
  13. Hello jeba,
    The next step is to upload the executable and create a launcher.

    http://farm2440.blogspot.com/2010/07/uploading-application-and-creating.html

    I can't tell what is the reason you get library instead of executable. Can you send me the contents of your project folder?
    farm2440@gmail.com

    ОтговорИзтриване
  14. I apologize for misleading some of you

    I wrote:
    CONFIG -= buildQuickLaunch
    and it should be:
    CONFIG -= buildQuicklaunch

    Now it's corrected.

    ОтговорИзтриване
  15. Hey,

    I don't have the Qt Designer on the CD which i got with my mini, therefore i can't make any apps for it. Can you mail me the download link for Qt Designer from Trolltech for qtopia 2.2.0 or post it as a download link in your blog. My mail id is tanushri.c [at] gmail [dot] com

    Regards
    Tan

    ОтговорИзтриване
  16. Hello,

    I am student and i have to do some application for FriendlyArm. I used your tutorial but i am still getting error:
    /opt/EmbedSky/4.3.3/bin/../lib/gcc/arm-none-linux-gnueabi/4.3.3/../../../../arm-none-linux-gnueabi/bin/ld: cannot find -lqtopia2
    collect2: ld returned 1 exit status

    i dont know what do do with it and i even cant google it. Sasha asked the same thing but he didnt write how he solved it. Can you please help me somehow? I cant move forward.
    Thanks
    Simon

    ОтговорИзтриване
  17. Този коментар бе премахнат от автора.

    ОтговорИзтриване
  18. Tanu, Simon
    Sorry I was away for a while and didn't notice your comments. If it's not too late tanu download

    http://www.friendlyarm.net/dl.php?file=arm-qtopia-2.2.0_20100108.tgz

    Qt Designer is included.


    Simon, please check #include. Did you miss <> signs?
    I missed them in my posts. Will correct it ASAP

    #include
    #include "MainWindowImp.h"

    Regards
    s_stavrev

    ОтговорИзтриване
  19. Hi,
    I am getting the following error:

    /opt/FriendlyARM/toolschain/4.4.3/lib/gcc/arm-none-linux-gnueabi/4.4.3/../../../../arm-none-linux-gnueabi/bin/ld: cannot find -lqtopia2
    collect2: ld returned 1 exit status
    make: *** [stivlib] Error 1

    Could anyone help me in resolving the issue?

    Thanks in advance...

    ОтговорИзтриване
  20. Този коментар бе премахнат от автора.

    ОтговорИзтриване
  21. Hi,
    Thanks for this tutorial. I need help with Touchscreen.
    I compiled Qt and tslib successfully.
    Typing "cat /dev/input/ts" works as mentioned (weird chars when touched).
    ts_calibrate and ts_test work fine too (touch screen is working fine).

    If I use "export QWS_MOUSE_PROTO=tslib:/dev/input/ts" it shows "Segmentation fault". When I replace this line by just "export QWS_MOUSE_PROTO=" (without "driver:options") my "Hello World" shows perfectly, but no touch screen.

    Compiling Qt with "-no-freetype" generates the same result: "Segmentation fault".

    I can't find a way to work touch with my app.

    Any suggestion?
    Thank you.
    Afonso.

    ОтговорИзтриване
  22. it works, how can I add button and signal ?
    thanks

    ОтговорИзтриване
  23. I tried to add another button but confuse on how to put the signal, do you know how to do it ?
    thanks

    ОтговорИзтриване
  24. I am created a same application in qt but problem generated in run time not search the various lib in mini2440

    ОтговорИзтриване