开发文档
程序的功能:可控地循环显示一指定字符串。
Ui界面:包含三个按钮(start,stop,save&close),一个编辑框(TextBrowser)。
窗口部件功能:start用于开始循环,stop停止循环save&close保存并关闭窗口,编辑框用于显示字符串。
设计工具:Qtopia2.2.0 designer
实现方法(算法):
发射QPushButton::clicked()信号
按下start按钮
调用与clicked()相连的startButtonDown()槽函数
QTimer::timeout()信号发射
显示String并调用QTimer::start(0)启动定时器,设置超时为0s
调用与timeout()相连的startButtonDown()
步骤:
1、设置X86的环境变量。
2、运行designer,按照要求设计并保存serial.ui。
3、 制作ui2cpp脚本并运行。之后得到serial.h/serial.cpp/moc_serial.cpp三个文件。分析三个文件的内容可以得到以下结论:①Qtopia把刚才设计的ui界面作为统一的整体封装到一个
叫做serial的同名类里面。②Serial.h与serial.cpp作为该serial类的头文件和源代码文件在运行ui2cpp脚本后由系统生成。③Moc_serial.cpp是经过moc元对象生成的文件。具体是moc读取C++源文件,如果发现有Q_OBJECT宏声明的类,它就会生成另外一个C++源文件,这个新生成的文件中包含有该类的元对象代码。
4、制作main.cpp文件。个人理解是天堑或者Qtopia为了便于管理便把main函数封装起来了。
5、用tmake工具产生工程文件 Serial.pro。
6、修改工程文件。具体就是把第二行的qt修改成qtopia。
7、用tmake工具产生Makefile文件。
8、修改Makefile文件,去掉重复的部分和添加新的内容。具体修改的是:①指定编译后生成的目标文件、桌面启动器文件和图标文件的存放位置;②指定链接所需要的桌面启动器文件与图标文件;③修改链接时所需清理的文件;④删除源文件重复编译的项目。
9、制作桌面启动器serial.desktop。
10、制作桌面图标文件,格式必须是png,大小为16*16。
11、修改serial.h serial.cpp,完整时间界面的功能。具体源代码请参考文档的附录部分。
附录:其中加粗倾斜的是手工添加的,其余的都是qtopia通过serial.ui自动生成的。
/***************** serial.h ****************/
#ifndef SERIAL_RECEIVER_H
#define SERIAL_RECEIVER_H
#include <qvariant.h>
#include <qwidget.h>
#include <stdio.h> //~~~files operation in C
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QPushButton;
class QTextBrowser;
class QTimer; //~~~
class QString; //~~~
class serial_receiver : public QWidget
{
Q_OBJECT
public:
serial_receiver( QWidget* parent = 0, const char* name = 0, WFlags fl = 0 );
~serial_receiver();
QTextBrowser* TextBrowser;
QPushButton* PushButton_start;
QPushButton* PushButton_stop;
QPushButton* PushButton_close;
QTimer* timer; //~~~the most improtant class for the loop
QString* buf; //~~~store string which get from socket
QString* temp; //~~~stort strings which will be showed in TextBrower
FILE* fp; //~~~declare of the data-file point
public slots:
virtual void startButtonDown();
virtual void stopButtonDown();
};
#endif // SERIAL_RECEIVER_H
/************* serial.cpp ***************/
#include "serial.h"
#include <qpushbutton.h>
#include <qtextbrowser.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qtimer.h> //~~~declare QTimer class
serial_receiver::serial_receiver( QWidget* parent, const char* name, WFlags fl )
: QWidget( parent, name, fl ) //构造函数
{
if ( !name )
setName( "serial_receiver" );
resize( 295, 198 );
setCaption( tr( "serial receiver" ) );
TextBrowser = new QTextBrowser( this, "TextBrowser" );
TextBrowser->setGeometry( QRect( 10, 10, 280, 130 ) );
PushButton_start = new QPushButton( this, "PushButton_start" );
PushButton_start->setGeometry( QRect( 10, 150, 80, 41 ) );
PushButton_start->setText( tr( "Start" ) );
PushButton_stop = new QPushButton( this, "PushButton_stop" );
PushButton_stop->setGeometry( QRect( 110, 150, 80, 41 ) );
PushButton_stop->setText( tr( "Stop" ) );
PushButton_close = new QPushButton( this, "PushButton_close" );
PushButton_close->setGeometry( QRect( 210, 150, 70, 41 ) );
PushButton_close->setText( tr( "Close&&Save" ) ); //~~~set the text of "close button"
timer=new QTimer(this); //~~~
temp=new QString(); //~~~initialize
buf=new QString(); //~~~
if ((fp=fopen("/","a+"))==NULL) //~~~location of data file
fp=fopen("/","w+");
// signals and slots connections
connect( PushButton_start, SIGNAL( clicked() ), this, SLOT( startButtonDown() ) );
connect( PushButton_stop, SIGNAL( clicked() ), this, SLOT( stopButtonDown() ) );
connect( PushButton_close, SIGNAL( clicked() ), this, SLOT( close() ) );
connect( timer, SIGNAL( timeout() ), this, SLOT( startButtonDown() ) ); //~~~set signal and slots
}
serial_receiver::~serial_receiver()//析构函数
{
fclose(fp); //~~~close and save the data-file}
void serial_receiver::startButtonDown()
{
char str_text[]="this is for testing\n";
//~~~an interface for char in order to vertical display we can add '\n' on the end of str_text
fputs(str_text,fp);
*buf=str_text;
*temp=*temp+*buf;
TextBrowser->setText(*temp);
timer->start(0); //~~~set time for updating
}
字符串函数strip的作用是什么void serial_receiver::stopButtonDown()
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论