Ubuntu中使用QT3和QT4实例解析


刚开始学习C++,对很多东西都不了解,由于在Ubuntu上,所以很容易的认识了QT,就像在Windows上会很容易认识MFC一样。QT确实很强大,我决定把我的学习过程记录下来,以备日后参考。先写一下我的入门知识(内容来自Ubuntu官方的wiki.ubuntu.org.cn ),"Hello ubuntu!"。第一个是简单的QT3程序:

首先建立一个目录qt3hello,然后在里面新建一个文件main.cpp,内容如下:

#include
#include

int main( int argc, char **argv )
{
QApplication a( argc, argv);

QPushButton hello("Hello ubuntu!", 0);
hello.resize(100,30);

a.setMainWidget( &hello);
hello.show();
return a.exec();
}


然后用以下命令编译运行

$ cd ~/qt3hello
$ qmake -project
$ qmake
$ make
$ ./qt3hello


这一个是QT4的例子,内容同样来自wiki.ubuntu.org.cn,但那上面的编译命令是错的,浪费了我这种初学者很多时间。我改正了一下。

首先建立文件夹qt4hello,在其中建立文件main.cpp,内容如下:

#include
#include

int main( int argc, char *argv[])
{
QApplication app(argc, argv);

QPushButtion hello("Hello Ubuntu!");
hello.resize(100,30);

hello.show();
return app.exec();
}


使用以下命令编译

$ cd ~/qt4hello
$ qmake-qt4 -project
$ qmake-qt4
$ make
 
然后运行

$ ./qt4hello


OK!

下面对QT程序的执行总结一下。以QT3为例,QT4也差不多。

头文件qapplication.h包含了类QApplaction的定义。所有QT程序都必需QApplication的一个对象。QApplication管理着程序的很多资源,能够实时监测系统状态对程序作出反馈。

头文件qpushbutton.h包含QPushButton类的定义。

首先生成一个QApplication对象,然后用a.setMainWidget(&hello)将QPushButton对象作为这个应用程序的主窗口部件。

a.exec();是将控制交给QT的时候,当程序执行完时,exec()会返回。

到这已经大体明白了QT的最基本的东西。我相信自己会把它给搞定的。 

相关内容