`
天海浪人
  • 浏览: 35497 次
  • 性别: Icon_minigender_1
  • 来自: 济南
最近访客 更多访客>>
社区版块
存档分类
最新评论

ubuntu下使用QT3和QT4

阅读更多

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

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

Cpp代码 复制代码
  1. <SPAN style="FONT-SIZE: x-small">#include <qapplication.h>   
  2. #include <qpushbutton.h>   
  3.   
  4. int main( int argc, char  **argv )   
  5. {   
  6.           QApplication a( argc, argv);   
  7.   
  8.           QPushButton hello("Hello ubuntu!", 0);   
  9.           hello.resize(100,30);   
  10.              
  11.           a.setMainWidget( &hello);   
  12.           hello.show();   
  13.           return a.exec();   
  14. }</SPAN>  
#include <qapplication.h>
#include <qpushbutton.h>

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();
}



 然后用以下命令编译运行

Cpp代码 复制代码
  1. <SPAN style="FONT-SIZE: x-small">$ cd ~/qt3hello   
  2. $ qmake -project   
  3. $ qmake   
  4. $ make   
  5. $ ./qt3hello</SPAN>  
$ cd ~/qt3hello
$ qmake -project
$ qmake
$ make
$ ./qt3hello



 

 

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

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

Java代码 复制代码
  1. <SPAN style="FONT-SIZE: x-small">#include <QApplication>   
  2. #include <QPushButton>   
  3.   
  4. int main( int argc, char *argv[])   
  5. {   
  6.          QApplication app(argc, argv);   
  7.     
  8.          QPushButtion hello("Hello Ubuntu!");   
  9.          hello.resize(100,30);   
  10.   
  11.          hello.show();   
  12.          return app.exec();   
  13. }</SPAN>  
#include <QApplication>
#include <QPushButton>

int main( int argc, char *argv[])
{
         QApplication app(argc, argv);
 
         QPushButtion hello("Hello Ubuntu!");
         hello.resize(100,30);

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



使用以下命令编译

Cpp代码 复制代码
  1. <SPAN style="FONT-SIZE: x-small">$ cd ~/qt4hello   
  2. $ qmake-qt4  -project   
  3. $ qmake-qt4   
  4. $ make   
  5. </SPAN>  
$ cd ~/qt4hello
$ qmake-qt4  -project
$ qmake-qt4
$ make




 然后运行

Cpp代码 复制代码
  1. <SPAN style="FONT-SIZE: x-small">$ ./qt4hello</SPAN>  
$ ./qt4hello




OK!

 

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

 

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


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


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


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

 

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

转载于wolfplanet。iteye.com

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics