X86—qtopia第一个应用程序(hello)编写

来源:互联网 发布:淘宝手机回收在哪里 编辑:程序博客网 时间:2024/06/05 08:20

平台:redhat 9.0

qt源码:

      qt-x11-2.3.2.tar.gz

      qt-embedded-2.3.7.tar.gz

      qtopia-free-1.7.0.tar.gz

      tmake-1.11.tar.gz

      konqueror-embedded-snapshot-20030705.tar.gz

1、先把这么安装x86-qtopia的方法说一下:

我把所有的整合在一起了,写成了一个build文件,这样就不用一步一步的敲命令,直接一个./build搞定。

build的内容如下:

#!/bin/bash

tar xfvz tmake-1.11.tar.gz
tar xfvz qt-embedded-2.3.7.tar.gz
tar xfvz qtopia-free-1.7.0.tar.gz
tar xfvz qt-x11-2.3.2.tar.gz
tar xvzf konqueror-embedded-snapshot-20030705.tar.gz
mv konqueror-embedded-snapshot-20030705 konq-em
mv tmake-1.11 tmake
mv qt-2.3.7 qt
mv qtopia-free-1.7.0 qtopia
mv qt-2.3.2 qt-x11
cd qt-x11
export QTDIR=$PWD
echo yes | ./configure -static -no-xft -no-opengl -no-sm
make -C src/moc
cp src/moc/moc bin
make -C src
make -C tools/qembed
make -C tools/designer
make -C tools/qvfb
cp tools/qvfb/qvfb tools/qembed/qembed bin
strip bin/uic bin/moc bin/designer bin/qvfb bin/qembed
cd ..
cp qt-x11/bin/?* qt/bin
#rm -fr qt-x11    #HJ
export QTDIR=$PWD/qt
export QPEDIR=$PWD/qtopia
export TMAKEDIR=$PWD/tmake
export TMAKEPATH=$TMAKEDIR/lib/qws/linux-generic-g++
export PATH=$QTDIR/bin:$QPEDIR/bin:$TMAKEDIR/bin:$PATH
cd qt
make clean
cp ../qtopia/src/qt/qconfig-qpe.h src/tools/
(echo yes; echo yes ) |./configure -system-jpeg -gif -system-libpng -system-zlib -platform linux-generic-g++  -qconfig qpe -depths 16,24,32
make -C src
cd ..
cd qtopia/src
./configure  -platform linux-generic-g++
make
cd ../..
#export QTDIR=$PWD/qt
#export QPEDIR=$PWD/qtopia
#export TMAKEDIR=$PWD/tmake
#export TMAKEPATH=$TMAKEDIR/lib/qws/linux-generic-g++
#export PATH=$QTDIR/bin:$QPEDIR/bin:$TMAKEDIR/bin:$PATH
cd konq-em
./configure --enable-embedded --enable-qt-embedded --enable-qpe --with-gui=qpe --disable-debug --enable-ftp --enable-static --disable-shared --disable-mt --with-extra-libs=$QPEDIR/lib --with-extra-include=$QPEDIR/include --without-ssl --with-qt-dir=$QTDIR --with-qt-includes=$QTDIR/include --with-qt-libraries=$QTDIR/lib --with-qtopia-dir=$QPEDIR
make
cd ..


2、环境变量设置:

export QTDIR=$PWD/qt
export QPEDIR=$PWD/qtopia
export TMAKEDIR=$PWD/tmake
export TMAKEPATH=$TMAKEDIR/lib/qws/linux-generic-g++
export PATH=$QTDIR/bin:$QPEDIR/bin:$TMAKEDIR/bin:$PATH
export KDEDIR=./kde

 

3、接着就可以写应用程序了,以hello为例:

我已经在x86-qtopia/目录下面新建了一个/pro目录(用以存放以后的应用程序),在pro/目录下新建一个hello/目录,用来存放hello应用程序。

(1)【新建一个窗体】

[root@iFico x86-qtopia]# ./qt/bin/designer &

选择"New"-->“Widget”

      这时候设计器出现一个Form1,可以修改Form1的name和caption,我将name改为hello,将caption改为Hello_Qt(也就是标题信息);然后我们添加2个pushbutton按钮,一个按钮:当鼠标按到后,就会出现一句话“Hello,QT”,另外一个按钮就是:退出。

一个按钮我将其名称改为user_b;显示信息改为User Button;另外一个按钮改起名称为close;显示信息改为close;

      完成了按钮的设置,我们还需要对要显示的打印信息进行设置,我们放置一个text到user按钮的下面;将Text的名称改为user_t,去除text显示的信息。

       完成以上设置之后,我们需要添加函数,使刚刚的按钮能够对其进行响应,这个不怎么好说,要实际操作才行啊。这些涉及到QT中的信号和槽的概念,按钮的操作是信号,槽就是该操作所响应的函数。

建立User Button到user_button()函数的关联;同事建立close到hello界面的关联。

差不多弄玩后,就可以保存工程文件了。我保存为hello.ui到hello/目录。

 

(2)【产生源代码】

      我们使用uic软件将刚刚建立的工程转换为源代码,我们首先建立一个可执行脚本来完成文件的转换,

[root@iFico hello]#gedit ui2cpp

内容如下:

#!/bin/sh

$QTDIR/bin/uic -o hello.h hello.ui
$QTDIR/bin/uic -o hello.cpp -impl hello.h hello.ui
$QTDIR/bin/moc hello.h -o moc_hello.cpp


[root@iFico hello]#chmod 755 ui2cpp(改变文件的权限)

[root@iFico hello]#./ui2cpp(执行脚本获取源码)

转换之后会生成三个文件:hello.cpp,moc_hello.cpp,hello.h

[root@iFico hello]#gedit main.cpp(新建一个main.cpp文件)

内容如下:

#include "hello.h"                                                            (调用刚刚得到的头文件)
#include <qapplication.h>
#include <qtopia/qpeapplication.h>

QTOPIA_ADD_APPLICATION("hello",hello)             (第一个hello是可执行文件的名称,第二个hello是主界面框的名称)
QTOPIA_MAIN


(3)产生*.pro文件

我们使用tmake中的progen软件产生pro文件

[root@iFico hello]#progen

TEMPLATE                 =app

CONFIG                    =qt warn_on release

HEADERS                  =hello.h

SOURCES                  =hello.cpp /

                                    main.cpp

INTERFACES             =hello.ui

[root@iFico hello]#progen -o hello.pro(获取*.pro文件)

然后编辑hello.pro文件,修改为:

TEMPLATE                 =app

CONFIG                    =qtopia warn_on release        (把qt改为qtopia,这样就可以调用qtopia的库)

HEADERS                  =hello.h

SOURCES                  =hello.cpp /

                                    main.cpp

INTERFACES             =                       

一定要将“INTERFACES  = hello.ui”修改为“INTERFACES  =   ”否则会出现错误:报错,重定义。
[root@iFico hello]# make
Makefile:132: warning: overriding commands for target `moc_hello.cpp'
Makefile:129: warning: ignoring old commands for target `moc_hello.cpp'
hello.o(.text+0x0): In function `hello::hello[not-in-charge](QWidget*, char const*, unsigned)':: multiple definition of `hello::hello[not-in-charge](QWidget*, char const*, unsigned)'
hello.o(.text+0x0): first defined here
hello.o(.text+0x33c): In function `hello::hello[in-charge](QWidget*, char const*, unsigned)':
: multiple definition of `hello::hello[in-charge](QWidget*, char const*, unsigned)'
hello.o(.text+0x33c): first defined here
hello.o(.text+0x678): In function `hello::~hello [not-in-charge]()':
: multiple definition of `hello::~hello [not-in-charge]()'
hello.o(.text+0x678): first defined here
hello.o(.text+0x69c): In function `hello::~hello [in-charge]()':
: multiple definition of `hello::~hello [in-charge]()'
hello.o(.text+0x69c): first defined here
hello.o(.text+0x6c0): In function `hello::~hello [in-charge deleting]()':
: multiple definition of `hello::~hello [in-charge deleting]()'
hello.o(.text+0x6c0): first defined here
hello.o(.text+0x6ec): In function `hello::event(QEvent*)':
: multiple definition of `hello::event(QEvent*)'
hello.o(.text+0x6ec): first defined here
hello.o(.text+0x760): In function `hello::user_button()':
: multiple definition of `hello::user_button()'
hello.o(.text+0x760): first defined here
moc_hello.o(.text+0x0): In function `hello::className() const':
: multiple definition of `hello::className() const'
moc_hello.o(.text+0x0): first defined here
moc_hello.o(.data+0x0): multiple definition of `hello::metaObj'
moc_hello.o(.data+0x0): first defined here
moc_hello.o(.text+0xc): In function `hello::initMetaObject()':
: multiple definition of `hello::initMetaObject()'
moc_hello.o(.text+0xc): first defined here
moc_hello.o(.text+0xcc): In function `hello::staticMetaObject()':
: multiple definition of `hello::staticMetaObject()'
moc_hello.o(.text+0xcc): first defined here
moc_hello.o(.text+0x74): In function `hello::tr(char const*)':
: multiple definition of `hello::tr(char const*)'
moc_hello.o(.text+0x74): first defined here
moc_hello.o(.text+0xa0): In function `hello::tr(char const*, char const*)':
: multiple definition of `hello::tr(char const*, char const*)'
moc_hello.o(.text+0xa0): first defined here
collect2: ld returned 1 exit status
make: *** [../x86-qtopia/qtopia/bin/hello] Error 1
[root@iFico hello]#

 

(4)【生成Makefile文件】

[root@iFico hello]#tmake -o Makefile hello.pro

然后修改Makefile文件,去掉无用的信息,和添加新的内容

修改15行为: LINK =  g++   (如果不改成g++的话,会出现错误)

修改37行为:TARGET    =    $(QPEDIR)/bin/hello
同时添加2行:

DESKTOP=    $(QPEDIR)/apps/iFico/hello.desktop
ICON=        $(QPEDIR)/pics/hell.png

大概在80行修改为:

clean:
    -rm -f $(OBJECTS) $(OBJMOC) $(DESKTOP) $(ICON) $(TARGET)
    -rm -f *~ core

 

(5)制作启动器

创建一个桌面启动器(*.desktop)文件,方法如下:

[root@iFico hello]#gedit hello.desktop

内容如下:

[Desktop Entry]
Version=1.0
Name=First Test
comment=Hello Qt
Exec=hello
Icon=hello
Type=Application
Name[zh_CN]=第一个测试程序
Name[zh_TW]=第一個測試程序


(6)制作桌面图标

  制作一个16 X 16大小的PNG格式的图标文件,将该文件存放在$QPEDIR/pics目录下,命名为:hello.png

 

(7)修改hello.cpp文件

为了实现前面讲到的按下user_button按钮,出现预设的打印信息,那就需要修改hello.cpp

修改后的内容如下:

/****************************************************************************
** Form implementation generated from reading ui file 'hello.ui'
**
** Created: Tue Dec 8 15:18:30 2009
**      by:  The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#include "hello.h"

#include <qlabel.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.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( 416, 296 );
    setCaption( tr( "Hello_QT" ) );

    close = new QPushButton( this, "close" );
    close->setGeometry( QRect( 200, 160, 108, 32 ) );
    close->setText( tr( "Close" ) );

    user_b = new QPushButton( this, "user_b" );
    user_b->setGeometry( QRect( 80, 40, 108, 32 ) );
    user_b->setText( tr( "User Button" ) );

    user_t = new QLabel( this, "user_t" );
    user_t->setGeometry( QRect( 70, 90, 200, 40 ) );
    QFont user_t_font(  user_t->font() );
    user_t_font.setBold( TRUE );
    user_t->setFont( user_t_font );
    user_t->setText( tr( "" ) );

    // signals and slots connections
    connect( user_b, SIGNAL( clicked() ), this, SLOT( user_button() ) );
    connect( close, SIGNAL( clicked() ), this, SLOT( close() ) );
}

/* 
 *  Destroys the object and frees any allocated resources
 */
hello::~hello()
{
    // no need to delete child widgets, Qt does it all for us
}

/* 
 *  Main event handler. Reimplemented to handle application
 *  font changes
 */
bool hello::event( QEvent* ev )
{
    bool ret = QWidget::event( ev );
    if ( ev->type() == QEvent::ApplicationFontChange ) {
    QFont user_t_font(  user_t->font() );
    user_t_font.setBold( TRUE );
    user_t->setFont( user_t_font );
    }
    return ret;
}

void hello::user_button()
{
    user_t->setText( tr("Hello,Qt world!") );              (就只修改了这一句话而已)
//    qWarning( "hello::user_button(): Not implemented yet!" );
}

(8)编译并仿真

完成前面的步骤后,在PC的linux终端上输入:make。即可完成编译。

编译完后的“hello”应用程序放在“qtopia/bin”目录下面,“hello.png”图标放在“qtopia/pics”目录下面,“hello.desktop”桌面启动器放到“qtopia/apps/iFico/”目录下面。

 

(9)运行应用程序

#qvfb &
#qpe

运行截图如下:

 

(本文参考了天嵌科技的文档)

原创粉丝点击