用Eclipse开发QT4的C++程序

用Eclipse开发QT4的C++程序

 

如何安装


我用的平台是Ubuntu904,在Ubuntu下安装QT4的相当简单的事情:

sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-designer ctags


如果本机还没有安装C++开发环境,那你还需要:

 sudo apt-get install build-essential g++ gcc- 4.3 -doc


出于推崇Linux的目的,这里只介绍Ubuntu平台的安装方法. Windows平台的同学请自行Google. :-)

配置Eclipse开发环境

从这个地址下载Eclipse CDT. 记得下载C/C++开发版本. 顺便说句题外话,现在的Eclipse版本号是Galileo,这是谁? 玩了两个铁球同时着地的伟大科学家加利略! 向伟人致敬.

Eclipse download: http://www.eclipse.org/downloads/

从这个地址下载Qt的Eclipse插件:

QT4 Integration download: http://www.qtsoftware.com/developer/eclipse-integration

打开Eclipse , 在"Window->Preference->qt" 里配置如下信息:

  • Name : Qt4
  • Bin Path : /usr/bin
  • Include Path : /usr/include/qt4

这是Ubuntu8.x/904 默认安装的qt4路径.如果你的路径和此不同,请自行修改.

然后你会看到一个漂亮的GUI设计界面. 曾经在vb/delphi 下做过程序的同学,估计会对这个界面感慨万千.......


一个HelloWorld工程

QT4的官方网站有一篇很棒的介绍如何在Eclipse下进行开发的文章:  http://doc.trolltech.com/qt-eclipse-1.5.0/eclipse-integration-getting-started.html

但这篇文章有点长. 所以我自行写了一个HelloWorld例子介绍开发流程. 

首先,New 一个QT Gui Project:


<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

next 之后, 输入lHelloWorld 作为工程名字, 如下图:




<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

你可以直接点Finish 结束. 也可以点Next 看看QT 为你生成了什么:



<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

我们不需对预定义的设置作改动. 直接点Finish 即可. 结束工程配置之后, 你会看到QT 为你生成了许多文件:




<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

双击helloworld.ui 文件, 拖动一个push button 和 一个label 对象到helloworld.ui . 并且设置 push buttonObject name 属性为 helloButton,text 属性为”Click me”, labelObject name helloLabel, text 属性为”Hello,QT!” 如下图:





<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

现在, 我们想在单击button 的时候改变label 显示的文字. 如果用VB/Delphi/ 浏览器里常用的事件术语来描述我们的需求的话, 需求如下:

1 单击button 的时候, 发生一个buttonclick 事件.

2 必须编写一个事件处理函数,buttononclick 发生后, 处理该事件.

3 这个事件处理函数的内容很简单. 改变labeltext 值即可.


很显然, 我们只需要关注3, 即如何添加事件处理函数即可.


打开helloworld.h, 添加如下代码:

private slots:

void on_helloButton_clicked ();


<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

打开helloworld.cpp 添加如下代码:

void HelloWorld::on_helloButton_clicked (){

ui . helloLabel -> setText ( "Great!Your first Qt application works fine!" );

}


最后,这两个文件的代码如下所示:

helloworld.h

#ifndef HELLOWORLD_H
#define  HELLOWORLD_H

#include 
< QtGui / QWidget >
#include 
" ui_helloworld.h "

class  HelloWorld :  public  QWidget
{
    Q_OBJECT

public :
    HelloWorld(QWidget 
* parent  =   0 );
    
~ HelloWorld();

private :
    Ui::HelloWorldClass ui;

private  slots:
    
void  on_helloButton_clicked();


};

#endif   //  HELLOWORLD_H

helloworld.cpp

#include  " helloworld.h "

HelloWorld::HelloWorld(QWidget 
* parent)
    : QWidget(parent)
{
    ui.setupUi(
this );
}

HelloWorld::
~ HelloWorld()
{

}

void  HelloWorld::on_helloButton_clicked(){
    ui.helloLabel
-> setText( " Great!Your first Qt application works fine! " );
}

<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

快捷键 ctrl+b 编译工程 . 然后打开 Eclipse->Run->Run configurations, 配置如下




<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

然后点击run, 你第一个Qt 程序就跑起来了.



<!-- @page { margin: 2cm } P { margin-bottom: 0.21cm } -->

伟大的<C programming language> 里说过, 学一门语言, 只要你懂得如何写程序, 编译, 运行, 那么学习这门语言将不再是难事. 本文介绍了在Eclipse 里基本的Qt 开发流程, 希望对大家有帮助.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值