第一步:
建立hello.pro文件,该文件的作用是为建立Makefile文件,如下例所示:
TEMPLATE = app
CONFIG += qt staticlib thread warn_on release
HEADERS += hello.h
SOURCES += hello.cpp/
main.cpp
TARGET = hello
其中SOURCES += hello.cpp识别源码中的.cpp源文件。可以添加多个.cpp文件
如有多个头文件,可以如下例所示添加:
HEADERS += += hello.h/
main.h
第二步:
使用qmake工具生成Makefile。如下例所示:
qmake -o Makefile hello.pro
此时可看到在hello.pro所在目录生成了一个Makefile文件。
第三步:
有了Makefile后,现在开始编程了。建立hello.cpp文件,其内容如下:
#include <qlayout.h>
#include <qpushbutton.h>
#include "hello.h"
Hello::Hello(QWidget * parent,const char * name)
: QDialog(parent,name)
{
setCaption(tr("Hello"));
resize(240,300);
}
这是一个继承了QDialog的Hello类。
再建立hello.h头文件,其内容如下:
#ifndef HELLO_H
#define HELLO_H
#include <qdialog.h>
class Hello : public QDialog
{
public:
Hello(QWidget * parent = 0,const char * name = 0);
};
#endif
最后建立main.cpp文件:
#include <qapplication.h>
#include <qfont.h>
#include "hello.h"
QFont font("",11,QFont::Bold);
int main(int argc,char ** argv)
{
QApplication app(argc,argv);
app.setFont(font,TRUE,0);
Hello * dlg = new Hello();
app.setMainWidget(dlg);
dlg->show();
return app.exec();
}
其作用是调用刚才我们写好的Hello类,并在屏幕显示。
第四步:
编译上面的几个文件,如无意外,可以得到一个hello文件。在终端上输入./hello运行该程序,可以看到屏幕上显示一个240*320大小的空对话框。
后记:在本例中,我们并没有涉及到信号与插槽,也没有涉及到交叉编译的问题,所以非常简单。目的是为了让大家了解一下QT开发的简单流程。下一节中我会继续为大家介绍更多的QT开发经验。