Hello world on qtopia

/****************************************************************************
** Form interface generated from reading ui file 'hello.ui'
**
** Created: Fri Jun 25 16:29:29 2004
** by: The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/

#ifndef HELLO_H
#define HELLO_H

#include <qvariant.h>
#include <qwidget.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;

class Hello : public QWidget
{
    Q_OBJECT

public:
    Hello( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
    ~Hello();
//下面是手動添加的代碼

signals:
    void clicked();
protected:
    void mouseReleaseEvent( QMouseEvent * );
    void paintEvent( QPaintEvent * );
private slots:
    void animate();
private:
    QString t;
    int b;

};

#endif // HELLO_H


hello.cpp
/****************************************************************************
** Form implementation generated from reading ui file 'hello.ui'
**
** Created: Wed Jun 23 18:02:36 2004
** by: The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/

#include "hello.h"

#include <qtopia/qpeapplication.h>
#ifdef QWS
#include <qtopia/qcopenvelope_qws.h>
#endif
#include <qtopia/config.h>
#include <qtopia/timestring.h>
#include <qtopia/alarmserver.h>
#include <qtopia/sound.h>
#include <qtopia/resource.h>

#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qpushbutton.h>
#include <qtimer.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qwhatsthis.h>

/*
 * Constructs a Hello which is a child of 'parent', with the
 * name 'name' and widget flags set to 'f'
 */

Hello::Hello( QWidget* parent, const char* name, WFlags fl )
    : QWidget( parent, name, fl )
{
    if ( !name )
    setName( "Hello" );
    resize( 240, 320 );
    setWFlags(fl | Qt::WStyle_ContextHelp);
    setMinimumSize( QSize( 240, 320 ) );
    setMaximumSize( QSize( 240, 320 ) );
    setSizeIncrement( QSize( 240, 320 ) );
    setBaseSize( QSize( 240, 320 ) );
    QPalette pal;
    QColorGroup cg;
    cg.setColor( QColorGroup::Foreground, black );
    cg.setColor( QColorGroup::Button, QColor( 192, 192, 192) );
    cg.setColor( QColorGroup::Light, white );
    cg.setColor( QColorGroup::Midlight, QColor( 223, 223, 223) );
    cg.setColor( QColorGroup::Dark, QColor( 96, 96, 96) );
    cg.setColor( QColorGroup::Mid, QColor( 128, 128, 128) );
    cg.setColor( QColorGroup::Text, black );
    cg.setColor( QColorGroup::BrightText, white );
    cg.setColor( QColorGroup::ButtonText, black );
    cg.setColor( QColorGroup::Base, white );
    cg.setColor( QColorGroup::Background, white );
    cg.setColor( QColorGroup::Shadow, black );
    cg.setColor( QColorGroup::Highlight, black );
    cg.setColor( QColorGroup::HighlightedText, white );
    pal.setActive( cg );
    cg.setColor( QColorGroup::Foreground, black );
    cg.setColor( QColorGroup::Button, QColor( 192, 192, 192) );
    cg.setColor( QColorGroup::Light, white );
    cg.setColor( QColorGroup::Midlight, QColor( 220, 220, 220) );
    cg.setColor( QColorGroup::Dark, QColor( 96, 96, 96) );
    cg.setColor( QColorGroup::Mid, QColor( 128, 128, 128) );
    cg.setColor( QColorGroup::Text, black );
    cg.setColor( QColorGroup::BrightText, white );
    cg.setColor( QColorGroup::ButtonText, black );
    cg.setColor( QColorGroup::Base, white );
    cg.setColor( QColorGroup::Background, white );
    cg.setColor( QColorGroup::Shadow, black );
    cg.setColor( QColorGroup::Highlight, black );
    cg.setColor( QColorGroup::HighlightedText, white );
    pal.setInactive( cg );
    cg.setColor( QColorGroup::Foreground, QColor( 128, 128, 128) );
    cg.setColor( QColorGroup::Button, QColor( 192, 192, 192) );
    cg.setColor( QColorGroup::Light, white );
    cg.setColor( QColorGroup::Midlight, QColor( 220, 220, 220) );
    cg.setColor( QColorGroup::Dark, QColor( 96, 96, 96) );
    cg.setColor( QColorGroup::Mid, QColor( 128, 128, 128) );
    cg.setColor( QColorGroup::Text, black );
    cg.setColor( QColorGroup::BrightText, white );
    cg.setColor( QColorGroup::ButtonText, QColor( 128, 128, 128) );
    cg.setColor( QColorGroup::Base, white );
    cg.setColor( QColorGroup::Background, white );
    cg.setColor( QColorGroup::Shadow, black );
    cg.setColor( QColorGroup::Highlight, black );
    cg.setColor( QColorGroup::HighlightedText, white );
    pal.setDisabled( cg );
    setPalette( pal );
    QFont f( font() );
    f.setFamily( "adobe-helvetica" );
    f.setPointSize( 29 );
    f.setBold( TRUE );
    setFont( f );
    setCaption( tr( "" ) );

    
//手動添加的代碼

    t = "Hello,World";
    b = 0;
    QTimer *timer = new QTimer(this);
    connect( timer, SIGNAL(timeout()), SLOT(animate()) );
    timer->start( 40 );
    QWhatsThis::add( this, tr( "this is hello, world sample." ) );
}

/*
 * Destroys the object and frees any allocated resources
 */

Hello::~Hello()
{
    
// no need to delete child widgets, Qt does it all for us

}
/*
  This private slot is called each time the timer fires.
*/


//以下是手動添加的代碼

void Hello::animate()
{
    b = (b + 1) & 15;
    repaint( FALSE );
}


/*
  Handles mouse button release events for the Hello widget.

  We emit the clicked() signal when the mouse is released inside
  the widget.
*/


void Hello::mouseReleaseEvent( QMouseEvent *e )
{
    if ( rect().contains( e->pos() ) )
        emit clicked();
}


/*
  Handles paint events for the Hello widget.

  Flicker-free update. The text is first drawn in the pixmap and the
  pixmap is then blt'ed to the screen.
*/


void Hello::paintEvent( QPaintEvent * )
{
    static int sin_tbl[16] = {
        0, 38, 71, 92, 100, 92, 71, 38,    0, -38, -71, -92, -100, -92, -71, -38};

    if ( t.isEmpty() )
        return;

    
// 1: Compute some sizes, positions etc.

    QFontMetrics fm = fontMetrics();
    int w = fm.width(t) + 20;
    int h = fm.height() * 2;
    int pmx = width()/2 - w/2;
    int pmy = height()/2 - h/2;

    
// 2: Create the pixmap and fill it with the widget's background

    QPixmap pm( w, h );
    pm.fill( this, pmx, pmy );

    
// 3: Paint the pixmap. Cool wave effect

    QPainter p;
    int x = 10;
    int y = h/2 + fm.descent();
    int i = 0;
    p.begin( &pm );
    p.setFont( font() );
    while ( !t[i].isNull() ) {
        int i16 = (b+i) & 15;
        p.setPen( QColor((15-i16)*16,255,255,QColor::Hsv) );
        p.drawText( x, y-sin_tbl[i16]*h/800, t.mid(i,1), 1 );
        x += fm.width( t[i] );
        i++;
    }
    p.end();

    
// 4: Copy the pixmap to the Hello widget

    bitBlt( this, pmx, pmy, &pm );
}

main.cpp

//#include <qconfig-qpe.h>

//#include "GUcdragonDlg.h"

#include "hello.h"
#include <qapplication.h>
#include <qtopia/qpeapplication.h>
/*
QTOPIA_ADD_APPLICATION("hello",Hello)
QTOPIA_MAIN
*/


/* The program starts here. It parses the command line and builds a message
  string to be displayed by the Hello widget.
*/


#define QT_NO_WIZARD

int main( int argc, char **argv )
{
    QApplication a(argc,argv);
    Hello dlg;
    QObject::connect( &dlg, SIGNAL(clicked()), &a, SLOT(quit()) );
    a.setMainWidget( &dlg );
    dlg.show();
    return a.exec();
}

1.在qtopia-free-2.2.0下執行命令:
  source setQpeEnv
  就可以設置好QPE相關的環境變量,還需要設置
export QMAKESPEC=?/qtopia-free-2.2.0/qtopia/mkspecs/qws/linux-arm-g++
2.在上面的hello目錄下面依次執行以下命令:
  qmake -project
  qmake
  make
  就可以得到hello的可執行程序了。
3.手寫hello.pro文件如下


TEMPLATE = app
CONFIG += qtopiaapp
# comment the following line to enable building the example application
# as a quicklaunch application.
# quicklaunch applications need to be installed before they can be run,
# But can be faster to start running in Qtopia.
#CONFIG -= buildQuicklaunch
HEADERS = hello.h
SOURCES = main.cpp hello.cpp
INTERFACES = hello.ui
TARGET = hello
TARGET.path = /home/nfs/s3c2440_recover/qtopia/bin desktop.files = hello.desktop
desktop.path=/home/nfs/s3c2440_recover/qtopia/apps/Applications
INSTALLS += desktop

pics.files=Example.png
pics.path=/home/nfs/s3c2440_recover/qtopia/pics/example INSTALLS+=pics
並修改main.cpp如下:
#include "hello.h"
#include <qapplication.h>
#include <qtopia/qpeapplication.h>

QTOPIA_ADD_APPLICATION("hello",Hello)
QTOPIA_MAIN


然後執行qmake make則可以生成libhello.so
4.編寫.desktop文件如下:

[Desktop Entry]
comment=A Hello Program
Exec=hello
Icon=Hello
Type=Application
Name=hello
5.將上面生成的可執行文件hello拷貝之開發板的qtopia/bin目錄下面,將hello.desktop拷貝到開發板的
qtopia/apps/Application/目錄下面。並且將來發板上的qtopia/pic/Clock複製一分,重新命名爲hello,將裏面的clock.png改爲hello.png
重新起動開發板的qtopia就可以看到application中有了hello程序了。點擊可以運行。但是該程序可以拖動,而且點擊狀態欄中的小圖標則報錯。
6.將上面的libhello.so拷貝到開發板的qtopia/plugins/application/中。
  重啓qtopia,這是就發現hello程序和其他的qtopia應用程序一樣了。開啓多個任務,點擊狀態欄中的ello圖標則會切換到hello程序。

不過有一個問題是,程序的運行與單個兒的hello有些出入。至於這一點可以參考qtopia其它應用程序的源文件。

有關.pro文件的編寫可以查看qmake的使用手冊!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章