用Eclipse开发QT4的C++程序

来源:互联网 发布:linux开机硬件启动顺序 编辑:程序博客网 时间:2024/06/02 06:21

用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 buttonObjectname 属性为 helloButton,text属性为”Clickme”, labelObject namehelloLabel, 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, 添加如下代码:

privateslots:

void on_helloButton_clicked();


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

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

void HelloWorld::on_helloButton_clicked(){

ui.helloLabel->setText("Great!Yourfirst Qtapplication 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->Runconfigurations, 配置如下




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

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



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

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