QT4编程指导.pdf
QT4QT4 完全开发手册完全开发手册 Author:sprife2008.12.12 硬件:UP-ARM2410CL、PC 机 Pentumn500 以上,硬盘 80G 以上。软件:PC 机操作系统 RHEL4 xshell ARM-Linux 开发环境 qt-embedded-linux-opensource-src-4.4.0.tar.bz2 tslib-1.4.tar.bz2、qt-x11-opensource-src-4.4.0.tar.gz 一、一、编译编译 QTQT-X11X11 环境环境 cd/home/sprife mkdir qt4 cd qt4 mkdir for_pc mkdir for_arm cp/home/qt-x11-opensource-src-4.4.0.tar.gz /home/sprife/qt4/for_pc cp/home/qt-embedded-linux-opensource-src-4.4.0.tar.bz2 /home/sprife/qt4/for_arm cp/home/tslib-1.4.tar.bz2/home/sprife/qt4/for_arm cd/home/sprife/qt4/for_pc tar xzvf qt-x11-opensource-src-4.4.0.tar.gz cd qt-x11-opensource-src-4.4.0 ./configure(./configure-prefix/usr/local/Trolltech/Qt-x11-4.4.0)(yes)gmake gmake install 二、二、编译编译 QT/EQT/E 环境环境 注:核心板使用注:核心板使用 PXA270PXA270 可略去触摸屏步骤使用可略去触摸屏步骤使用 USBUSB 鼠标作为输入鼠标作为输入 1、编译 tslib1.4 触摸屏库 cd/home/sprife/qt4/for_arm tar xjvf tslib-1.4.tar.bz2 cd tslib-1.4 vi build.sh#/bin/sh export CC=arm-linux-gcc ./autogen.sh echo ac_cv_func_malloc_0_nonnull=yes arm-linux.cache ./configure-host=arm-linux-cache-file=arm-linux.cache -prefix=$PWD/./tslib1.4-install make make install 编译:./build.sh 2、编译 QT/E 库 cd/home/sprife/qt4/for_arm tar xjvf qt-embedded-linux-opensource-src-4.4.0.tar.bz2 cp-a tslib1.4-install/lib/*qt-embedded-linux-opensource-src-4.4.0/lib/cp-a tslib1.4-install/include/ts*qt-embedded-linux-opensource-src-4.4.0/include/cd qt-embedded-linux-opensource-src-4.4.0 ./configure-embedded arm-xplatform qws/linux-arm-g+-depths 16 -little-endian-qt-mouse-linuxtp-qt-mouse-tslib -I/home/sprife/qt4/for_arm/tslib1.4-install /include L/home/sprife/qt4/for_arm/tslib1.4-install/lib -prefix/mnt/nfs/Trolltech/qt-embedded-4.4.0 make make install -prefix 选项要特殊注意,该指定目录必须要与实际 NFS 目录对应否则字体处会出现错误 3、测试触摸屏及 QT/E 程序 1、建立 NFS 共享目录/arm2410cl cd/mkdir arm2410cl cd arm2410cl mkdir Trolltech cd Trolltech mkdir qt-embedded-4.4.0 cd qt-embedded-4.4.0 cp /home/sprife/qt4/for_arm/qt-embedded-linux-opensource-src-4.4.0/l ib/./-arf cp/home/sprife/qt4/for_arm/tslib1.4-install/etc/./-arf cp/home/sprife/qt4/for_arm/tslib1.4-install/bin/./-arf 2、ARM 端挂载 NFS 共享目录 mount-o nolock,rsize=4096,wsize=4096 192.168.1.111:/arm2410cl /mnt/nfs 3、设置环境变量 cd/mnt/nfs/Trolltech/qt-embedded-4.4.0 export QTDIR=$PWD export LD_LIBRARY_PATH=$PWD/lib export TSLIB_TSDEVICE=/dev/input/event0 export TSLIB_PLUGINDIR=$PWD/lib/ts export TSLIB_CONSOLEDEVICE=none export TSLIB_CONFFILE=$PWD/etc/ts.conf export POINTERCAL_FILE=$PWD/etc/ts-calib.conf export QWS_MOUSE_PROTO=tslib:/dev/input/event0 export TSLIB_CALIBFILE=$PWD/etc/ts-calib.conf export LANG=zh_CN insmod/mnt/yaffs/ts-uptech.o 4、执行触摸屏校准程序(在 qt-embedded-4.4.0 下)./bin/ts_calibrate 如果出现如下错误提示:Couldnt load module pthres No raw modules loaded.ts_config:Success 则更改 ts.conf 配置文件选择一个输入设备 vi/mnt/nfs/Trolltech/qt-embedded-4.4.0/etc/ts.conf#Uncomment if you wish to use the linux input layer event interface#module_raw input 更改为#Uncomment if you wish to use the linux input layer event interface module_raw input 若提示:/dev/touchscreen/ucb1x00:No such file or directory 则一般是环境变量没设置好的问题,可以重新仔细检查环境变量的设置 再次执行触摸屏校准程序即可,程序会自动存储坐标校准信息以便应用程序 使用。此时如无错误则进入 5 点触摸屏校准程序并存储配置文件 还可以进行其他触摸屏测试程序 ts_test、ts_print 等 5、执行 QT/E 带触摸屏的例子程序 在宿主机端拷贝编译好的 ARM 端可执行程序 cp /home/sprife/qt4/for_arm/qt-embedded-linux-opensource-src-4.4.0/e xamples/widgets/digitalclock/digitalclock /arm2410cl/Trolltech/qt-embedded-4.4.0/ARM 端执行该程序 ./digitalclock qws 如出现如下错误:error while loading shared libraries:libz.so.1:cannot open shared object file:No such file or directory 则在宿主机端交叉编译器目录下搜索该库文件,拷贝到 lib 目录下(具体库 文件位置可能不尽相同)cp/opt/host/armv4l/armv4l-unknown-linux/lib/libz.so.1 /arm2410cl/Trolltech/qt-embedded-4.4.0/lib/若出现错误 error while loading shared libraries:libstdc+.so.6:cannot open shared object file:No such file or directory 拷贝 cp /home/bc/gcc-3.4.2-glibc-2.2.5/arm-linux/arm-linux/lib/libstdc+.so.6/arm2410cl/Trolltech/qt-embedded-4.4.0/lib/若 error while loading shared libraries:libgcc_s.so.1:cannot open shared object file:No such file or directory 则 cp /home/bc/gcc-3.4.2-glibc-2.2.5/arm-linux/arm-linux/lib/libgcc_s.s o.1/arm2410cl/Trolltech/qt-embedded-4.4.0/lib/如果想让 QT/E 在 ARM 端支持 USB 鼠标,可以在 ARM 端配置环境变量 2410 核心板:export QWS_MOUSE_PROTO=MouseMan:/dev/input/mouse0 270 核心板:export QWS_MOUSE_PROTO=TPanel:/dev/touchpanel MouseMan:/dev/mice 即可。三、三、编写编写 QTQT-X11X11 程序程序 进入 QT for X11 目录中进行设计 rootvm-dev#cd/home/sprife/qt4/for_pc/建立实验目录 rootvm-dev for_pc#mkdir testqt-x11 rootvm-dev for_pc#cd testqt-x11/rootvm-dev testqt-x11#使用 designer 编辑程序控件 rootvm-dev testqt-x11#/home/sprife/qt4/for_pc/qt-x11-opensource-src-4.4.0/bin/designer 将弹出 designer 界面 选择一个窗口布局 Widget 点击-创建 拖拽几个简单的控件(TextEdit、PushBotton、TextLabel)进行界面设计 初始化控件及相关属性内容 建立信号与槽的连接 show 按钮与文本编辑框的连接 clicked-clear 第二组信号与槽的连接 exit 按钮与 Form 的连接 clicked-closed 退出界面编辑保存为 UI 格式 testx11.ui rootvm-dev testqt-x11#/home/sprife/qt4/for_pc/qt-x11-opensource-src-4.4.0/bin/designer rootvm-dev testqt-x11#ls testx11.ui rootvm-dev testqt-x11#编辑 main.cpp 函数 rootvm-dev testqt-x11#vi main.cpp#include ui_testx11.h int main(int argc,char*argv)QApplication app(argc,argv);QWidget*widget=new QWidget;Ui:Form ui;ui.setupUi(widget);widget-show();return app.exec();rootvm-dev testqt-x11#ls main.cpp testx11.ui rootvm-dev testqt-x11#编译程序生成工程文件.pro rootvm-dev testqt-x11#/home/sprife/qt4/for_pc/qt-x11-opensource-src-4.4.0/bin/qmake-project rootvm-dev testqt-x11#ls testqt-x11.pro testx11.ui main.cpp 编译生成 Makefile 文件 rootvm-dev testqt-x11#/home/sprife/qt4/for_pc/qt-x11-opensource-src-4.4.0/bin/qmake rootvm-dev testqt-x11#ls Makefile testqt-x11.pro testx11.ui main.cpp 编译生成可执行文件 rootvm-dev testqt-x11#make/usr/local/Trolltech/Qt-x11-4.4.0/bin/uic testx11.ui-o ui_testx11.h g+-c-pipe-O2-Wall-W-D_REENTRANT-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr/local/Trolltech/Qt-x11-4.4.0/mkspecs/linux-g+-I.-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include-I.-I.-I.-o main.o main.cpp g+-Wl,-rpath,/usr/local/Trolltech/Qt-x11-4.4.0/lib-o testqt-x11 main.o -L/usr/local/Trolltech/Qt-x11-4.4.0/lib-lQtGui-L/usr/local/Trolltech/Qt-x11-4.4.0/lib-L/usr/X11R6/lib-lpng-lSM-lICE-pthread-pthread-lXi-lXrender-lXrandr-lfreetype-lfontconfig-lXext-lX11-lQtCore-lz-lm-pthread-lgthread-2.0-lglib-2.0-lrt-ldl-lpthread rootvm-dev testqt-x11#ls main.cpp main.o Makefile testqt-x11 testqt-x11.pro testx11.ui ui_testx11.h rootvm-dev testqt-x11#程序编译成功了 执行编译好的程序测试下观察效果 rootvm-dev testqt-x11#./testqt-x11 QTQT 编程扩展编程扩展 编写自定义的信号与槽编写自定义的信号与槽 将上个实验的目录复制出另一份并改名为 testmyqt-x11 rootvm-dev for_pc#cd/home/sprife/qt4/for_pc/rootvm-dev for_pc#cp testqt-x11 testmyqt-x11 arf rootvm-dev for_pc#cd testmyqt-x11/rootvm-dev testmyqt-x11#编辑自己定义槽函数的头文件 myqt.h 包含 ui_testx11.h 头文件,继承其控件类,定义自定义函数文件处理函数MyDlg(),并添加自定义函数槽 ShowInfo()即可。rootvm-dev testmyqt-x11#vi myqt.h#ifndef _DLG_H_#define _DLG_H_#include#include class MyDlg:public QDialog Q_OBJECT public:MyDlg();public slots:void ShowInfo();private:Ui:Form ui;#endif 编辑 myqt.cpp 文件进行函数功能实现 其中 ShowInfo()函数槽中定义改变 textEdit 的内容为“Welcome to UP-TECH”rootvm-dev testmyqt-x11#vi myqt.cpp#include myqt.h MyDlg:MyDlg()ui.setupUi(this);QObject:connect(ui.pushButton,SIGNAL(clicked(),this,SLOT(ShowInfo();QObject:connect(ui.pushButton_2,SIGNAL(clicked(),this,SLOT(close();void MyDlg:ShowInfo()ui.textEdit-setHtml(QApplication:translate(Form,n n p,li white-space:pre-wrap;n n Welcome to UP-TECH,0,QApplication:UnicodeUTF8);重新编辑 main.cpp 函数 包含自定义头文件并使用自定义的类对象进行 QT 实现 rootvm-dev testmyqt-x11#vi main.cpp#include#include myqt.h int main(int argc,char*argv)QApplication app(argc,argv);MyDlg*mydlg=new MyDlg;return mydlg-exec();查看当前目录文件 rootvm-dev testmyqt-x11#ls main.cpp myqt.cpp myqt.h testx11.ui rootvm-dev testmyqt-x11#编译程序:rootvm-dev testmyqt-x11#/home/sprife/qt4/for_pc/qt-x11-opensource-src-4.4.0/bin/qmake-project rootvm-dev testmyqt-x11#/home/sprife/qt4/for_pc/qt-x11-opensource-src-4.4.0/bin/qmake rootvm-dev testmyqt-x11#make/usr/local/Trolltech/Qt-x11-4.4.0/bin/uic testx11.ui-o ui_testx11.h g+-c-pipe-O2-Wall-W-D_REENTRANT-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr/local/Trolltech/Qt-x11-4.4.0/mkspecs/linux-g+-I.-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include-I.-I.-I.-o main.o main.cpp g+-c-pipe-O2-Wall-W-D_REENTRANT-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr/local/Trolltech/Qt-x11-4.4.0/mkspecs/linux-g+-I.-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include-I.-I.-I.-o myqt.o myqt.cpp/usr/local/Trolltech/Qt-x11-4.4.0/bin/moc-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr/local/Trolltech/Qt-x11-4.4.0/mkspecs/linux-g+-I.-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include-I.-I.-I.myqt.h-o moc_myqt.cpp g+-c-pipe-O2-Wall-W-D_REENTRANT-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr/local/Trolltech/Qt-x11-4.4.0/mkspecs/linux-g+-I.-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include-I.-I.-I.-o moc_myqt.o moc_myqt.cpp g+-Wl,-rpath,/usr/local/Trolltech/Qt-x11-4.4.0/lib-o testmyqt-x11 main.o myqt.o moc_myqt.o -L/usr/local/Trolltech/Qt-x11-4.4.0/lib-lQtGui-L/usr/local/Trolltech/Qt-x11-4.4.0/lib-L/usr/X11R6/lib-lpng-lSM-lICE-pthread-pthread-lXi-lXrender-lXrandr-lfreetype-lfontconfig-lXext-lX11-lQtCore-lz-lm-pthread-lgthread-2.0-lglib-2.0-lrt-ldl-lpthread rootvm-dev testmyqt-x11#ls main.cpp main.o Makefile moc_myqt.cpp moc_myqt.o myqt.cpp myqt.h myqt.o testmyqt-x11 testmyqt-x11.pro testx11.ui ui_testx11.h rootvm-dev testmyqt-x11#编译成功执行可执行文件 rootvm-dev testmyqt-x11#./testmyqt-x11 如图:点击自己重新定义的 show 按钮,查看自己定义的功能。QTQT 国际化实现国际化实现 建立实验文件目录 rootvm-dev for_pc#cp testmyqt-x11 testmyqt-translator/-arf rootvm-dev for_pc#cd testmyqt-translator/打开 testmyqt-x11.pro 工程文件添加国际化支持 rootvm-dev testmyqt-translator#vi testmyqt-x11.pro#Automatically generated by qmake(2.01a)?29 23:42:16 2008#TEMPLATE=app TARGET=DEPENDPATH+=.INCLUDEPATH+=.#Input HEADERS+=myqt.h FORMS+=testx11.ui SOURCES+=main.cpp myqt.cpp TRANSLATIONS+=sprife_en.ts sprife_CN.ts 其中 sprife_en.ts、sprife_CN.ts 为国际化的本地转化文件,文件名可以自己任意定义。提取要国际化的字符串保存在 XML 文件中 rootvm-dev testmyqt-translator#/home/sprife/qt4/for_pc/qt-x11-opensource-src-4.4.0/bin/lupdate testmyqt-x11.pro 利用 linguist 进行字符串的逐个国际化翻译 rootvm-dev testmyqt-translator#/home/sprife/qt4/for_pc/qt-x11-opensource-src-4.4.0/bin/linguist 打开 sprife_CN.ts 文件逐个翻译 翻译好后点击-另发布为 保存为 sprife_CN.qm 文件 rootvm-dev testmyqt-translator#ls main.cpp Makefile myqt.cpp myqt.h sprife_CN.qm sprife_CN.ts sprife_en.ts testmyqt-x11 testmyqt-x11.pro testx11.ui rootvm-dev testmyqt-translator#重新编辑 main.cpp 文件添加翻译器支持 rootvm-dev testmyqt-translator#vi main.cpp#include#include#include myqt.h int main(int argc,char*argv)QApplication app(argc,argv);QTranslator translator;translator.load(QString(argv1);app.installTranslator(&translator);MyDlg*mydlg=new MyDlg;return mydlg-exec();其中 translator.load(QString(argv1);为根据执行参数选择翻译语言,这里我们只翻译一种中文来做例子,用户可以添加多个翻译文本进来 编译程序 查看自己国际化的效果:rootvm-dev testmyqt-translator#make/usr/local/Trolltech/Qt-x11-4.4.0/bin/uic testx11.ui-o ui_testx11.h g+-c-pipe-O2-Wall-W-D_REENTRANT-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr/local/Trolltech/Qt-x11-4.4.0/mkspecs/linux-g+-I.-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include-I.-I.-I.-o main.o main.cpp g+-c-pipe-O2-Wall-W-D_REENTRANT-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr/local/Trolltech/Qt-x11-4.4.0/mkspecs/linux-g+-I.-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include-I.-I.-I.-o myqt.o myqt.cpp/usr/local/Trolltech/Qt-x11-4.4.0/bin/moc-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr/local/Trolltech/Qt-x11-4.4.0/mkspecs/linux-g+-I.-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include-I.-I.-I.myqt.h-o moc_myqt.cpp g+-c-pipe-O2-Wall-W-D_REENTRANT-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr/local/Trolltech/Qt-x11-4.4.0/mkspecs/linux-g+-I.-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtCore-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include/QtGui-I/usr/local/Trolltech/Qt-x11-4.4.0/include-I.-I.-I.-o moc_myqt.o moc_myqt.cpp g+-Wl,-rpath,/usr/local/Trolltech/Qt-x11-4.4.0/lib-o testmyqt-x11 main.o myqt.o moc_myqt.o -L/usr/local/Trolltech/Qt-x11-4.4.0/lib-lQtGui-L/usr/local/Trolltech/Qt-x11-4.4.0/lib-L/usr/X11R6/lib-lpng-lSM-lICE-pthread-pthread-lXi-lXrender-lXrandr-lfreetype-lfontconfig-lXext-lX11-lQtCore-lz-lm-pthread-lgthread-2.0-lglib-2.0-lrt-ldl-lpthread rootvm-dev testmyqt-translator#执行查看效果 rootvm-dev testmyqt-translator#./testmyqt-x11 sprife_CN 国际化时候要注意本地语言的字体问题,如果加入本地不支持的字体,还需要在fond/fondir 中添加相应字体的支持才可以正常显示国际化字体。