QT编程(2):应用qt-<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />2.3.2编译QT程序,可以下载到开发板中,qt-3.1不可以,如有自己定义的信号槽,需要用moc转换后,再用g++ 进行编译.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
1. 设置编译路径:
(1)export QTDIR="/home/qt-"2.3.2/
(2)export PATH=$QTDIR/bin:$PATH (3)exportLD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH
2.编辑ui界面:终端中进入qt-2.3.2->bin中, ./desigher .file->new->dialog->确定,加一个数码显示,一个增加按钮,一个关闭按钮,文件名为custom,保存在home下的文件夹中,如xw. 打开xw可以看到多了一个.ui后缀文件.
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />
3.转化为.h文件: uic -o custom.h custom.ui,可以看到又多了一个.h后缀文件.
4.右键单击h文件->打开方式->gedit,注释掉 //Q_OBJECT
5.转化为.cpp文件: uic -o custom.cpp -impl custom.h custom.ui 可以看到xw中多了一个custom.cpp.
6.h文件中增加槽定义: public slots: void add(); 如下示:
/****************************************************************************
** Form interface generated from reading ui file 'custom.ui'
**
** Created: Thu Jul 2 08:38:19 2009
** by: The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#ifndef CUSTOM_H
#define CUSTOM_H
#include <qvariant.h>
#include <qdialog.h>
class QVBoxLayout;
class QHBoxLayout;
class QGridLayout;
class QLCDNumber;
class QPushButton;
class custom : public QDialog
{
// Q_OBJECT
public:
custom( QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 );
~custom();
QLCDNumber* LCDNumber1;
QPushButton* PushButton1;
QPushButton* PushButton2;
public slots: void add(); //为新添加部分
};
#endif // CUSTOM_H
7.在custom.cpp文件中实现函数:
void custom::add()
{
LCDNumber1->display((LCDNumber1->value())+1);
}
信号槽连接部分添加的语句:
connect(PushButton1,SIGNAL(clicked()),SLOT(add()));
connect(PushButton2,SIGNAL(clicked()),SLOT(close()));
函数如下:
/****************************************************************************
** Form implementation generated from reading ui file 'custom.ui'
**
** Created: Thu Jul 2 08:40:32 2009
** by: The User Interface Compiler (uic)
**
** WARNING! All changes made in this file will be lost!
****************************************************************************/
#include "custom.h"
#include <qlcdnumber.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
/*
* Constructs a custom which is a child of 'parent', with the
* name 'name' and widget flags set to 'f'
*
* The dialog will by default be modeless, unless you set 'modal' to
* TRUE to construct a modal dialog.
*/
custom::custom( QWidget* parent, const char* name, bool modal, WFlags fl )
: QDialog( parent, name, modal, fl )
{
if ( !name )
setName( "custom" );
resize( 596, 480 );
setCaption( tr( "custom" ) );
LCDNumber1 = new QLCDNumber( this, "LCDNumber1" );
LCDNumber1->setGeometry( QRect( 190, 90, 191, 61 ) );
PushButton1 = new QPushButton( this, "PushButton1" );
PushButton1->setGeometry( QRect( 110, 230, 131, 61 ) );
PushButton1->setText( tr( "add" ) );
PushButton2 = new QPushButton( this, "PushButton2" );
PushButton2->setGeometry( QRect( 340, 230, 90, 50 ) );
PushButton2->setText( tr( "close" ) );
connect(PushButton1,SIGNAL(clicked()),SLOT(add())); //信号槽的连接
connect(PushButton2,SIGNAL(clicked()),SLOT(close()));
}
void custom::add() //加1函数的实现
{
LCDNumber1->display((LCDNumber1->value())+1);
}
/*
* Destroys the object and frees any allocated resources
*/
custom::~custom()
{
// no need to delete child widgets, Qt does it all for us
}
8.主函数的头文件包含及改动,头文件名要对应,下面w前的类变为custom,如下:
#include <qapplication.h>
#include "custom.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
custom w;
w.show();
a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
return a.exec();
}
9.此时功能实现部分已经完成,下面要进行编译,因为有定义的槽,所以要转换为标准的C文件,具体实现如下:
(1)右键单击h文件->打开方式->gedit,把注释掉 //Q_OBJECT的语句还原, Q_OBJECT .
(2)转化成标准C文件:终端中输入,moc custom.h –o custom.moc.cpp
10.最后整体编译:g++ -o custom custom.moc.cpp custom.cpp main.cpp –L /home/qt-2.3.2/lib –lqt –I /home/qt-2.3.2/include/
编译完成后可以看到多了个可执行文件custom.
文章评论(0条评论)
登录后参与讨论